发送包含HTML和纯文本部分的电子邮件

时间:2015-11-29 作者:user3290060

我使用SMTP邮递员插件从wordpress发送电子邮件。我在函数中创建了一个函数。创建HTML注册电子邮件的php文件,如下所示:

/* 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>\';

}
然而,许多电子邮件提供商检查HTML和纯文本版本的电子邮件内容。我的电子邮件目前仅以HTML格式发送。我当前的设置是否有办法发送HTML和纯文本版本?

1 个回复
SO网友:jgraup

看看你是否能phpmailer_init 和设置AltBody.

add_action(\'phpmailer_init\', \'add_plain_text\');

function add_plain_text($phpmailer)
{
    $phpmailer->AltBody = "This is a text message";
}
奖励:通过HTMLpremailer.dialect.ca 要呈现可用的文本版本并查找有关多部分电子邮件的更多信息,请访问Things I\'ve Learned About Building & Coding HTML Email Templates.

Update:

Postman的测试电子邮件是一个包含文本和html部分的邮件示例。它的内容类型是“multipart/alternative”,这是一个MIME扩展。

根据这个support question 在…上Postman SMTP Mailer 答案就在邮递员的测试邮件和网站上example. 只需确保更换mail 具有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);

相关推荐