我正在尝试使用内置的PHP邮件功能来发送包含html和我的邮件的纯文本版本的多部分邮件。我一直在玩不同的编码类型但是,我一直遇到问题。最初我将Content-Transfer-Encoding
设置为Binary
但是,这导致每78个字符放置一个感叹号。我也试过base64
,但我相信base64对我正在做的事情来说太过分了。
我所做的就是发送基本的HTML,没有编码的图像,文件或附件。我更喜欢一种编码方法,它仍然允许源代码是人类可读的。
我听说Quoted-Printable
正是我正在寻找的,但是当我尝试使用该编码类型发送消息时,结果看起来非常奇怪。我注意到消息源代码中散布着一堆"
个符号。
以下是我正在使用的代码:
$to = "to@test.com";
$subject = "test subject";
$boundary = uniqid('np');
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: from-address@test.com\r\n";
$headers .= "Reply-To: reply-address@test.com\r\n";
$headers .= "Return-Path: return-path@test.com\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $HTMLmessage;
$message .= "\r\n\r\n--" . $boundary . "--";
$ok = mail($to,$subject,$message,$headers);
我到底做错了什么?
答案 0 :(得分:0)
不需要第三方库或外部邮件转发主机。这就是我所使用的,它就像一种魅力。它还通过使用 $ from 地址强行标头来修复某些潜在的安全漏洞,这些标头可能会泄露您的系统用户:
private function sendMail($to, $from, $fromName, $subject, $text, $html)
{
$boundary = uniqid('np');
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: ".$fromName." <".$from.">" . "\r\n";
$headers .= "X-Sender: ".$fromName." <".$from.">\n";
$headers .= "Return-Path: <".$from.">\n";
$headers .= "To: ".$to."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
// Content body
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
// Plain text body
$message .= $text;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
// Html body
$message .= $html;
$message .= "\r\n\r\n--" . $boundary . "--";
// Send mail
mail('', $subject, $message, $headers, '-f ' . $from);
}
答案 1 :(得分:-1)
您好,请尝试以下代码,
$to = "to@test.com";
$subject = "test subject";
$plainTextMessage = "Hi all";
$HTMLmessage = "<b>Hi all</b>";
//$boundary = uniqid('np');
$boundary = md5(uniqid(time()));
$headers .= "From: from-address@test.com\r\n";
$headers .= "Reply-To: reply-address@test.com\r\n";
$headers .= "Return-Path: return-path@test.com\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/plain; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $plainTextMessage;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-Type: text/html; charset=UTF-8\r\n";
$message .= "Content-Transfer-Encoding: Quoted-Printable\r\n";
$message .= $HTMLmessage;
$message .= "\r\n\r\n--" . $boundary . "--";
$ok = mail($to,$subject,$message,$headers);
愿它有所帮助