PHPMailer Body被截断

时间:2013-05-14 11:22:48

标签: phpmailer

我在使用PHPMailer时遇到了一些奇怪的问题。我正在尝试发送一些我用PHP和纯文本生成的内容,但是正文被截断了。更奇怪的是,这只发生在我生成的电子邮件中,如果我在其中放入一些更长的通用内容,它会被正确发送。我还必须提一下,我确实回应了$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代码非常长,在添加一些换行符之后,完整的内容就完成了。

1 个答案:

答案 0 :(得分:1)

此外,这可能没有关系,但你使用的是Body而不是MsgHTML

$mail->Body = $content;

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

我对此很新,但是从我读过的PHPMailer中使用HTML你应该使用

$mail->MsgHTML = $content;

虽然您的文字看起来很喜欢它显示正常,但您解决了问题。但我以为我会分享它有帮助。

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

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