Problems with displaying formatted emails in Wordpress

343 views Asked by At

By default, wp_mail function does not support displaying formatted emails. For example, notifications of new comments that have formatted text shows HTML-tags displaying like a plain text.

I solved this problem by adding the following code to the functions.php file:

function set_html_mail_content_type(){
    return "text/html";
}
add_filter( 'wp_mail_content_type','set_html_mail_content_type' );

After that, emails began to come with formatted text and without HTML tags.

But another problem appears. The whole email comes in continuous text: no new lines and no paragraphs: It’s just that all sentences are in a one row.

Do you have any idea what it might be related to?

1

There are 1 answers

1
Levi Cole On

I usually handle this by passing the content type in the headers parameter for wp_mail()

For example...

$to = '[email protected]';
$subject = 'HTML Test';
$body = '<html>...</html>';
$headers = [
  'Content-Type' => 'text/html; charset=UTF-8'
];

wp_mail( $to, $subject, $body, $headers );

More info here: https://developer.wordpress.org/reference/functions/wp_mail/

Hope this answers your question.