You are currently viewing wp_mail() function to send email using wordpress

wp_mail() function to send email using wordpress

PHP has a built-in function called mail() that allows you to send email. The advantage of mail() is that it’s very easy to use with PHP and WordPress.

wp_mail() function

Here we will explore the details function of wp_mail() to send emails and customize email message as per your need. This post is going to show you some common examples using wp_mail() function.

wp_mail( string|array $to, string $subject, string $message, string|array $headers = '', string|array $attachments = array() )

Mail parameter explanation
$to
(string|array) (Required) Array or comma-separated list of email addresses to send message.

$subject
(string) (Required) Email subject

$message
(string) (Required) Message contents

$headers
(string|array) (Optional) Additional headers. Default value: ”

$attachments
(string|array) (Optional) Files to attach. Default value: array()

The wp_mail() function really only needs three inputs; to, subject, and message as per below.

<?php
$to = ‘test@gmail.com’;
$subject = ‘Email Send’;
$message = ‘Follow, wp_mail() function to send email.’;
wp_mail( $to, $subject, $message );
?>

This is the simple way to send emails.

Header Options

You can also send emails to multiple user using header option parameters. The wp_mail() also take parameter input for headers and attachments. The mail header can be passed like below.

$headers = ‘From: Name test@test.com;’ . “\r\n”;

$headers .= ‘BCC: Second Name user2@gmail.com;’ . “\r\n”;

Here “.” is used to combine 2 users to send mail. We can also mke array of $headers, like below,

$headers = array(
    ‘From: Name <test@test.com>;',
    'BCC: Second Name <user2@gmail.com>;',
);

Now you can call your function like below,

wp_mail( $to, $subject, $message, $headers );

Attachments Options

You can also attach any file in email using attachments like below,

$attach = WP_CONTENT_DIR . '/uploads/attach_file.jpg’;

Or, like $headers, you can make an array given below,

$attach = array(
WP_CONTENT_DIR . '/uploads/attach_file.jpg',
WP_CONTENT_DIR . '/uploads/test.pdf'
 );

Using the WordPress API & WP_MAIL

you can also use the WordPress API to develop your own custom code for sending emails using SMTP. To send e-mail messages with SMTP authentication using the WordPress API, follow these steps:

  1. In the wp-config.php file, copy and paste the below code:
define( 'SMTP_USER',   'test@gmail.com' );    // Your email to use for SMTP authentication
define( 'SMTP_PASS',   'test' );       // Password to use for SMTP authentication
define( 'SMTP_HOST',   'smtp.gmail.com' );    // The hostname of the mail server
define( 'SMTP_FROM',   'test@gmail.com' ); // SMTP From email address
define( 'SMTP_NAME',   'Your Website' );    // SMTP From name
define( 'SMTP_PORT',   '587' );                  // SMTP port number - likely to be 25, 465 or 587
define( 'SMTP_SECURE', 'tls' );                 // Encryption system to use - ssl or tls
define( 'SMTP_AUTH',    true );                 // Use SMTP authentication (true|false)
define( 'SMTP_DEBUG',   0 );                    // for debugging purposes only set to 1 or 2

2. Replace the values with the settings for your own site, and then save your changes to the wp-config.php file.

3. In your plugin or theme’s function.php file, copy and paste the following code:

add_action( 'phpmailer_init', 'send_smtp_email' );
function send_smtp_email( $phpmailer ) {
	$phpmailer->isSMTP();
	$phpmailer->Host       = SMTP_HOST;
	$phpmailer->SMTPAuth   = SMTP_AUTH;
	$phpmailer->Port       = SMTP_PORT;
	$phpmailer->Username   = SMTP_USER;
	$phpmailer->Password   = SMTP_PASS;
	$phpmailer->SMTPSecure = SMTP_SECURE;
	$phpmailer->From       = SMTP_FROM;
	$phpmailer->FromName   = SMTP_NAME;
}

4. To send an e-mail message, call the wp_mail() function. For example:

wp_mail( $to, $subject, $message, $headers); 

WordPress then sends the message using the SMTP authentication settings you defined above. Thats all! Now you can send your custom email with headers and attachments using wordpress.

Leave a Reply