300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > phpmailer 私密抄送_使用 phpmailer 发送邮件 支持抄送 密送和发送附件

phpmailer 私密抄送_使用 phpmailer 发送邮件 支持抄送 密送和发送附件

时间:2018-11-18 02:31:40

相关推荐

phpmailer 私密抄送_使用 phpmailer 发送邮件 支持抄送 密送和发送附件

1、使用composer下载phpmailercomposerrequirephpmailer/phpmailer

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

namespaceutil;

usePHPMailer\PHPMailer\PHPMailer;

classMailer

{

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

protected$charSet='utf-8';

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

protected$host='';

//163邮箱的ssl协议方式端口号是465/994QQ邮箱的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='';

/**

*初始化发送邮件参数

*@paramarray$conf

*/

publicfunction__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;

}

}

/**

*发送邮件

*

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

*@paramstring$mail_title发送邮件标题

*@paramstring$mail_body发送邮件正文

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

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

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

*[

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

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

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

*]

*@returnarray

*/

publicfunctionsendEmail($to_mail='',$mail_title='',$mail_body='',$to_name='',$options=[])

{

$mailer=newPHPMailer();

//使用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@)

if(is_array($to_mail)){

foreach($to_mailas$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_mailas$mail){

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

}

}else{

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

}

}

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

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

$cc_mail=$options['cc'];

foreach($cc_mailas$mail){

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

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

}

}

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

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

$bcc_mail=$options['bcc'];

foreach($bcc_mailas$mail){

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

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

}

}

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

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

$attachment=$options['attachment'];

foreach($attachmentas$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、测试#需要引入文件

useutil\Mailer;

#使用

$Mailer=newMailer();

#发送

$email='****@';

$options=[

'cc'=>[

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

],

'bcc'=>[

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

],

];

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

print_r($res);

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。