频道栏目
首页 > 资讯 > E-mail > 正文

php send e-mail with attachment

16-01-08        来源:[db:作者]  
收藏   我要投稿
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";
}

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

相关TAG标签
上一篇:Struts的Token(令牌)机制解决表单重复提交的问题
下一篇:GET与POST比较(数据大小、数据类型、传送形式)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站