【问题标题】:Send html and plain text email simultaneously with PHP Mailer使用 PHP Mailer 同时发送 html 和纯文本电子邮件
【发布时间】:2014-03-19 13:20:18
【问题描述】:

我想在电子邮件中同时发送 html 和纯文本。我找到了this pretty straightforward tutorial,但它使用了 mail() 函数来发送电子邮件。 如何转换此代码发送带有 PHP Mailer 类的电子邮件?

//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
mail('', $subject, $message, $headers);

【问题讨论】:

    标签: php


    【解决方案1】:

    见下文:

    $mail = new PHPMailer();
    
    $mail->IsHTML(true);
    $mail->CharSet = "text/html; charset=UTF-8;";
    $mail->IsSMTP();
    
    $mail->WordWrap = 80;
    $mail->Host = "smtp.thehost.com"; 
    $mail->SMTPAuth = false;
    
    $mail->From = $from;
    $mail->FromName = $from; // First name, last name
    $mail->AddAddress($to, "First name last name");
    #$mail->AddReplyTo("reply@thehost.com", "Reply to address");
    
    $mail->Subject =  $subject;
    $mail->Body =  $htmlMessage; 
    $mail->AltBody  =  $textMessage;    # This automatically sets the email to multipart/alternative. This body can be read by mail clients that do not have HTML email capability such as mutt.
    
    if(!$mail->Send())
    {
      throw new Exception("Mailer Error: " . $mail->ErrorInfo);
    }
    

    感谢http://snippets.aktagon.com/snippets/129-how-to-send-both-html-and-text-emails-with-php-and-phpmailer 和 Google。 SMTP 的使用对您来说可能是可选的。

    【讨论】:

    • 太棒了!我用谷歌搜索了这个,但找不到任何关于 PHP Mailers AltBody 属性的信息。就像一个魅力,我的电子邮件刚刚收到 9.9 而不是 mail-tester.com 上的 8.3!
    • 我需要注意什么,纯文本正文看起来应该是这样吗?我的换行符总是被删除...$message['plaintext'] = $PHPMailer->html2text($message['html']);
    • 试试$PHPMailer->html2text(nl2br($message['html']))
    • 效果很好!我现在得分 9/10 而不是 7.9 。 :) 谢谢!
    【解决方案2】:

    PHPMailer 中有一个函数:msgHTML()

    # Minimum code to send an email:
    $phpmailer = new PHPMailer();
    
    $phpmailer->From = $from;
    $phpmailer->FromName = $from_name;
    
    $phpmailer->AddAddress( $to, $to_name );
    
    $phpmailer->Subject = $subject;
    
    $phpmailer->CharSet = 'UTF-8';
    
    $phpmailer->msgHTML( $htmlMessage ); # <- right here!
    
    # Use PHP's mail() instead of SMTP:
    $phpmailer->IsMail();
    
    $phpmailer->Send();
    

    从 HTML 字符串创建消息。 自动对内联图像和背景进行修改 并通过转换 HTML 创建纯文本版本。 覆盖 $this->Body 和 $this->AltBody

    中的任何现有值

    在 Github 上查看完整功能的代码: https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php#L3299

    【讨论】:

      【解决方案3】:

      试试这个:

      $mail->SMTPDebug = 2;
      

      调试级别

      PHPMailer-&gt;SMTPDebug 属性设置为这些数字或常量(在SMTP 类中定义)会导致不同数量的输出:

      • SMTP::DEBUG_OFF(0):禁用调试(也可以离开这个 完全退出,默认为 0)。
      • SMTP::DEBUG_CLIENT (1):输出客户端发送的消息。
      • SMTP::DEBUG_SERVER (2):作为 1,加上从 服务器(这是最有用的设置)。
      • SMTP::DEBUG_CONNECTION (3):作为 2,加上更多关于 初始连接 - 此级别有助于诊断 STARTTLS 故障。
      • SMTP::DEBUG_LOWLEVEL (4): 为 3,加上更低的级别 信息,非常冗长,不用于调试 SMTP,仅用于 低级问题。

      您不需要使用高于 2 的级别,除非您根本无法连接 - 它只会使输出更冗长且更难以阅读。

      请注意,在您调用 send() 之前,您不会得到任何输出,因为在您这样做之前不会进行任何 SMTP 对话。

      链接https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-28
        • 1970-01-01
        • 1970-01-01
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-25
        相关资源
        最近更新 更多