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 = "\r\n";

// 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() );

}

在某些电子邮件提供商使用中更易读

$body .= $eol . $message . $eol . $eol; 和 $body .= $eol . $content . $eol . $eol;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值