3

Okay, I've got the following issue. I'm trying to send a mail in HTML format. I made a class that returns an HTML string, and that works great.

When I pass that html-mail as $message in my function, works also. But it will not send as html, but plain text.

Now I've tried the following things:

1) $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail($to, $subject, $message, $headers);

2) $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

3) $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

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

What else could it be?

1

Try this one.

  add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

    $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);

    // Reset content-type to avoid conflicts -- https://core.trac.wordpress.org/ticket/23578
    remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

    function wpdocs_set_html_mail_content_type() {
        return 'text/html';
    }
  • Already tried that one ;) But thanks for your answer. I use the standard PHP mail function now, and that does work... – Refilon Jul 5 '16 at 14:14
  • Using this filter always works for me. I don't need the extra Content-Type header. – Andy Macaulay-Brook Jul 21 '16 at 10:45
0

I personally wouldn't use the wp_mail-function, nor the php mail-function. Both of them don't give you much control over your email. If you want to send HTML emails it's a good idea to send a plain text mail with it, so that users that are unable to read HTML emails can read your message too.

Have a look on phpmailer that's a very good email sending class which gives you a lot of control over the email. You can add plain text to an HTML email, set a sender name instead of just having your email address and so on.

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.