【问题标题】:HTML and plain text in same email同一封电子邮件中的 HTML 和纯文本
【发布时间】:2015-05-13 05:39:18
【问题描述】:

我正在构建一个基于 PHP 的时事通讯应用程序。我知道在 html 旁边发送纯文本版本的电子邮件也是一个好习惯。我的问题是我如何在实践中做到这一点?只是简单地把一个放在另一个下面?喜欢:

<p style="font-weight:bold;">This is a newsletter!</p>
<p style="color:red;">
   You can read awesome things here!
   <br/>
   <a href="www.the-awesome-site.com">check out us on the web!</a>
</p>

This is a newsletter\r\n
You can read awesome things here!\r\n
check out us on the web: www.the-awesome-site.com

这两个不会互相干扰吗?我的意思是,如果邮件客户端可以理解 HTML,那么内容几乎相同的纯文本在邮件末尾会令人困惑。或者,如果客户端无法解析 html,那么用户将在人类友好的纯文本之前看到令人烦恼的原始 HTML 源。有什么方法可以根据情况隐藏无用的吗?如果很重要,我将使用 PHPMailer。

【问题讨论】:

    标签: php email html-email


    【解决方案1】:

    PHPMailer 是一个很好用的类,因为它可以检测电子邮件客户端何时不支持 HTML。

    Check out this code snippet. It should help a little.
    
    require("PHPMailer-master/PHPMailerAutoload.php");
    
    $mail = new PHPMailer();
    $mail->IsSMTP();
    
    $mail->Host = "mail.somemailserver.com";  
    $mail->SMTPAuth = false;
    
    
    $mail->From = $someemail;
    $mail->FromName = "Who ever";
    $mail->AddAddress($email);
    $mail->AddCC($anotheremail);
    
    $mail->WordWrap = 50;
    
    
    
    $mail->IsHTML(true);
    
    $mail->Subject = "Some subject";
    
    $mail->Body    = "<html>Content goes here</html>";
    
    
    //if the client doesn't support html email use
    $mail->AltBody = "Content";
    
    if(!$mail->Send())
    {
       echo "Message could not be sent. <p>";
       echo "Mailer Error: " . $mail->ErrorInfo;
       exit;
    }
    

    通过使用 $mail->AltBody,您可以发送纯文本电子邮件。希望这会有所帮助!

    【讨论】:

    • 不错,我用的是同款
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 2012-08-04
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    相关资源
    最近更新 更多