PHP发送带附件的电子邮件

3

我似乎找不到这个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";
}

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


1
你没有说出哪里出了问题。 - Pekka
包括整个输出,包括头部(减去base64编码的文件) - Artefacto
4个回答

11

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

--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 谢谢,但也许我的努力是徒劳的。尽管我找到了正确的解决方案,但看起来同样的解决方案是独立得到的 :-( - Mike

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 ? "邮件已发送" : "邮件发送失败"; }

3
除非您是为了了解 MIME 邮件的内部工作原理而这样做,否则标准答案是使用类似 PHPMailerSwiftmailer 的邮件库,它们可以轻松处理附件。

有关如何附加文件的 SwiftMailer 示例在 此处


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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接