内容类型:WordPress中的multipart / alternative with wp_mail()

时间:2013-12-03 14:09:46

标签: wordpress content-type multipart-alternative

是否可以使用具有Content-Type:multipart / alternative?的wp_mail()函数发送电子邮件?

我需要发送可以显示为HTML或纯文本的电子邮件,具体取决于媒体对电子邮件的解释。

欢迎任何建议!

3 个答案:

答案 0 :(得分:2)

当您有权访问phpmailer实例时,这是完全可能的。

if ($is_html) 
    add_action('phpmailer_init', 'fix_mimeheader');

// more code.

wp_mail( $to, $subject, $html_message, $headers, $attachments );

// ...

function fix_mimeheader( $phpmailer ) {
     // Generate $text_message here.
     // ...

     $phpmailer->AltBody = $text_message;
}

发送给wp_mail的邮件应该是你的html代码。您也不应包含任何内容类型标头。我目前在插件中使用from,cc和reply-to。

如果电子邮件是以HTML格式发送的,我会运行直接在phpmailer对象上设置AltBody属性的操作。然后,这会导致正确的标志将电子邮件转换为多部分/备用电子邮件。

答案 1 :(得分:1)

它就在wp_mail() documentation

  

默认内容类型为“text / plain”,不允许使用HTML。您可以使用“wp_mail_content_type”过滤器(请参阅下面的示例)或通过包含“Content-type:text / html”之类的标题来设置电子邮件的内容类型。 发送消息后,请小心将'wp_mail_content_type'重置为'text / plain',因为如果不这样做可能会导致来自WP或插件/主题的电子邮件出现意外问题。

(强调我的)

页面上的第二个示例显示了如何操作(示例使用text/html,但您应该可以使用multipart/alternative代替。

答案 2 :(得分:1)

您可以使用现在已在食典委中记录的wp_mail_content_type filter

关于将内容类型重置为“text / plain”的wp_mail文档有点误导,IMO。由于这是一个过滤器,你不会真正“重置”它。您需要在过滤器中考虑的是一些条件逻辑,以确定何时需要使用multipart与纯文本或html:

add_filter( 'wp_mail_content_type', 'my_mail_content_type' );
function my_mail_content_type( $content_type ) {

    if( $some_condition ) {
        return 'multipart/mixed';
    } else {
        return 'text/plain';
    }
}
相关问题