荆轲刺秦王
公司的邮箱发送验证码功能 之前一直正常工作 但是最近停止了 查看代码发现邮件类使用的是codeigniter框架的Email class
原有的代码是:
/application/config.php
'mail' =>[
'protocol' => 'smtp',
'smtp_host' => 'mail.baidu.com',
'smtp_user' => 'root',
'smtp_pass' => 'root'
],
common.php 公共函数类:
function send_email($to, $subject, $message,$from='',$title="通知",$priority=3,$file_url='',$cc='',$bcc='') {
$config = config('mail');
if(get_client_ip()=='127.0.0.1' && IS_WIN){
$to = 'yaowei.liu@chipwealth.com';
}
if(strlen($from)==0){
$from='ad@chipwealth.com';
}
if(strlen($title)==0){
$title="通知";
}
$email = new Email($config);
$email->from($from, $title);
$email->to($to);
$email->set_priority($priority);
if($cc){
$email->cc($cc);
}
if($bcc){
$email->bcc($bcc);
}
$email->attach($file_url);
$email->subject($subject);
$email->message($message);
if($to != 'aa@aa.com'){
$email->send();
/**
$res = $email->print_debugger();
var_dump($res);exit();
**/
}
}
Email类使用的是 codeigniter 的Email类
详情见:Email Class — CodeIgniter 4.4.5 documentation
/**
* CodeIgniter Email Class
*
* Permits email to be sent using Mail, Sendmail, or SMTP.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author EllisLab Dev Team
* @link https://codeigniter.com/user_guide/libraries/email.html
*/
收到的错误代码:
email_attachment_missing
220 mail.baidu.com ESMTP Tue, 30 Apr 2024 09:13:22 +0800
hello: 250-mail.baidu.com Hello hr-t.baidu.com [192.9.230.201], pleased to meet you
250-ETRN
250-AUTH LOGIN PLAIN
250-8BITMIME
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250-STARTTLS
250 SIZE 419430400
from: 250 2.1.0 Sender OK
to: 250 2.1.5 Recipient OK
data: 354 Enter mail, end with .
email_smtp_error
email_send_failure_smtp
User-Agent: SentCMS
Date: Tue, 30 Apr 2024 09:09:44 +0800
From: =?UTF-8?Q?=E8=8A=A2=96=E=E7=AE=A1=E9=9A=E7=9F=A5?= <hr@baidu.com>
Return-Path: <hr@baidu.com>
To: yaowei.liu@baidu.com
Subject: =?UTF-8?Q?=E8=96=E8=B5==E6=AC=A1=E9=AA=E8=AF=81?= =?UTF-8?Q?=E9=AA=8C=9A=E7==A5?=
Reply-To: <hr@baidu.com>
X-Sender: hr@baidu.com
X-Mailer: SentCMS
X-Priority: 3 (Normal)
Message-ID: <6638062@baidu.com>
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="B_ALT_6680e266"
This is a multi-part message in MIME format.
Your email application may not support this format.
--B_ALT_663044d80e266
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
验证码: 879159 ,有效时间:3分钟,请尽快填写!
--B_ALT_663044d80e266
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
=E88=B5=84=E8=AF=81=E7=A0=81: 879159 ,=E6=9C=86=95=88=
=E6=B6=97=B4:3=E5=89=92=9F,=E8=AF=B7=E5=B0=B=E5=A1=AB=
=E5=98=21
--B_ALT_663046--
需要注意的是,如果想打印bug记得用email类的:
$email->print_debugger();
这个代码以前曾正常工作,但是现在却不能正常工作
最后发现是因为没有设置:
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
那么在我的代码中 我就需要设置email类的config
'mail' =>[
'protocol' => 'smtp',
'smtp_host' => 'mail.baidu.com',
'smtp_user' => 'root',
'smtp_pass' => 'root',
'crlf' => "\r\n",
'newline' => "\r\n",
],
然后更新代码,就发现可以正常发送邮件了。这个问题似乎是因为 Codeigniter默认情况下没有这样做。