Send a verification email using gmail in CakePhp 3 Follow these steps to send verification code using gmail 1. open your your project/config/app.php 2. In your app.php, replace(hide) 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), ], write this code in your app.php '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 Controller file and add this code use Cake\Mailer\Email; use Cake\Network\Exception\SocketException; 4. Write this code on function of your controller which is in used $msg="Your Password is generate"; $email = new Email('default'); $email ->transport('gmail') ->from(['abcx.com' => 'abcx.com']) ->to($to) ->subject($subject) ->emailFormat('html') ->viewVars(array('msg' => $msg)) ->send($msg); now you can send email from your controller by using this code 5. if this error generate(SMTP server did not accept the password.) then do this process i.> If the tips above didn't help, visit https://www.google.com/accounts/DisplayUnlockCaptcha and follow the steps on the page. ii.> Allow access to your Google account As a security precaution, Google may require you to complete this additional step when signing into a new device or application. To allow access, click the Continue button below. iii.> Account access enabled Please try signing in to your Google account again from your new device or application. 6. run your application
source share