PHPMailer正文被截断
问题描述:
我遇到了PHPMailer的一些奇怪问题。我试图发送一些我用PHP和HTML生成的内容,但是正文会被截断。更奇怪的是,这只发生在我生成的电子邮件上,如果我在那里放置一些更长的通用内容,它会被正确发送。我还必须提及,我确实回应了$content
和$nohtmlcontent
变量的内容,并且所有内容都像应该出现的那样,但是当我收到我的邮箱中的电子邮件时,它会被截断。PHPMailer正文被截断
$content="<BODY bgColor=\"#ffffff\"><FONT face=\"Verdana\" size=\"2\">";
$content.="Hello $name.<br /><br />Administrator of <a href=\"http://$url\">$url</a> has created a new account for you.<br /><br />Your new account details:<br />";
$content.=$message."<br /><br />";
$content.="If you see something wrong, please reply with correct details and we will update your account.<br /><br />";
$content.="Have a nice day,<br />$url</FONT></FONT></BODY>";
$nohtmlcontent="Hello $name.\n\nAdministrator of $url has created a new account for you.\n\nYour new account details:\n\n";
$nohtmlcontent.=$usrEmail."\n\n";
$nohtmlcontent.="If you see something wrong, please reply with correct details and we will update your account.\n\n";
$nohtmlcontent.="Have a nice day,\n$url";
所有变量都具有适当的数据填充:创建纯文本和HTML电子邮件正文
我的PHP代码。用于发送电子邮件
我PHPMailer的代码:
require_once("class.phpmailer.php");
$mail=new PHPMailer(true);
try {
$mail->AddAddress($email);
$mail->SetFrom('[email protected]', 'example.com');
$mail->CharSet = 'UTF-8';
$mail->Subject = "New account for you";
$mail->IsHTML(true);
$mail->AltBody = $nohtmlcontent;
$mail->Body = $content;
$mail->Send();
return true;
}catch(phpmailerException $e){
trigger_error("PHPMailer failed: ".$e->errorMessage());
return false;
}
结果:
Hello 12 23.
Administrator of admin.localhost.dev has created a new account for you.
Your new account details:
Username: user1
Password: 123456
E-Mail Address: [email protected]
Subscription Status: Not Verified (you must verify your email address before you can use your account)
Package: Free (limitations: 1 tour, 5 items)
First Name: 12
Last Name: 23
City 34
Country 45
Your verification link: http://admin.localhost.dev/verify-account/882672636ce2ad8c498f75a9b836ff055aecf573/
If you see something wrong, please reply with correct details and we will update you
预期结果:
Hello 12 23.
Administrator of admin.localhost.dev has created a new account for you.
Your new account details:
Username: user1
Password: 123456
E-Mail Address: [email protected]
Subscription Status: Not Verified (you must verify your email address before you can use your account)
Package: Free (limitations: 1 tour, 5 items)
First Name: 12
Last Name: 23
City 34
Country 45
Your verification link: http://admin.localhost.dev/verify-account/882672636ce2ad8c498f75a9b836ff055aecf573/
If you see something wrong, please reply with correct details and we will update your account.
Have a nice day,
admin.localhost.dev
请注意到底是额外的内容。
我也尝试使用PHP的函数mail()
发送相同的内容,它也被截断。
任何想法?
解决方案: PHP代码生成很长的一行,添加了几个换行符后,完整的内容通过。
答
而且这可能不相关,但你用肢体而不是MsgHTML
$mail->Body = $content;
但它看起来使用HTML表现在你的内容你喜欢。
我是相当新的,但这个从我读过的PHPMailer的使用HTML,你应该使用
$mail->MsgHTML = $content;
虽然你的文字看起来很喜欢它显示的是罚款和你解决你的问题。但我认为我会分享它的帮助。
这里一些有用的信息 https://phpbestpractices.org/ (向下滚动到电子邮件信息)
我从来没有注意到,有一个'$ MAIL-> MsgHTML'可用,在所有的例子(和所以我总是注意'$ mail-> Body',但是必须提供'$ mail-> IsHTML(true)',否则PHPMailer会假定内容是纯文本,错误的Content-Type '发送。 – 2014-04-05 12:34:46