php发送带附件的电子邮件 [英] php send e-mail with attachment

查看:39
本文介绍了php发送带附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

function myMail($to, $subject, $mail_msg, $filename, $contentType){$random_hash = md5(date('r', time()));$headers = "From: webmaster@example.com
Reply-To: ".$to;$headers .= "
Content-Type: ".$contentType.";boundary="PHP-mixed-".$random_hash.""";$attachment = chunk_split(base64_encode(file_get_contents($filename)));ob_start();回声"--PHP-mixed-$random_hash内容类型:多部分/替代;边界="PHP-alt-$random_hash"--PHP-alt-$random_hash内容类型:文本/纯文本;字符集="utf-8"内容传输编码:7 位$mail_msg--PHP-alt-$random_hash--PHP-mixed-$random_hash--内容类型:文本/纯文本;名称="$文件名"内容传输编码:base64内容处理:附件$附件--PHP-mixed-$random_hash--";$message = ob_get_clean();$mail_sent = @mail( $to, $subject, $message, $headers );返回 $mail_sent ?"邮件发送" : "邮件失败";}

编辑 问题是邮件的信息与文件混合并作为附件发送.

解决方案

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

<前>函数 myMail($to, $subject, $mail_msg, $filename, $contentType, $pathToFilename){$random_hash = md5(date('r', time()));$headers = "发件人:webmaster@mysite.com Reply-To: ".$to;$headers .= " Content-Type: multipart/mixed; boundary="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"内容传输编码:7 位$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 ?"邮件发送" : "邮件失败";}

I can't seem to find the problem with this php function i wrote that should send an e-mail with attachment. I've been struggling with it for quite a while.

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

    $random_hash = md5(date('r', time()));
    $headers = "From: webmaster@example.com
Reply-To: ".$to;
    $headers .= "
Content-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";
}

Edit The problem is that the message of the mail is mixed with the file and send as an attachment.

解决方案

Artefacto made me look at the output with more attention and i've found the fix:

function myMail($to, $subject, $mail_msg, $filename, $contentType, $pathToFilename){
    $random_hash = md5(date('r', time()));
    $headers = "From: webmaster@mysite.com
Reply-To: ".$to;
    $headers .= "
Content-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";
}

这篇关于php发送带附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆