Send HTML Emails Using wp_mail and SMTP in WordPress

You are here : Home / Send HTML Emails Using wp_mail and SMTP in WordPress

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 email with WordPress:

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 with the message. You can also define the filenames with array.

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

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:

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

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.

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:

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 mail couldn’t send successfully and there is no way to check SMTP Errors without doing a little hack in wp_mail() function.

 Send HTML Emails Using wp mail and SMTP in Wordpress

Flipper Code

We have been building WordPress Plugins at flipper code since 2008. We follow wordpress coding standard that ensures we deliver the excellent wordpress plugins and services.

More Posts - Website

Follow Me:
twitter Send HTML Emails Using wp mail and SMTP in Wordpressfacebook Send HTML Emails Using wp mail and SMTP in Wordpressgoogleplus Send HTML Emails Using wp mail and SMTP in Wordpress

Customize Product according to your requirements?

We will be glad to discuss any of your needs related to our wordpress plugins . if any of functionality you'd like to see in products, you can reach us by via email hello@flippercode.com.