基本上我要做的是将文件附加到我发送的电子邮件中。很简单吧?由于某种原因,它不喜欢以下代码(可能是因为标题)。有人可以帮忙吗?
提前致谢!!
$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()函数参数错误,邮件未发送。
答案 0 :(得分:7)
请不要构建自己的MIME电子邮件。使用PHPMailer或Swiftmailer,几乎可以为您完成所有操作。您可以用大约5或6行代码替换整个脚本。
最重要的是,它们会给你提供比可疑的mail()
功能更好的错误消息/诊断。
答案 1 :(得分:1)
如果您坚持构建自己的标题,我建议您在输出缓冲区的帮助下这样做 - 我也注意到您未能关闭内容边界。下面粘贴的是我如何编辑脚本生成部分的标题。
ob_start();
?>
MIME-Version: 1.0
From: noreply@x.com
Reply-To: noreply@x.com
Content-Type: multipart/mixed; boundary="<?php echo $uid; ?>"
This is a multi-part message in MIME format.
--<?php echo $uid; ?>
Content-Type:text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit
<?php echo $message; ?>
--<?php echo $uid; ?>--
--<?php echo $uid; ?>
Content-Type: text/csv; name="<?php echo $filename; ?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="<?php echo $filename; ?>"
<?php echo $content; ?>
--<?php echo $uid; ?>--
<?php
$header = trim(ob_get_clean());
答案 2 :(得分:0)
Geekmail PHP库可以轻松地向电子邮件添加附件(以及发送电子邮件):
$geekMail = new geekMail();
$geekMail->setMailType('text');
$geekMail->from("noreply@x.com");
$geekMail->to($to_email);
$geekMail->subject($subject);
$geekMail->message($message);
$geekMail->attach($filename);
if (!$geekMail->send()){
//an error occurred sending the email
$errors = $geekMail->getDebugger();
}
答案 3 :(得分:0)
您似乎没有填充目标地址(在代码示例中),并且您的消息都在标题中(肯定比标题扩展得更远)和正文...