gpt4 book ai didi

php - 邮件正文中的 MIME 信息与 PHP

转载 作者:可可西里 更新时间:2023-10-31 23:32:38 27 4
gpt4 key购买 nike

我一直在尝试使用 PHP Mail 函数发送带有 HTML 正文和附件的电子邮件。没有附加文件,我可以毫无问题地收到我的 HTML 电子邮件,但是当我尝试附加文件时,我在正文中得到了所有 MIME 信息——还有附件,已编码。

这是没有附件的电子邮件功能的代码 - 工作正常:

        $this->to = $to;
$this->subject = $subject;
$this->message = $message;
$this->headers = "From: " . Mailer::FROM_EMAIL . "\r\n";
$this->headers .= "MIME-Version: 1.0\r\n";
$this->headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

这是带有附件的电子邮件版本的代码:

        $this->to = $to;
$this->subject = $subject;
$this->attachment =
chunk_split(base64_encode(file_get_contents($attachment)));

//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$boundary = md5(date('r', time()));
$this->headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com\r\n";
$this->headers .= "MIME-Version: 1.0\r\n ";
$this->headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-$boundary\"\r\n";

$this->message =
"--PHP-mixed-$boundary
Content-Type: text/html; charset=\"ISO-8859-1\"

" . $message . "


--PHP-mixed-$boundary
Content-Type: application/pdf; name=\"test.pdf\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

".$this->attachment ."
--PHP-mixed-$boundary--";

我正在使用此功能发送电子邮件:

public function send(){

if (preg_match(Mailer::PATTERN, trim(strip_tags($this->to)))) {
$cleanedTo = trim(strip_tags($this->to));
} else {
return FALSE;
}
return mail ($cleanedTo, $this->subject, $this->message, $this->headers);
}

然后我按如下方式创建 Mailer 对象:

$mailer = new Mailer("youremail@gmail.com", "test mail", "Some <b>old good</b> HTML email", 'pdf/test.pdf');
//$mailer = new Mailer("youremail@gmail.com", "test mail", "Some <b>old good</b> HTML email");

$mailer->send();

我收到的邮件是:

--PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1
Content-Type: text/html; charset="ISO-8859-1"

Some <b>old good</b> HTML email


--PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1
Content-Type: application/pdf; name="test.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

JVBERi0xLjMKMSAwIG9iago8 ... FT0YK

--PHP-mixed-e37929b72bbc6f8e3b37cf802619aac1--

我想我真的很接近答案,但是......我需要你的帮助才能找到它。

最佳答案

$this->headers .= "MIME-Version: 1.0\r\n ";
$this->headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-$boundary\"\r\n";

删除 MIME-Version 行中换行符后的空格。 Content-Type 前面的这个尾随空格将使它成为前一行的延续。

顺便说一句:如果您的代码在 Linux/Unix 上运行,请仅在每行末尾使用“\n”。

关于php - 邮件正文中的 MIME 信息与 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14022271/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com