php发送带附件的电子邮件

时间:2010-06-22 11:59:32

标签: php email attachment

我似乎无法找到我写的这个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";
}

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

4 个答案:

答案 0 :(得分:10)

我刚刚查看了几封电子邮件,并注意到最终的附件边界以' - '结尾,而开放边界标记则没有。在您的代码中,您有:

--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

答案 1 :(得分:6)

Artefacto让我更加关注输出,我找到了修复:

function myMail($to, $subject, $mail_msg, $filename, $contentType, $pathToFilename){
    $random_hash = md5(date('r', time()));
    $headers = "From: webmaster@mysite.com\r\nReply-To: ".$to;
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
    $attachment = chunk_split(base64_encode(file_get_contents($pathToFilename)));
    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: $contentType; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--PHP-mixed-$random_hash--
";
$message = ob_get_clean();
$fh=fopen('log.txt','w');
fwrite($fh,$message);
$mail_sent = @mail( $to, $subject, $message, $headers );
return $mail_sent ? "Mail sent" : "Mail failed";
}

答案 2 :(得分:3)

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

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

答案 3 :(得分:1)

这些是我使用的标题,它们总是像魅力一样。

$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