PHP 邮件标头

发布于 2024-10-31 19:01:23 字数 1510 浏览 1 评论 0原文

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

提前致谢!!

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

守不住的情 2024-11-07 19:01:23

请不要建立您自己的 MIME 电子邮件。使用 PHPMailerSwiftmailer,它几乎可以为您做所有事情。您可以用大约 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.

因为看清所以看轻 2024-11-07 19:01:23

如果您坚持构建自己的标头,我建议您在输出缓冲区的帮助下这样做 - 我还注意到您未能关闭内容边界。下面粘贴的是我如何编辑脚本的标头生成部分。

ob_start();

?>
MIME-Version: 1.0
From: [email protected]
Reply-To: [email protected]
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());

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.

ob_start();

?>
MIME-Version: 1.0
From: [email protected]
Reply-To: [email protected]
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());
第七度阳光i 2024-11-07 19:01:23

Geekmail PHP 库可以轻松地向电子邮件添加附件(以及一般发送电子邮件):

$geekMail = new geekMail();
$geekMail->setMailType('text');
$geekMail->from("[email protected]");
$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();
}

The Geekmail PHP library makes it easy to add attachments to emails (and to send emails in general):

$geekMail = new geekMail();
$geekMail->setMailType('text');
$geekMail->from("[email protected]");
$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();
}
血之狂魔 2024-11-07 19:01:23

您似乎没有填充目标地址(在代码示例中),并且您的消息位于标头(肯定比标头延伸得更远)和正文中......

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…

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文