How to fix HTML tags showing up in emails sent with Zoho Mail
I recently ran into an issue sending HTML emails from a WordPress site using Zoho Mail. Zoho Mail provides their own plugin to easily use Zoho Mail’s API to send emails from WordPress. The plugin was just using WordPress’s built-in wp_mail function, which, by default, sends all emails with a content type of ‘text/plain’.
So in order to fix the HTML tags showing in the emails, I just needed to change the default content type for the wp_mail function. And, as expected, WordPress has a filter we can use to do so.
Be sure to use FTP/SFTP when editing theme files. Never use the file editor from within the WordPress dashboard unless it is absolutely necessary and you know what you are doing.
The code to solve the problem
Just copy and paste the following code into your functions.php file and you will be all set:
// Set the default content type for the wp_mail function
add_filter('wp_mail_content_type', 'set_wp_mail_default_content_type');
function set_wp_mail_default_content_type($content_type) {
$content_type = 'text/html';
return $content_type;
}
Or, if you are a fan of closures and short-n-sweet code, use this snippet instead:
// Set the default content type for the wp_mail function
add_filter('wp_mail_content_type', function() {
return 'text/html';
});
Need more help?
As always, just reach out if you need more help. You can reach me through my request a free estimate form.
Tags: Development wp_mail