Is there an action_hook or something similar that could help me achieve this?

I tried adding markup in a PHP string variable and just fired off an email with the wp_mail() function like so:

$email_to = 'someaddress@gmail.com';
$email_subject = 'Email subject';
$email_body = "<html><body><h1>Hello World!</h1></body></html>";
$send_mail = wp_mail($email_to, $email_subject, $email_body);

But it showed up as plain text?

Any ideas?


from wp_mail codex page:

The default content type is 'text/plain' which does not allow using HTML. However, you can set the content type of the email by using the 'wp_mail_content_type' filter.

// In theme's functions.php or plug-in code:

function wpse27856_set_content_type(){
    return "text/html";
add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );
  • 2
    Hmm sounds useful. Just a question, any particular reason why you named your function wpse27856_set_content_type? – racl101 Sep 6 '11 at 21:31
  • 16
    No, it's just a unique name based on the id of this particular question. wpse = wp stachexchange, 27856 is the id of this question in the URL. I just do that to avoid potential collisions if people copy/paste code out of here. – Milo Sep 6 '11 at 21:45
  • 3
    You can also just include the Content-Type in your email headers. Check out how the Notifly plugin does it. – Otto Sep 7 '11 at 0:29
  • oh yeah, ha ha. What a n00b I am. Guess it is the id of this post. – racl101 Sep 7 '11 at 7:44
  • 1
    This will break your password reset email, because the reset link is wrapped in <>. – Simon Josef Kok Oct 24 '17 at 6:08

As an alternative, you can specify the Content-Type HTTP header in the $headers parameter:

$to = 'sendto@example.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8');

wp_mail( $to, $subject, $body, $headers );
  • 3
    This works better as the add_filter sometimes shows as attachment. Thanks for sharing! – deepakssn Feb 17 '18 at 18:54
  • 1
    This is the generally best way to-do this. The top answer will interfere with other plugins and cause problems. – Alex Standiford Dec 6 '19 at 19:12

Don't forget to remove the content type filter after you use the wp_mail function. Following the accepted answer naming you should do this after wp_mail is executed:

remove_filter( 'wp_mail_content_type','wpse27856_set_content_type' );

Check this ticket here - Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578

  • 8
    This should be a comment, not an answer, no? – Bob Diego Jul 26 '17 at 14:31

If you want to send all emails in HTML you can also use aplugin such as http://wordpress.org/plugins/email-templates

  • 1
    Seriously? You are suggesting a plugin to send just HTML email. – Jewel Dec 10 '19 at 17:11
  • Overkill when it's a couple of lines of code... – Andrew Schultz Jan 21 at 5:07
  • Not everyone is a programmer. A plugin could be the easiest solution. – Mark Berry Feb 11 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.