Sending HTML emails in WordPress

Sounds simple, but WordPress defaults to text emails, so if you want to send pretty HTML ones, then you need to let it know.

Previously I have done this in a bit of a convoluted way…

add_filter('wp_mail_content_type','set_html_mail_content_type');
wp_mail($to_address, $subject, $message);
remove_filter('wp_mail_content_type','set_html_mail_content_type');

This relies on this function to work…

function set_html_mail_content_type() {
  return 'text/html';
}

You have to remember to remove the filter after sending, so that other emails aren’t affected by this, just the one that you’re sending. Messy.

However, I have since found it’s much easier than that, check this out…

$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail($to_address, $subject, $message, $headers);

Ah, that’s better!