Laravel email

Four steps:
  1. Change settings in .env file. Then restart server.
  2. Change settings in config\mail.php
  3. Write Mail::send code in route or Controller
  4. If you use smtp.gmail.com then allow less secure apps: https://myaccount.google.com/lesssecureapps
    Note: Refresh and check it again.
  5. If still not working: Go to https://accounts.google.com/UnlockCaptcha , and click continue and unlock your account for access through other media/sites.
  6. After unlocking captcha, send email through your server immediately.
Sept details:
  1. Change settings in .env file.

    .env

    MAIL_DRIVER = smtp
    MAIL_HOST = smtp.gmail.com
    MAIL_PORT = 587
    MAIL_USERNAME = your-gmail (rejauldu@gmail.com)
    MAIL_PASSWORD = gmail-password
    MAIL_ENCRYPTION = tls
  2. Change settings in config\mail.php
  3. Do not forget to Run: php artisan config:cache
Example:
Flatvara (With two step verification and https)
.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
mail_username=flatvara@gmail.com
MAIL_PASSWORD=dp7cmpd6mc /* gmail pass */
MAIL_ENCRYPTION=ssl
config/mail.php
'username' => 'flatvara@gmail.com', /* app name was 'flatvara' */
'password' => 'kmradrfzlsmkajnc', /*App password*/ 
Note: To create app->Google apps icon>Account>Secure account>Third-party access.
Error: Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Network is unreachable)
Solution: change the .env configuration to ->
MAIL_DRIVER=sendmail

Email Verification

1. Follow the steps: https://laravel.com/docs/5.8/verification
2.In order to send the email after successful registration you can do this workaround:

at App\Http\Controllers\Auth\RegisterController

change this:

protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
to this:

protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->sendEmailVerificationNotification();
return $user;
}

Labels: ,

© copyright-2020 Rejaul