Using WordPress to send html mails with the smtp plugin

I want to send html mails to my users when they are going to sign up in my site and I have written these codes in the function “wp_new_user_notification”.

$message = '<html><body>';
$message .= '<img src="//liux.so/mail/pic.jpg" alt="rn" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>username:</strong> </td><td>" . strip_tags($user->user_login) . "</td></tr>";
$message .= "<tr><td><strong>password:</strong> </td><td>" . strip_tags($plaintext_pass) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";

After sending a test mail to my own mailbox, I was surprised to see that the whole mail is full of html codes! And then I search this issue on Google. It says that I should add “header” in front of “message”. Just like…

Read More
mail($to, $subject, $message, $headers);

So the question is over here! I’ve installed a plugin which allows me to send mails through the smtp service. And this plugin should write the code like this…

wp_mail($user->user_email, sprintf(__('[%s] Welcome!”'), $blogname), $message);

I cannot figure out how to put the “header” into “wp_mail” function or just make the html codes work…

Related posts

Leave a Reply

2 comments

  1. @shahrukh’s answer is absolutely correct. You should set the content type to html, this will solve your problem. But it might have some conflicts with other plugins or with default wordpress functionality. So what you should do is to add the filter before sending the mail, and remove the filter after its been sent.

    add_filter( 'wp_mail_content_type', 'set_html_content_type' );
    
    wp_mail( 'To Email', 'Subject', '<h1>Html Email</h1>' );     
    
    remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
    
    function set_html_content_type() {
        return 'text/html';
    }
    
  2. add filter to send html email

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

    after this you will be able to send html formated emails.
    this code must be in your functions.php or any plugin