HTML 和同一封电子邮件中的纯文本

HTML and plain text in same email

我正在构建一个基于 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。

PHPMailer 非常好用 class,因为它会检测电子邮件客户端何时不支持 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,您可以发送纯文本电子邮件。希望这对您有所帮助!