0

我在使用 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()来发送相同的内容,它也会被截断。

有任何想法吗?

SOLUTION: PHP代码生成的行很长,添加几个换行符后,完整的内容就通过了。

4

1 回答 1

1

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

$mail->Body = $content;

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

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

$mail->MsgHTML = $content;

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

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

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

于 2014-04-02T18:58:00.147 回答