php mail函数 附件,基于PHP自带的mail函数实现发送邮件以及带有附件的邮件功能 Web程序 - 贪吃蛇学院-专业IT技术平台...

PHPmail函数简介

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

其中: $to 必需。规定邮件的接收者

$subject 必需。规定邮件的主题。该参数不能包含任何换行字符。

$message 必需。规定要发送的消息。

$additional_headers 规定额外的报头,比如 From, Cc 以及 Bcc等

$additional_parameters 规定 sendmail 程序的额外参数

参考代码(Linux服务器下测试有效)

class SendMailApi

{

/**

* @param $to 收件人

* @param $subject 邮件主题

* @param $message 发送的消息

* @param $from 发件人

* @param $content_type 类型

* @param $attache 附件

*/

public function sendMail($to, $subject, $message, $from, $content_type, $attache = array())

{

if (!empty($from)) $head = "From: $from\n";

if (empty($content_type)) $content_type = "text/plain";

if (is_array($attache)) {

$boundary = "===" . md5(uniqid("")) . "===";

$head .= "Mime-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"";

$head .= "$boundary\"\n\nThis is a multi-part message in MIME format.\n\n";

$head .= "--$boundary\n";

$head .= "Content-Type: $content_type\n";

$head .= "\n$message\n\n";

while (list($key, $val) = each($attache)) {

$fd = fopen("$val", "r") or die("unable to open file$val");

$contents = chunk_split(base64_encode(fread($fd, filesize("$val"))));

fclose($fd);

$head .= "--$boundary\n";

$head .= "Content-Type: application/octet-stream; name=\"" . basename($val);

$head .= "\"\nContent-Transfer-Encoding: BASE64\n";

$head .= "Content-Disposition: attachment; filename=\"" . basename($val);

$head .= "\"\n\n" . $contents . "\n\n";

}

$head .= "--" . $boundary . "--\n\n";

} else {

if (!empty($content_type)) {

$head .= "Content-Type: $content_type\n";

$head .= "\n$message\n";

}

}

return mail($to, $subject, "", $head);

}

public function sendMailTest()

{

$to = "[email protected]"; // 邮件接收者

$subject = "test"; // 邮件标题

$from = "[email protected]"; // 邮件发送者

$subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";

$order_bn = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);

$memer_id = self::mt_rand(1000000,9999999);

$ship_mobile = '139xxxxxxxx';

//产生随机数据

$res = array(

0 =>

array(

'order_bn' => $order_bn,

'member_id' => $memer_id ,

'ship_mobile' => $ship_mobile,

),

1 =>

array(

'order_bn' => $order_bn,

'member_id' => $memer_id,

'ship_mobile' => $ship_mobile,

),

2 =>

array(

'order_bn' => $order_bn,

'member_id' => $memer_id,

'ship_mobile' => $ship_mobile,

),

3 =>

array(

'order_bn' => $order_bn,

'member_id' => $memer_id,

'ship_mobile' => $ship_mobile,

),

4 =>

array(

'order_bn' => $order_bn,

'member_id' => $memer_id,

'ship_mobile' => $ship_mobile,

),

5 =>

array(

'order_bn' => $order_bn,

'member_id' => $memer_id,

'ship_mobile' => $ship_mobile,

),

6 =>

array(

array(

'order_bn' => $order_bn,

'member_id' => $memer_id,

'ship_mobile' => $ship_mobile,

));

$csv_header = array('订单号', '会员id', '手机号');

$file_name = date("Y-m-d") . ".csv";

$fp = fopen("$file_name", 'a');

// 处理头部标题

$header = implode(',', $csv_header) . PHP_EOL;

// 处理内容

$content = '';

foreach ($res as $k => $v) {

$content .= implode(',', $v) . PHP_EOL;

}

// 拼接

$csv = $header . $content;

// 写入并关闭资源

fwrite($fp, $csv);

fclose($fp);

//添加附件

$attache = array($file_name);

$message = " This is a test"; // 邮件正文

$headers = "From: $from" . "\n";// 头部信息设置

$headers .= "MIME-Version: 1.0" . "\n";

$headers .= "Content-type: text/html; charset=uft-8" . "\r\n";

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

$rst = $this->sendMail($to, $subject, $message, $from, '', $attache);

@unlink($file_name);

var_dump($rst);

}

}

//测试

$mailObj = new SendMailApi();

$rst = $mailObj->sendMailTest();

测试效果

20180327004628567497.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值