In order to send an email via Gmail SMTP Server in Laravel, you may follow the following directions.
1. Gmail account setup
- Login to your gmail account;
- Select Google account > Sign In & Security > Sign in to google:enable two step verification [Enter your password again and a unique verification code that's sent to your phone], then generate app password, and you can use that app password in .env file.
- Select Google account > Sign In & Security > Apps with account access (if necessary):enable "Allow less secure apps".
- 2. Laravel configuration setup.env file will look like this:
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=<your_email_address> MAIL_PASSWORD=<your_gmail_app_password_> MAIL_ENCRYPTION=tls
or like this:MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=465 MAIL_USERNAME=<your_email_address> MAIL_PASSWORD=<your_gmail_app_password_> MAIL_ENCRYPTION=ssl
- Note: Don't forget to run "php artisan config:cache" after you make changes in your .env file.3. Laravel controller file:
$data = array('name'=>"Joshua", "body" => "This is my first Online Email."); Mail::send('emails.mail', $data, function($message) { $message->to('TO_EMAIL_ADDRESS', 'To Website') ->subject('Online Email Test'); $message->from('FROM_EMAIL_ADDRESS','From Visitor'); });
4. Laravel View file:resources->views->emails->mail.blade.phpHi <strong>{{ $name }}</strong>, <p>{{ $body }}</p>
No comments:
Post a Comment