3

I am sending emails from my wordpress using SMTP Postman plugin. I have created a function in my functions.php file that creates a HTML registration email as per the below:

/* Custom user registration email */
function set_bp_message_content_type() {
    return 'text/html';
}

add_filter( 'bp_core_signup_send_validation_email_message', 'custom_buddypress_activation_message', 10, 3 );

function custom_buddypress_activation_message( $message, $user_id, $activate_url ) {
    add_filter( 'wp_mail_content_type', 'set_bp_message_content_type' );
    $user = get_userdata( $user_id );
    return '

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Welcome to My Site</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body style="margin: 0; padding: 0;">
</body>
</html>';

}

However, many email providers check email content for both an HTML and a plain text version. My emails currently send in HTML only. Is there a way with my current setup to send a HTML and a Plain text version?

1

Check to see if you can hook into phpmailer_init and set AltBody.

add_action('phpmailer_init', 'add_plain_text');

function add_plain_text($phpmailer)
{
    $phpmailer->AltBody = "This is a text message";
}

Bonus: pass your HTML through premailer.dialect.ca to render a usable text version and find more info on multipart email at Things I've Learned About Building & Coding HTML Email Templates.

Update:

Postman's test email is an example of a message that has both text and html parts. It's content type is "multipart/alternative" which is a MIME extension.

According to this support question on Postman SMTP Mailer the answer lies in the Postman's test email and sites this example. Just be sure to replace mail with wp_mail.

//specify the email address you are sending to, and the email subject
$email = 'email@example.com';
$subject = 'Email Subject';

//create a boundary for the email. This 
$boundary = uniqid('np');

//headers - specify your from email address and name here
//and specify the boundary for the email
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Your Name \r\n";
$headers .= "To: ".$email."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";

//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";

//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 .= "
 Hello,
This is a text email, the html version.

Regards,
Your Name";
$message .= "\r\n\r\n--" . $boundary . "--";

//invoke the PHP mail function
wp_mail('', $subject, $message, $headers);
  • Got an example i could use with my above code ? Im having difficulties with this. – user3290060 Dec 2 '15 at 17:12
  • The answer has been updated to include links to a previously asked question on their support forum. – jgraup Dec 2 '15 at 18:01
  • Maybe mail() works with an empty email address, but for me wp_mail() does not, so I used: wp_mail($email, $subject, $message, $headers); – Zade Jan 15 at 8:18
  • Also I couldn't get Gmail to display the message properly without adding the following header: Content-Transfer-Encoding: 7bit – Zade Jan 15 at 9:11

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.