PHP邮件头 [英] PHP Mail Headers

查看:157
本文介绍了PHP邮件头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本质上,我想要做的是将文件附加到我发送的电子邮件中。很简单,对吧?由于某种原因,它不喜欢以下代码(大概是因为标题)。任何人都可以帮忙?

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?

提前感谢

$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: noreply@x.com\r\n";
$header .= "Reply-To: noreply@x.com\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()函数的参数不正确,邮件未发送。

Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent.

推荐答案

请不要建立自己的MIME电子邮件。使用 PHPMailer Swiftmailer ,几乎为你做的一切。您可以使用大约5或6行代码替换整个脚本。

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.

最重要的是,他们会给你更好的错误消息/诊断,而不是愚蠢的愚蠢 mail()函数将会。

And best of all, they'll give you far better error messages/diagnostics than the pathetically stupid mail() function ever will.

这篇关于PHP邮件头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆