[英]Cakephp 3 email with SMTP not working
I am trying to send an email from my CakePHP 3 application. 我正在尝试从CakePHP 3应用程序发送电子邮件。 But every time it is using the localhost SMTP and I am getting the error. 但是,每次使用本地SMTP时,都会收到错误消息。
So here is my code. 这是我的代码。
public function sendEmail($email, $subject, $message){
// Sample SMTP configuration.
$this->loadModel('Generalsettings');
$query = $this->Generalsettings->find('all')->where(['meta_key' => 'smtp_details'])->applyOptions(['default' => false]);
$smtpdetail = $query->first();
$detail = json_decode($smtpdetail->value);
Email::configTransport('gmail', [
'host' => $detail['host'], //value is 'ssl://smtp.gmail.com'
'port' => $detail['port'], //value is 465
'username' => $detail['username'],
'password' => $detail['password'],
'className' => 'Smtp'
]);
$emailClass = new Email();
$emailClass->from(['er.dhimanmanoj@gmail.com' => "Sender"])
->to($email)
->subject($subject)
->send($message);
}
Please tell me if I am doing something wrong. 请告诉我我做错了什么。 Thanks in advance. 提前致谢。
You haven't specified the transport you just created using configTransport()
method. 您尚未指定刚刚使用configTransport()
方法创建的传输。 So it is taking the default
settings from config/app.php. 因此它将采用config / app.php中的default
设置。
You can setup transport like this: 您可以这样设置传输:
$emailClass = new Email();
$emailClass->transport('gmail');
NOTE: Deprecated since version 3.4.0: Use setTransport() instead of transport(). 注意:从版本3.4.0开始不推荐使用:使用setTransport()代替transport()。
For more info please refer to this link @ https://book.cakephp.org/3.0/en/core-libraries/email.html 有关更多信息,请参阅此链接@ https://book.cakephp.org/3.0/en/core-libraries/email.html
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.