Send mail using gmail smtp cakephp 3
to send mail using gmail in cakephp you nedd to follow these steps
1. open your yourproject/config/app.php
2. in your app.php replace
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
to this code
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
'gmail'=> [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'abc@gmail.com', //your gmail address
'password' => 'abcd123', //your gmail password
'className' => 'Smtp',
'log' => true,
'context' => [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]
],
],
3. Now open your appController file and add this
use Cake\Mailer\Email;
4. now you can send email from your controller by using this code
$email = new Email('default');
->transport('gmail')
->from(['abcx.com' => 'abcx.com'])
->to($to)
->subject($subject)
->emailFormat('html')
->viewVars(array('msg' => $msg))
->send($msg);
5. you can create a function in appController and use this function into all controller
public function sendUserEmail($to,$subject,$msg)
{
$email = new Email('default');
->transport('gmail')
->from(['abcx.com' => 'abcx.com'])
->to($to)
->subject($subject)
->emailFormat('html')
->viewVars(array('msg' => $msg))
->send($msg);
}
and use this code to send mail from any controller
$this->sendUserEmail($email,$aSubject,$aBody);
This tutorial is related to
How to send mail using gmail in cakephp 3
send mail in cakephp 3
how to send mail in cakephp3 with gmail
how to use mail class cakephp 3
It has been a great help to me. Thank you very much for sharing
ReplyDelete