【问题标题】:PHP Mail HeadersPHP 邮件头
【发布时间】:2011-08-03 08:25:35
【问题描述】:

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

提前致谢!

$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() 函数的参数错误,邮件未发送。

【问题讨论】:

  • $to_email 包含什么内容?

标签: php email email-headers


【解决方案1】:

请不要建立自己的 MIME 电子邮件。使用PHPMailerSwiftmailer,它们几乎可以为您做所有事情。您可以用大约 5 或 6 行代码替换整个脚本。

最重要的是,它们会为您提供比可悲愚蠢的mail() 函数更好的错误消息/诊断。

【讨论】:

  • 好电话。我试图避免使用库,但是一旦我将一个库弹出到位,它就起作用了。谢谢!
【解决方案2】:

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

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());

【讨论】:

    【解决方案3】:

    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();
    }
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2014-07-19
        • 2013-06-29
        • 1970-01-01
        • 1970-01-01
        • 2017-04-30
        • 1970-01-01
        • 1970-01-01
        • 2011-01-02
        • 2013-04-26
        相关资源
        最近更新 更多