PHP HTML电子邮件结果中的随机感叹点


Exclamation Point Randomly In Result of PHP HTML-Email

我在这个PHP电子邮件函数的结果中得到了随机的感叹号。我读到这是因为我的行太长,或者我必须用Base64编码电子邮件,但我不知道如何做到这一点。

这就是我所拥有的:

$to = "you@you.you";
$subject = "Pulling Hair Out";
$from = "me@me.me";
$headers = "From:" . $from;
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
$headers .= "Content-Transfer-Encoding: 64bit'r'n";
mail($to,$subject,$message,$headers); 

我该如何解决这个问题,这样就不会有随机性了!结果呢?谢谢

如前所述:HTML电子邮件中的感叹点

问题是你的字符串太长了。将长度超过78个字符的HTML字符串输入到mail函数,您将得到一个!(砰)的一声。

这是由于RFC2822中的线路长度限制https://www.rfc-editor.org/rfc/rfc2822#section-2.1.1

尝试使用以下代码:

$to = "you@you.you";
$subject = "Pulling Hair Out";
$from = "me@me.me";
$headers = "From:" . $from;
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
$headers .= "Content-Transfer-Encoding: 64bit'r'n";
$finalMessage = wordwrap( $message, 75, "'n" );
mail($to,$subject,$finalMessage,$headers);

问题是一行的长度不应超过998个字符。(另请参阅https://stackoverflow.com/a/12840338/2136148)

你是对的,那是因为你的电子邮件太长了。请尝试在邮件头中替换为此行。

Content-Transfer-Encoding: quoted-printable

这里的答案有关于行长度的正确信息,但没有一个答案为我提供足够的代码片段来解决这个问题。我环顾四周,找到了最好的方法,就在这里;

<?php
// send base64 encoded email to allow large strings that will not get broken up
// ==============================================
$eol = "'r'n";
// a random hash will be necessary to send mixed content
$separator = md5(time());
$headers  = "MIME-Version: 1.0".$eol;
$headers .= "From: Me <info@example.com>".$eol;
$headers .= "Content-Type: multipart/alternative; boundary='"$separator'"".$eol;
$headers .= "--$separator".$eol;
$headers .= "Content-Type: text/html; charset=utf-8".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol.$eol;
// message body
$body = rtrim(chunk_split(base64_encode($html)));
mail($email, $subject, $body, $headers);
// ==============================================