加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php使用Mail()函数发送带附件的邮件

发布时间:2020-05-25 02:29:34 所属栏目:PHP 来源:互联网
导读:本文章向大家介绍php使用Mail()函数发送带附件的邮件,需要的朋友可以参考一下。

您可以尝试使用以下代码:

$filename = 'myfile';

$path = 'your path goes here';

$file = $path . "/" . $filename;

$mailto = 'mail@mail.com';

$subject = 'Subject';

$message = 'My message';

$content = file_get_contents($file);

$content = chunk_split(base64_encode($content));

// a random hash will be necessary to send mixed content

$separator = md5(time());

// carriage return type (RFC)

$eol = "rn";

// main header (multipart mandatory)

$headers = "From: name " . $eol;

$headers .= "MIME-Version: 1.0" . $eol;

$headers .= "Content-Type: multipart/mixed; boundary="" . $separator . """ . $eol;

$headers .= "Content-Transfer-Encoding: 7bit" . $eol;

$headers .= "This is a MIME encoded message." . $eol;

// message

$body = "--" . $separator . $eol;

$body .= "Content-Type: text/plain; charset="iso-8859-1"" . $eol;

$body .= "Content-Transfer-Encoding: 8bit" . $eol;

$body .= $message . $eol;

// attachment

$body .= "--" . $separator . $eol;

$body .= "Content-Type: application/octet-stream; name="" . $filename . """ . $eol;

$body .= "Content-Transfer-Encoding: base64" . $eol;

$body .= "Content-Disposition: attachment" . $eol;

$body .= $content . $eol;

$body .= "--" . $separator . "--";

//SEND Mail

if (mail($mailto,$subject,$body,$headers)) {

echo "mail send ... OK"; // or use booleans here

} else {

echo "mail send ... ERROR!";

print_r( error_get_last() );

}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读