phpmailer 私密抄送_使用 phpmailer 发送邮件,支持抄送、密送和发送附件
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 发送邮件,支持抄送、密送和发送附件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 德国曼发动机怎么样 评测德国曼发动机的性
- 下一篇: php5.1 facade,php设计模