Send an email that contains a HTML and plain text part

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" /
  titleWelcome 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?

Topic html-email Wordpress

Category Web


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 protected]';
$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);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.