【问题标题】:PHPMailer Body gets truncatedPHPMailer 正文被截断
【发布时间】:2013-05-14 11:22:47
【问题描述】:

我在使用 PHPMailer 时遇到了一些奇怪的问题。我正在尝试发送一些使用 PHP 生成的 HTML 和纯文本内容,但正文被截断。更奇怪的是,这只会发生在我生成的电子邮件中,如果我在其中放入一些更长的通用内容,它就会被正确发送。我还必须提到,我确实回显了 $content$nohtmlcontent 变量的内容,一切都应该是这样,但是当我在邮箱中收到电子邮件时,它被截断了。

我用于创建纯文本和 HTML 电子邮件正文的 PHP 代码:

$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";

所有变量都填充了适当的数据。

我用于发送电子邮件的 PHPMailer 代码:

require_once("class.phpmailer.php");
$mail=new PHPMailer(true);
try {
    $mail->AddAddress($email);
    $mail->SetFrom('admin@example.com', '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: info@tourazore.com
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: info@tourazore.com
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 代码生成了很长的一行,添加几个换行符后,完整的内容就打通了。

【问题讨论】:

    标签: phpmailer


    【解决方案1】:

    这也可能不相关,但您使用的是 Body 而不是 MsgHTML

    $mail->Body = $content;
    

    但看起来您在内容中使用了 HTML 表达式。

    我对此很陌生,但从我读过的内容来看,在 PHPMailer 中使用 HTML 你应该使用

    $mail->MsgHTML = $content;
    

    虽然您的文字看起来很好,但您解决了您的问题。但我想我会分享它以防万一。

    这里有一些有用的信息 https://phpbestpractices.org/ (向下滚动到电子邮件信息)

    这里: https://github.com/PHPMailer/PHPMailer

    【讨论】:

    • 我从来没有注意到有一个$mail-&gt;MsgHTML 可用,在所有示例(以及我看到的 SO 问题/答案)我总是注意到$mail-&gt;Body,但必须事先提供$mail-&gt;IsHTML(true),否则 PHPMailer 假定内容是纯文本并且发送了错误的Content-Type
    猜你喜欢
    • 2016-05-24
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2021-12-28
    • 2016-07-20
    • 2016-01-10
    相关资源
    最近更新 更多