Configuring gmail in Cakephp 3.x
Hello friends, welcome to findnerd. Today I am going to tell you how to configure gmail in cakephp 3.x. First of all you need to load the class for email in Controller:
use Cake\Mailer\Email;
Second step is to create configuration in config/app.php
. Now to configure gmail in cakephp you need to set your className => Smtp
, hostname => 'ssl://smtp.gmail.com
', port => 465
, username and password of your gmail account. Write the following lines as shown below:
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'youremail@gmail.com',
'password' => 'password@123',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
]
]
If you want to configure tls communication, then set tls => true
Third step is to create template for email. Lets create welcome template at src/Template/html/welcome.ctp
. You can write your welcome message on welcome.ctp file.
Next step to write function in the controller to send email:
public function sendMail(){
$email = new Email();
$email->transport('default');
$email->to('amukmca@gmail.com');
$email->template('welcome');
$email->viewVars(['user' => ['username' => 'amukmca']]);
$email->send();
exit;
}
If you get this error message: "SMTP server did not accept the password" as shown below:
Then this issue can be resolved by turning on Access for less secure apps on Google. You can do it by logging in to your google account and clicking on this link https://www.google.com/settings/security/lesssecureapps
For more information you can refer to this link: https://support.google.com/accounts/answer/6010255
Thanks for reading the blog.
2 Comment(s)