1、使用 composer 下载 phpmailercomposer require phpmailer/phpmailer

2、自定义 Mailer.php 文件,我使用的是TP5.1的框架测试,可自定义了参数弄的有点乱,大家将就下<?php

namespace util;

use PHPMailer\PHPMailer\PHPMailer;

class Mailer

{

// 编码格式为utf8,不设置编码的话,中文会出现乱码

protected $charSet = 'utf-8';

// 发送方的SMTP服务器地址,QQ邮箱为:smtp.qq.com

protected $host = 'smtp.163.com';

// 163邮箱的ssl协议方式端口号是465/994  QQ邮箱的ssl协议方式端口号是465/587

protected $port = 465;

// 是否使用身份验证,默认false

protected $smtpAuth = true;

// 发送方的邮箱用户名,就是你申请163的SMTP服务使用的163邮箱,或者QQ邮箱等

protected $mailUsername = '';

// 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!QQ邮箱同理,如果没有授权码就是邮箱密码

protected $mailPassword = '';

// 使用ssl协议方式

protected $smtpSecure = 'ssl';

// 错误调试,默认关闭

protected $smtpDebug = 0;

// 发送邮件地址

protected $mailFrom = '';

// 发送名称

protected $mailFromname = '';

// 回复邮箱地址

protected $replyMail = '';

/**

* 初始化发送邮件参数

* @param array $conf

*/

public function __construct($conf = [])

{

if (!empty($conf)) {

$this->charSet      = !empty($conf['charSet']) ? $conf['charSet'] : $this->charSet;

$this->host         = !empty($conf['host']) ? $conf['host'] : $this->host;

$this->port         = !empty($conf['port']) ? $conf['port'] : $this->port;

$this->smtpAuth     = isset($conf['smtpAuth']) ? $conf['smtpAuth'] : $this->smtpAuth;

$this->mailUsername = !empty($conf['mailUsername']) ? $conf['mailUsername'] : $this->mailUsername;

$this->mailPassword = !empty($conf['mailPassword']) ? $conf['mailPassword'] : $this->mailPassword;

$this->smtpSecure   = isset($conf['smtpSecure']) ? $conf['smtpSecure'] : $this->smtpSecure;

$this->smtpDebug    = isset($conf['smtpDebug']) ? $conf['smtpDebug'] : $this->smtpDebug;

$this->mailFrom     = !empty($conf['mailFrom']) ? $conf['mailFrom'] : $this->mailFrom;

$this->mailFromname = !empty($conf['mailFromname']) ? $conf['mailFromname'] : $this->mailFromname;

} else {

$mail_smtpAuth   = config('email.mail_smtpAuth');

$mail_smtpSecure = config('email.mail_smtpSecure');

$mail_smtpDebug  = config('email.mail_smtpDebug');

$this->host         = !empty(config('email.mail_host')) ? config('email.mail_host') : $this->host;

$this->port         = !empty(config('email.mail_port')) ? config('email.mail_port') : $this->port;

$this->smtpAuth     = isset($mail_smtpAuth) ? config('email.mail_smtpAuth') : $this->smtpAuth;

$this->mailUsername = !empty(config('email.mail_mailUsername')) ? config('email.mail_mailUsername') : $this->mailUsername;

$this->mailPassword = !empty(config('email.mail_mailPassword')) ? config('email.mail_mailPassword') : $this->mailPassword;

$this->smtpSecure   = isset($mail_smtpSecure) ? config('email.mail_smtpSecure') : $this->smtpSecure;

$this->smtpDebug    = isset($mail_smtpDebug) ? config('email.mail_smtpDebug') : $this->smtpDebug;

$this->mailFrom     = !empty(config('email.mail_mailFrom')) ? config('email.mail_mailFrom') : $this->mailFrom;

$this->mailFromname = !empty(config('email.mail_mailFromname')) ? config('email.mail_mailFromname') : $this->mailFromname;

}

}

/**

* 发送邮件

*

* @param string|array $to_mail 单人发送为邮箱地址,多人发送[['email'=>'***@163.com', 'name'=>'发送人名称']]

* @param string $mail_title    发送邮件标题

* @param string $mail_body     发送邮件正文

* @param string $to_name       单人发送时,个人名称,多人无效

* @param array $options        抄送、秘密抄送、附件设置,格式

* cc, bcc, attachment 使用到哪个传哪个就行了,不需要的可以不写

* [

*   'cc'  => [['email'=>'抄送邮箱地址1', 'name'=>'收件人姓名,如果为空此参数可没有']],

*   'bcc' => [['email'=>'秘密抄送邮箱地址1', 'name'=>'收件人姓名,如果为空此参数可没有']],

*   'attachment' => [['file_path'=>'附件地址', 'file_name'=>'附件名称,如果为空此参数可没有']]

* ]

* @return array

*/

public function sendEmail($to_mail = '', $mail_title = '', $mail_body = '', $to_name = '', $options = [])

{

$mailer = new PHPMailer();

// 使用SMTP服务

$mailer->isSMTP();

$mailer->isHTML();

$mailer->SMTPDebug  = $this->smtpDebug;

$mailer->CharSet    = $this->charSet;

$mailer->Host       = $this->host;

$mailer->Port       = $this->port;

$mailer->SMTPAuth   = $this->smtpAuth;

$mailer->Username   = $this->mailUsername;

$mailer->Password   = $this->mailPassword;

$mailer->SMTPSecure = $this->smtpSecure;

$mailer->From       = $this->mailFrom;

$mailer->FromName   = $this->mailFromname;

// 设置收件人信息,如邮件格式说明中的收件人,这里会显示为 zhangsan(yyyy@163.com)

if (is_array($to_mail)) {

foreach ($to_mail as $mail) {

$mailer->addAddress($mail['email'], $mail['name']);

}

} else {

$mailer->addAddress($to_mail, $to_name);

}

if (!empty($this->replyMail)) {

// 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址,第二个参数代表回复的邮箱收到邮件后看到名称

$mailer->addReplyTo($this->replyMail, $to_name);

if (is_array($to_mail)) {

foreach ($to_mail as $mail) {

$mailer->addReplyTo($mail['email'], $mail['name']);

}

} else {

$mailer->addReplyTo($to_mail, $to_name);

}

}

// 设置邮件抄送人,邮件地址和姓名

if (isset($options['cc'])) {

$cc_mail = $options['cc'];

foreach ($cc_mail as $mail) {

$mail_name = $mail['name'] ?? $mail['name'];

$mailer->addCC($mail['email'], $mail_name);

}

}

// 设置秘密抄送人,邮件地址和姓名

if (isset($options['bcc'])) {

$bcc_mail = $options['bcc'];

foreach ($bcc_mail as $mail) {

$mail_name = $mail['name'] ?? $mail['name'];

$mailer->addBCC($mail['email'], $mail_name);

}

}

// 设置发送附件,** 文件地址不支持URL格式 **

if (!empty($options['attachment'])) {

$attachment = $options['attachment'];

foreach ($attachment as $val) {

$file_name = isset($val['file_name']) ? $val['file_name'] : '';

$mailer->addAttachment($val['file_path'], $file_name);

}

}

$mailer->Subject = $mail_title; // 邮件标题

$mailer->Body    = $mail_body;  // 邮件正文

$mailer->AltBody = "请切换到支持 HTML 的邮件客户端,或者使用浏览器打开";  // 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用

// 发送邮件

if (!$mailer->send()) {

// 输出错误信息

return ['code'=>1, 'msg'=>$mailer->ErrorInfo];

}

return ['code'=>0];

}

}

3、测试# 需要引入文件

use util\Mailer;

# 使用

$Mailer = new Mailer();

# 发送

$email   = '****@qq.com';

$options = [

'cc' => [

['email'=>'****@163.com', 'name'=>'163邮箱1']

],

'bcc' => [

['email'=>'****@qq.com', 'name'=>'QQ邮箱2']

],

];

$res     = $Mailer->sendEmail($email, '测试标题', '测试内容', 'QQ邮箱1', $options);

print_r($res);

注意:发送附件不能使用URL文件地址

phpmailer 私密抄送_使用 phpmailer 发送邮件,支持抄送、密送和发送附件相关推荐

  1. phpmailer 私密抄送_使用PHPMailer发送邮件实例

    本文实例为大家分享了PHPMailer发送邮件的具体代码,供大家参考,具体内容如下 /* to: 邮件接收地址 subject: 邮件主题 body: 邮件内容 attachpath:附件地址 cc: ...

  2. phpmailer 私密抄送_PHPMailer 发送邮件(含详细介绍及使用方法说明)

    一: 上篇文章PHP mail()方法发送邮件部分邮箱无法收到邮件问题提到要介绍一下phpmailer这款免费开源的php 邮件程序,下面我们来看看吧,以下资料全部来自phpmailer官方网站: P ...

  3. phpmailer 私密抄送_phpmailer发送邮件(附件)

    以前一直都在用发送邮件 但是没有用过发送附件 刚接到需求头大 在网上找了半天但是还是都是没有附件的 现在整理一份 设置smtp什么的我就不说了 直接贴代码 common里面的代码 需要use一下 us ...

  4. phpmailer 私密抄送_最详细的phpmailer的使用方法

    第一,需要下载PHPMailer文件包phpmailer,PHPMailer 非常小巧.简单.方便.快捷. 第二,确认你的服务器系统已经支持socket ,通过phpinfo();查看是否支持sock ...

  5. phpmailer 私密抄送_phpmailer 参数使用说明

    $AltBody--属性 出自:PHPMailer::$AltBody 文件:class.phpmailer.php 说明:该属性的设置是在邮件正文不支持HTML的备用显示 AddAddress--方 ...

  6. phpmailer 私密抄送_phpmailer中文手册属性方法

    $AltBody --属性 出自:PHPMailer::$AltBody ,文件:class.phpmailer.php 阐发 :该属性的设置是在邮件正文不支持 HTML的备用表现 AddAddres ...

  7. PHP下利用PHPMailer配合QQ邮箱下的域名邮箱发送邮件

    作为PHP入门开发者,常常有这种述求:自己的网站中需要添加一个使用自己的域名作为发件人邮件地址的自动发送邮件的方法,用于诸如给用户发送验证码.通知信息等.比如:我的某个用户注册模块,需要使用regis ...

  8. 板邓:PHP下利用PHPMailer配合QQ邮箱下的域名邮箱发送邮件(转)

    作为PHP入门开发者,常常有这种述求:自己的网站中需要添加一个使用自己的域名作为发件人邮件地址的自动发送邮件的方法,用于诸如给用户发送验证码.通知信息等.比如:我的某个用户注册模块,需要使用regis ...

  9. php配置email支持_使用PHPMailer实现PHP发邮件功能

    第一步: 打开网址https://github.com/PHPMailer/PHPMailer/ 下载PHPMailer,PHPMailer 需要 PHP 的 sockets 扩展支持,而登录 QQ ...

最新文章

  1. java通过异常处理错误,java基础之通过错误处理异常
  2. SAP创建中国版免费在线课程openSAP
  3. 【数据结构与算法】最小生成树--Kruskal算法 Prim算法
  4. java moneyutils_java中每月等额与先息后本计算
  5. android canvas帧动画,html5视频,canvas,画布
  6. jQuery Mobile开发的新闻阅读器,适应iphone和android手机
  7. COM编程入门 第三部分(网文补充)
  8. IM即时通讯项目讲解(二) 自定义实现图片选择GalleryView
  9. linux集群环境搭建
  10. excel自动排班表_Excel通用值班表日历版,排班人员自动显示,万年历套用
  11. Linux mysql 主从 配置
  12. 图片验证码的逻辑实现
  13. 学习型组织的思维方式:保持努力,终身成长!
  14. ROUGE: A Package for Automatic Evaluation of Summaries
  15. python 期货现货差价监测_基差,即期货与现货之间价格之差
  16. 哥德尔不完全性定理探索数学边界
  17. Web前端(一)HTML超文本标记语言
  18. 一梦江湖(楚留香)自用日常手机脚本
  19. 虚拟机重启网络失败:Error:Failed to start LSB: Bring up/down networking的解决方法
  20. 北大计算机科学与技术教材,北京大学计算机科学与技术参考书目

热门文章

  1. 【UAV】从单个螺旋桨到四旋翼无人机运动学分析
  2. 1.5 卷积步长-深度学习第四课《卷积神经网络》-Stanford吴恩达教授
  3. Python对比两个txt文件的不同
  4. STM32 电机教程 27 - ST MCLIB实战之 PID参数调节
  5. 一起学nRF51xx 18 -  蓝牙协议栈烧录
  6. 4、C语言面试笔试--内存操作-指针
  7. Android中对Log日志文件的分析
  8. 理一理Latency、Bandwidth、Throughput、Response Time概念的区别
  9. Struts2配置文件【代码库】
  10. 2016-2017-2 《Java程序设计》预备作业1 总结