PHP 邮件标头
本质上我想做的是将文件附加到我发送的电子邮件中。很简单,对吧?由于某种原因,它不喜欢以下代码(大概是因为标头)。有人可以帮忙吗?
提前致谢!!
$subject = "File ".date("Ymd");
$message = "NONE";
$filename = "test.csv";
$content = chunk_split(base64_encode(file_get_contents($filename)));
$uid = md5(uniqid(time()));
$name = basename($file);
$header .= "MIME-Version: 1.0\r\n";
$header .= "From: [email protected]\r\n";
$header .= "Reply-To: [email protected]\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/csv; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."\r\n";
//echo $header;
if (mail($to_email, $subject, $message, $header)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR!";
}
错误:
警告: mail() [function.mail]: mail() 函数的参数错误,邮件未发送。
Essentially what I'm trying to do is attach a file to an email I'm sending out. Simple enough, right? For some reason or another it does not like the following code (presumably because of the headers). Can anyone help?
Thanks in advance!!
$subject = "File ".date("Ymd");
$message = "NONE";
$filename = "test.csv";
$content = chunk_split(base64_encode(file_get_contents($filename)));
$uid = md5(uniqid(time()));
$name = basename($file);
$header .= "MIME-Version: 1.0\r\n";
$header .= "From: [email protected]\r\n";
$header .= "Reply-To: [email protected]\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: text/csv; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."\r\n";
//echo $header;
if (mail($to_email, $subject, $message, $header)) {
echo "mail send ... OK";
} else {
echo "mail send ... ERROR!";
}
And the error:
Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请不要建立您自己的 MIME 电子邮件。使用 PHPMailer 或 Swiftmailer,它几乎可以为您做所有事情。您可以用大约 5 或 6 行代码替换整个脚本。
最重要的是,它们将为您提供比愚蠢的
mail()
函数更好的错误消息/诊断。Please please please don't build your own MIME emails. Use PHPMailer or Swiftmailer, which do almost everything for you. You can replace you entire script with about 5 or 6 lines of code.
And best of all, they'll give you far better error messages/diagnostics than the pathetically stupid
mail()
function ever will.如果您坚持构建自己的标头,我建议您在输出缓冲区的帮助下这样做 - 我还注意到您未能关闭内容边界。下面粘贴的是我如何编辑脚本的标头生成部分。
If you insist on building your own header, I would suggest doing so with the aid of your output buffer - also I noticed that you were failing to close up your content boundaries. Pasted below is how I would edit the header generating part of your script.
Geekmail PHP 库可以轻松地向电子邮件添加附件(以及一般发送电子邮件):
The Geekmail PHP library makes it easy to add attachments to emails (and to send emails in general):
您似乎没有填充目标地址(在代码示例中),并且您的消息位于标头(肯定比标头延伸得更远)和正文中......
You don't seem to populate destination address (in the code sample) and you have your message both in headers (that definitely extends further than headers) and in body…