简体   繁体   English

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

[英]HTML and plain text in same email

I'm building a PHP based newsletter application. 我正在构建一个基于PHP的新闻通讯应用程序。 I know it is a good practice to send plain text version of the email beside the html as well. 我知道,在html旁边发送电子邮件的纯文本版本也是一种好习惯。 My question is how can I do this in practice? 我的问题是在实践中该怎么做? Just simply put one under the other? 只是简单地将一个放在另一个下面? like: 喜欢:

<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

Won't these two interfere with each other? 这两个会不会互相干扰? I mean if the mailer client can understand HTML, then the plain text with nearly the same content would be confusing at the end of the mail. 我的意思是,如果邮件客户端可以理解HTML,则内容几乎相同的纯文本会在邮件末尾造成混乱。 Or if the client can't parse html then the user will see bothering raw HTML source before the human friendly plain text. 或者,如果客户端无法解析html,则用户将在人类友好的纯文本之前看到原始的HTML源代码。 Is there any way hide the useless one depending on the situation? 有什么方法可以根据情况隐藏无用的方法吗? I'm going to use PHPMailer if it is important. 如果重要的话,我将使用PHPMailer。

PHPMailer is a great class to use because it detects when the email client doesn't support HTML. 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;
}

By using the $mail->AltBody, you can send plain text emails. 通过使用$ mail-> AltBody,您可以发送纯文本电子邮件。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM