【问题标题】:php send e-mail with attachmentphp发送带附件的电子邮件
【发布时间】:2010-06-22 11:59:32
【问题描述】:

我似乎找不到我编写的应该发送带有附件的电子邮件的这个 php 函数的问题。我已经为此苦苦挣扎了一段时间。

function myMail($to, $subject, $mail_msg, $filename, $contentType){

    $random_hash = md5(date('r', time()));
    $headers = "From: webmaster@example.com\r\nReply-To: ".$to;
    $headers .= "\r\nContent-Type: ".$contentType.
        "; boundary=\"PHP-mixed-".$random_hash."\"";

    $attachment = chunk_split(base64_encode(file_get_contents($filename)));
    ob_start();

    echo "
--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"

--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"utf-8\"
Content-Transfer-Encoding: 7bit

$mail_msg

--PHP-alt-$random_hash

--PHP-mixed-$random_hash--
Content-Type: text/plain; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--PHP-mixed-$random_hash--
";
    $message = ob_get_clean();
    $mail_sent = @mail( $to, $subject, $message, $headers );
    return $mail_sent ? "Mail sent" : "Mail failed";
}

编辑问题是邮件的消息与文件混在一起,作为附件发送。

【问题讨论】:

  • 你不是说什么不起作用。
  • 包括整个输出,包括头文件(减去 base64 编码文件)

标签: php email attachment


【解决方案1】:

我刚刚查看了几封电子邮件,发现最后的附件边界以“--”结尾,而开头的边界标记则没有。在您的代码中,您有:

--PHP-mixed-$random_hash--
Content-Type: text/plain; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--PHP-mixed-$random_hash--

也许应该是:

--PHP-mixed-$random_hash
Content-Type: text/plain; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--PHP-mixed-$random_hash--

看看这里的例子:

http://en.wikipedia.org/wiki/MIME#Multipart_messages

【讨论】:

  • @Pekka 谢谢,但也许我的努力是徒劳的。尽管得出了正确的解决方案,但看起来好像是独立得出了相同的解决方案:-(
【解决方案2】:

Artefacto 让我更加关注输出结果,我找到了解决方法:

功能 myMail($to, $subject, $mail_msg, $filename, $contentType, $pathToFilename){ $random_hash = md5(date('r', time())); $headers = "发件人:webmaster@mysite.com\r\n回复:".$to; $headers .= "\r\nContent-Type: multipart/mixed; 边界=\"PHP-mixed-".$random_hash."\""; $attachment = chunk_split(base64_encode(file_get_contents($pathToFilename))); ob_start(); 回声“ --PHP-mixed-$random_hash 内容类型:多部分/替代;边界=\"PHP-alt-$random_hash\" --PHP-alt-$random_hash 内容类型:文本/纯文本;字符集=\“utf-8\” 内容传输编码:7bit $mail_msg --PHP-alt-$random_hash-- --PHP-mixed-$random_hash 内容类型:$contentType;名称=\"$文件名\" 内容传输编码:base64 内容处置:附件 $附件 --PHP-mixed-$random_hash-- "; $message = ob_get_clean(); $fh=fopen('log.txt','w'); fwrite($fh,$message); $mail_sent = @mail($to, $subject, $message, $headers); 返回 $mail_sent ? "邮件已发送" : "邮件失败"; }

【讨论】:

    【解决方案3】:

    除非您这样做是为了了解 MIME 邮件的内部工作原理,否则标准答案是使用像 PHPMailerSwiftmailer 这样可以开箱即用地处理附件的邮件程序库。

    有关如何附加文件的 SwiftMailer 示例是 here

    【讨论】:

      【解决方案4】:

      这些是我使用的标头,它们一直都很有魅力。

      $base = basename($_FILES['upload']['name']);
      $file = fopen($randname_path,'rb');
      $size = filesize($randname_path);
      $data = fread($file,$size);
      fclose($file);
      $data = chunk_split(base64_encode($data));
      
      //boundary
      $div = "==Multipart_Boundary_x".md5(time())."x";
      //headers
      $head = "From: $from\n".
              "MIME-Version: 1.0\n".
              "Content-Type: multipart/mixed;\n".
              " boundary=\"$div\"";
      //message
      $mess = "--$div\n".
              "Content-Type: text/plain; charset=\"iso-8859-1\"\n".
              "Content-Transfer-Encoding: 7bit\n\n".
              "$message\n\n".
              "--$div\n".
              "Content-Type: application/octet-stream; name=\"$base\"\n".
              "Content-Description: $base\n".
              "Content-Disposition: attachment;\n".
              " filename=\"$base\"; size=$size;\n".
              "Content-Transfer-Encoding: base64\n\n".
              "$data\n\n".
              "--$div\n";
      $return = "-f$from";
      

      http://asdlog.com/Create_form_to_send_email_with_attachment

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-15
        • 2020-11-20
        • 2014-02-28
        • 2012-01-15
        • 2013-08-26
        • 2017-09-06
        • 2015-08-28
        相关资源
        最近更新 更多