日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

發(fā)布時間:2025/4/5 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 phpmailer 私密抄送_使用 phpmailer 发送邮件,支持抄送、密送和发送附件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、使用?composer?下載?phpmailercomposer?require?phpmailer/phpmailer

2、自定義 Mailer.php?文件,我使用的是TP5.1的框架測試,可自定義了參數(shù)弄的有點亂,大家將就下<?php

namespace?util;

use?PHPMailer\PHPMailer\PHPMailer;

class?Mailer

{

//?編碼格式為utf8,不設(shè)置編碼的話,中文會出現(xiàn)亂碼

protected?$charSet?=?'utf-8';

//?發(fā)送方的SMTP服務(wù)器地址,QQ郵箱為:smtp.qq.com

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

//?163郵箱的ssl協(xié)議方式端口號是465/994??QQ郵箱的ssl協(xié)議方式端口號是465/587

protected?$port?=?465;

//?是否使用身份驗證,默認false

protected?$smtpAuth?=?true;

//?發(fā)送方的郵箱用戶名,就是你申請163的SMTP服務(wù)使用的163郵箱,或者QQ郵箱等

protected?$mailUsername?=?'';

//?發(fā)送方的郵箱密碼,注意用163郵箱這里填寫的是“客戶端授權(quán)密碼”而不是郵箱的登錄密碼!QQ郵箱同理,如果沒有授權(quán)碼就是郵箱密碼

protected?$mailPassword?=?'';

//?使用ssl協(xié)議方式

protected?$smtpSecure?=?'ssl';

//?錯誤調(diào)試,默認關(guān)閉

protected?$smtpDebug?=?0;

//?發(fā)送郵件地址

protected?$mailFrom?=?'';

//?發(fā)送名稱

protected?$mailFromname?=?'';

//?回復(fù)郵箱地址

protected?$replyMail?=?'';

/**

*?初始化發(fā)送郵件參數(shù)

*?@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;

}

}

/**

*?發(fā)送郵件

*

*?@param?string|array?$to_mail?單人發(fā)送為郵箱地址,多人發(fā)送[['email'=>'***@163.com',?'name'=>'發(fā)送人名稱']]

*?@param?string?$mail_title????發(fā)送郵件標(biāo)題

*?@param?string?$mail_body?????發(fā)送郵件正文

*?@param?string?$to_name???????單人發(fā)送時,個人名稱,多人無效

*?@param?array?$options????????抄送、秘密抄送、附件設(shè)置,格式

*?cc,?bcc,?attachment?使用到哪個傳哪個就行了,不需要的可以不寫

*?[

*???'cc'??=>?[['email'=>'抄送郵箱地址1',?'name'=>'收件人姓名,如果為空此參數(shù)可沒有']],

*???'bcc'?=>?[['email'=>'秘密抄送郵箱地址1',?'name'=>'收件人姓名,如果為空此參數(shù)可沒有']],

*???'attachment'?=>?[['file_path'=>'附件地址',?'file_name'=>'附件名稱,如果為空此參數(shù)可沒有']]

*?]

*?@return?array

*/

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

{

$mailer?=?new?PHPMailer();

//?使用SMTP服務(wù)

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

//?設(shè)置收件人信息,如郵件格式說明中的收件人,這里會顯示為?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))?{

//?設(shè)置回復(fù)人信息,指的是收件人收到郵件后,如果要回復(fù),回復(fù)郵件將發(fā)送到的郵箱地址,第二個參數(shù)代表回復(fù)的郵箱收到郵件后看到名稱

$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);

}

}

//?設(shè)置郵件抄送人,郵件地址和姓名

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

}

}

//?設(shè)置秘密抄送人,郵件地址和姓名

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

}

}

//?設(shè)置發(fā)送附件,**?文件地址不支持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;?//?郵件標(biāo)題

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

$mailer->AltBody?=?"請切換到支持?HTML?的郵件客戶端,或者使用瀏覽器打開";??//?這個是設(shè)置純文本方式顯示的正文內(nèi)容,如果不支持Html方式,就會用到這個,基本無用

//?發(fā)送郵件

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

//?輸出錯誤信息

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

}

return?['code'=>0];

}

}

3、測試#?需要引入文件

use?util\Mailer;

#?使用

$Mailer?=?new?Mailer();

#?發(fā)送

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

$options?=?[

'cc'?=>?[

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

],

'bcc'?=>?[

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

],

];

$res?????=?$Mailer->sendEmail($email,?'測試標(biāo)題',?'測試內(nèi)容',?'QQ郵箱1',?$options);

print_r($res);

注意:發(fā)送附件不能使用URL文件地址

總結(jié)

以上是生活随笔為你收集整理的phpmailer 私密抄送_使用 phpmailer 发送邮件,支持抄送、密送和发送附件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。