WordPress has its own email function for sending emails, i.e. wp_mail(). With wp_mail, you can send email as easily as in general PHP function: mail().

The following is the syntax for sending an email with WordPress:

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

Where:

  • $to (required) is the intended recipients. You can also specify multiple recipients by using an array or comma-separated email ids.
  • $subject (required) is the subject of your message.
  • $message (required) is the content of your message.
  • $headers (optional) is the mail headers to be sent with the message.
  • $attachments (optional) is the file name, to be attached to the message. You can also define the filenames with an array.

Since $headers and $attachments are optional parameters, a simple example would look like this:

wp_mail("To Email","Email Subject", "Message");

Set HTML Content Type in wp_mail Function

If your message contains the HTML tags, you’ve to set the HTML content type to ‘text/html’ as follows:

add_filter( ‘wp_mail_content_type’, ‘set_html_content_type’ );

wp_mail( ‘To Email’, ‘Subject’, ‘<h1>Html Email</h1>’ );

function set_html_content_type() {
return ‘text/html’;
}

And don’t forget to use the following filter after using wp_mail(), as to avoid conflicts:

remove_filter( ‘wp_mail_content_type’, ‘set_html_content_type’ );

Add CC/BCC Recipients in wp_mail Function

If you want to use extra information with the message, like From, CC, BCC, you can simply put those in $headers.

$headers[]= “From: YourName <first email>”;
$headers[]= “Cc: ReceiverName <second email>”;
$headers[]= “Bcc: Receiver2Name <third email>”;

If you want to attach a file with your message, you can simply put the file name as follows:

$attachments=array (WP_CONTENT_DIR.’/uploads/file_to_attach.doc’);

Send Emails Using SMTP in WordPress

The function wp_mail() requires you to configure the SMTP at your server. To configure the SMTP, you have to use the PHPMailer Class. This class contains the SMTP settings and responsible for sending emails. To use the PHPMailer Class, we’ve to use action: phpmailer_init as follows:

add_action( 'phpmailer_init', 'configure_smtp' );
function configure_smtp( PHPMailer $phpmailer ){
    $phpmailer->isSMTP(); //switch to smtp
    $phpmailer->Host = 'mail.mydomain.com';
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 25;
    $phpmailer->Username = 'Username Here';
    $phpmailer->Password = 'myemailpassword';
    $phpmailer->SMTPSecure = false;
    $phpmailer->From = 'From Email Here;
    $phpmailer->FromName='Sender Name';
}

Inside of our function, we simply call isSMTP(), set SMTPAuth to true, and provide the appropriate SMTP host and credentials. This is all we have to do to make WordPress send emails through our SMTP server.

Display SMTP Errors in WordPress

Still, Sometime email couldn’t send successfully and there is no way to check SMTP Errors without doing a little hack in wp_mail() function.

if(!$phpmailer ->Send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $phpmailer->ErrorInfo;
    exit;
}