使用PHPMailer发邮件的时候,经常出现本地可以发送,上传到服务器就发送失败了。老是提示SMTP connect() failed
把端口改成587,然后把连接方式改为tls,具体代码如下
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//email配置
return[
'Host' => 'smtp.qq.com',//SMTP 服务器
// 'port' => '465',//SMTP 服务器端口号
'port' => '587',//SMTP 服务器端口号
'SMTPSecure' => 'tls',//使用安全协议
// 'SMTPSecure' => 'ssl',//使用安全协议
'MAIL_USERNAME' => '[email protected]',//SMTP服务器用户名
'MAIL_PASSWORD' => '',///是smtp发送密码,不是登录密码
'setEmail' => '[email protected]',//发件人email
'setName' => '',//发件人名称
'replyEmail' => '[email protected]',//回复地址
'replyName' => '[email protected]',//回复名称
'MAIL_SMTPAUTH' => 'true',//回复名称
'SMTPDebug' => 0,//错误调试,默认关闭
];
465 ssl
587 tls
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['MAIL_SMTPAUTH']) ? $conf['MAIL_SMTPAUTH'] : $this->smtpAuth;
$this->mailUsername = !empty($conf['MAIL_USERNAME']) ? $conf['MAIL_USERNAME'] : $this->mailUsername;
$this->mailPassword = !empty($conf['MAIL_PASSWORD']) ? $conf['MAIL_PASSWORD'] : $this->mailPassword;
$this->smtpSecure = isset($conf['SMTPSecure']) ? $conf['SMTPSecure'] : $this->smtpSecure;
$this->smtpDebug = isset($conf['SMTPDebug']) ? $conf['SMTPDebug'] : $this->smtpDebug;
$this->mailFrom = !empty($conf['setEmail']) ? $conf['setEmail'] : $this->mailFrom;
$this->mailFromname = !empty($conf['setName']) ? $conf['setName'] : $this->mailFromname;
} else {
$mail_smtpAuth = config('email.MAIL_SMTPAUTH');
$mail_smtpSecure = config('email.SMTPSecure');
$mail_smtpDebug = config('email.SMTPDebug');
$this->host = !empty(config('email.Host')) ? config('email.Host') : $this->host;
$this->port = !empty(config('email.port')) ? config('email.port') : $this->port;
$this->smtpAuth = isset($mail_smtpAuth) ? config('email.MAIL_SMTPAUTH') : $this->smtpAuth;
$this->mailUsername = !empty(config('email.MAIL_USERNAME')) ? config('email.MAIL_USERNAME') : $this->mailUsername;
$this->mailPassword = !empty(config('email.MAIL_PASSWORD')) ? config('email.MAIL_PASSWORD') : $this->mailPassword;
$this->smtpSecure = isset($mail_smtpSecure) ? config('email.SMTPSecure') : $this->smtpSecure;
$this->smtpDebug = isset($mail_smtpDebug) ? config('email.SMTPDebug') : $this->smtpDebug;
$this->mailFrom = !empty(config('email.setEmail')) ? config('email.setEmail') : $this->mailFrom;
$this->mailFromname = !empty(config('email.setName')) ? config('email.setName') : $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 = [])
{
include(EXTEND_PATH."PHPMailer/src/PHPMailer".'.php');
include(EXTEND_PATH."PHPMailer/src/Exception".'.php');
include(EXTEND_PATH."PHPMailer/src/SMTP".'.php');
$mailer = new PHPMailer(true);
// 使用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([email protected])
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);
// }
foreach ($cc_mail as $mail) {
$mailer->addCC($mail);
}
}
// 设置秘密抄送人,邮件地址和姓名
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);
// }
foreach ($bcc_mail as $mail) {
$mailer->addBCC($mail);
}
}
// 设置发送附件,** 文件地址不支持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);
// }
foreach ($attachment as $val) {
$mailer->addAttachment($val);
}
}
$mailer->Subject = $mail_title; // 邮件标题
$mailer->Body = $mail_body; // 邮件正文
$mailer->AltBody = "请切换到支持 HTML 的邮件客户端,或者使用浏览器打开"; // 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用
// 发送邮件
if (!$mailer->send()) {
// 输出错误信息
return ['code'=>1, 'msg'=>$mailer->ErrorInfo];
}
return ['code'=>0];
}
}
/**
* 发送邮件
* @param array $options 抄送、秘密抄送、附件设置,格式
* cc, bcc, attachment 使用到哪个传哪个就行了,不需要的可以不写
* [
* 'cc' => [['email'=>'抄送邮箱地址1', 'name'=>'收件人姓名,如果为空此参数可没有']],
* 'bcc' => [['email'=>'秘密抄送邮箱地址1', 'name'=>'收件人姓名,如果为空此参数可没有']],
* 'attachment' => [['file_path'=>'附件地址', 'file_name'=>'附件名称,如果为空此参数可没有']]
* ]
*/
public function sendMail(){
$id = input('id');
$data = Db::name('base_task_mails')->where('id',$id)->find();
// $result = Email::send_mail($data['to'],'','测试邮件','邮件正文');
// dump($result);die;
$Mailer = new Mailer();
$ccs = [];
$bccs = [];
$files = [];
if(!empty($data['cc'])){
$ccs = explode(',',$data['cc']);
}
if(!empty($data['bcc'])){
$bccs = explode(',',$data['bcc']);
}
if(!empty($data['files'])){
$files = explode(',',$data['files']);
//如果附件是链接中的文件走这
//存储链接地址中的文件到本地
foreach($files as $v){
$ext = basename($v);
$isext = ROOT_PATH.'uploads/'.$ext;
//已存在不进行存储操作
if(!file_exists($isext)){
$result = file_get_contents($v);
file_put_contents(ROOT_PATH.'uploads/'.$ext, $result);
}
$path = ROOT_PATH.'uploads/'.$ext;
$attachment[] = $path;
}
//本地附件直接传附件路径
}
$options = [
'cc' => $ccs,
'bcc' => $bccs,
'attachment' => $attachment
];
$result = $Mailer->sendEmail($data['to'], $data['subject'], $data['content'], '',$options);
if($result == true){
//发送成功删除文件
foreach($attachment as $v){
unlink($v);
}
return httpreturn(ReturnCode::SUCCESS, ReturnMsg::SUCCESS);
}else{
return httpreturn(ReturnCode::ERROREMAILS, ReturnMsg::ERROREMAILS,$result);
}
}