Home    Articles

 

How to send html emails with wordpress wp_mail() function

 

wordpress-logo-14064_500x210

WordPress uses his own email function that send emails, wp_mail(). The email send function is very easy to be used as easy is PHP mail() function.

The sintax for wp_mail() is:

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

The wp_mail() parameters meaning can be found here.

In order to send emails like a HTML page you need to tell wordpress what you want.

Your email will contain HTML tags and you have to set the HTML content type as “text/html”.
For that you need to call a function that will return the type (function set_html_content_type).
The function that will return the email type is called by the add_filter() wordpress function.

add_filter( 'wp_mail_content_type', 'set_html_content_type' );
wp_mail ( 'somebody@domain.com', 'Mail subject', 'A <strong>HTML</strong> email' );
function set_html_content_type() 
{
  return 'text/html';
}

After sending the email as html do not forget to remove the filter in order to avoid some conflicts in the code flow.

remove_filter ( ‘wp_mail_content_type’, ‘set_html_content_type’ );

This will help you to make nice newsletters with wordpress by sending emails in HTML format.

That should be all.
Enjoy.

…first time posted on w3bdeveloper.com

by