How to Send WordPress HTML Email Correctly

Send HTML Email correctly

If you own a WordPress website, you might need to send HTML emails. Emails are generally sent for registration, order placements, deals, coupons, etc. You can use the wp_mail method to send an email in plain text format. Or you can send an Email in a nice format from a website. Instead of having only plain text, it should contain a logo, a footer, better design elements, social links, etc. 

Let’s say you have created an email content with inline styling and HTML tags. Now, it is expected that your email will render HTML. But that’s not the case, instead of showing the styling it will display plain text along with HTML tags. 

What can we do to make a HTML email work properly?

Solution 1: It can break WordPress core email formatting (Not Recommended)

Email content type can be set to text/html by using a WordPress filter called wp_mail_content_type. You can use all HTML tags in your email by using this content type.

HTML will be automatically rendered in an email by using this filter.

Add the code given below in your functions.php file to use this filter.

add_filter( 'wp_mail_content_type', function( $content_type)

   return'text/html';

} );

Now, if you send emails through your application, they will be HTML emails. 

This Solution create WordPress Core Email Problem:

It seems that you have resolved the problems with HTML emails. Though this method resolves the problem with custom emails, the formatting of WordPress core emails is broken. 

What is a core email?

Email sent by wordpress to the admin/author, whenever someone posts a comment on your blog or whenever you place a request to reset password or any update from wordpress these are known as the core email of wordpress.

To break the formatting of all these core emails, use wp_mail_content_type filter. WordPress uses line breaks in the content of core emails. But when we use the wp_mail_content_type filter, line breaks will not appear.

If you add a wp_mail_content_type filter in the theme, you will get an email without any line break, which is not good for the website. So this has to be fixed.. 

Solution 2: This is the best way for sending HTML Email properly

If you are a website owner, then you always want that your both core and custom emails work with correct formatting. For this, you have to remove wp_mail_content_type from your code. It is never recommended to use this filter on your website. 

Instead, use the wp_mail method in your custom emails. Just read the wp_mail documentation very carefully. You will get to know that it has a parameter called $headers. This will fix your problem.

You just have to use the $header parameter in your custom code and it will send you a custom HTML email. 

You need to send this parameter explicitly in your code so that it doesn’t affect your core emails.

Code : 

<?php

$message= '<h1>Email Heading</h1><p>Email Content</p>';

$headers= array('Content-Type: text/html; charset=UTF-8');

$to= get_option('admin_email');

$subject= "My Email Subject";

$mail= wp_mail($to, $subject, $message, $headers);

?>

Here, I have used the $headers parameter to set the content type text/html. This will be your custom HTML email.

Now you can test your core email by commenting. I hope this article will help you understand how you can send HTML emails correctly. 

Share This Post...
Table of Contents

Add a Comment

Share This Post...