I am trying to send email via wp_mail() containing both plain text, as well as HTML body.

When I am setting up headers like this:

  $headers = "MIME-Version: 1.0 \r\n";
  //$headers .= "From: Dawid Adach\r\n";
  $headers .= "Reply-To: d.adach@mdbootstrap.com \r\n";
  $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";

  //$headers .= "To: ".$email."\r\n";
  $headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

and later on content:

  //Plain text body
  $message .= "Hello,\nThis is a text email, the text/plain version.
  \n\nRegards,\nYour Name";
  $message .= "\r\n\r\n--" . $boundary . "\r\n";
  $message .= "Content-type: text/html;charset=utf-8\r\n\r\n";

  //Html body
  $message .= '
<!--my html comes here -->
  $message .= "\r\n\r\n--" . $boundary . "--";

It doesn't work. The entire body is treated like a plain HTML. When I check an original message I can see that new line chars aren't interpreted correctly.

Reply-To: d.adach@mdbootstrap.com X-Mailer:PHP/7.0.8Content-Type:
 multipart/alternative@720plan.ovh.net; ,

Second thing is that when I try to set header "From: " wp_mail returns 0 and doesn't send anything.

I was going trough Sending multipart (text/html) emails via wp_mail() will likely get your domain banned , however I cannot overwrite wp_mail function so I have to use original one.

  • "I cannot overwrite wp_mail" why ? explain the special configuration you have if you need help – mmm Aug 29 '16 at 11:05
  • In order to overwrite wp_mail have to put it inside mu-plugin (i.e. "wp-content/mu-plugins/functions.php" ) , as far as I understand I can do it with my local copy, however if I want to provide other users with plugin I cannot overwrite wp_mail. When I am trying to do this from my plugin I am getting error - cannot redeclare wp_mail(). – Dawid Adach Aug 29 '16 at 13:24
  • you can do it in a plugin but only directly in the main file of the plugin, before all actions – mmm Aug 29 '16 at 13:25
  • Redeclaring wp_mail() does not require an mu-plugin. You can do it in any ordinary plugin. The only thing you have to be concerned with is if another plugin attempts to redeclare it as well, but if you format your code correctly (i.e. use if ( ! function_exists() )...), then you'll be fine. – butlerblog Dec 12 '19 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.