How to to Send Mail Using Gmail in Laravel

How to send mail with laravel through gmail smtp?

Need to change your .env file to something like this:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

After completion of .env edit please enter this command in your terminal for clear cache:

php artisan config:cache

You need to generate app password, and you can use that app password in .env file.

How to generate an App password:

  1. Go to your Google Account

  2. On the left navigation panel, click Security.

  3. On the Signing in to Google panel, click App passwords.
    (Note: If you can't get to the page, 2-Step Verification is:
    Not set up for your account,
    Set up for security keys only)

  4. At the bottom, click Select app and choose the app you’re using.

  5. Click Select device and choose the device you’re using.
  6. Click Generate
  7. Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
  8. Click Done.

Once you are finished, you won’t see that App password code again.

Note: You may not be able to create an App password for less secure apps.

Send mail using Laravel and gmail

With Laravel 6, I am able to send mail with like this

.env

MAIL_DRIVER=smtp 
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youruser@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME=yourname

please notice that you use "smtp.google.com" not "smtp.gmail.com",
also I think you must have "Enable POP for all mail" at your gmail setting

Controller

use Illuminate\Support\Facades\Mail;

Mail::html($body, function ($message) use ($email_to) {
$message
->to($email_to)
->subject($subject);
});

mail.php

The default

Please note that i do not use an app password, but the account password becouse the gmail account I use, is deticated to the application.

how to send smtp email in fresh laravel 9 project

First you should set your mail configuration in your .env

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Then create mail class using this command php artisan make:mail DemoMail

Update your DemoMail Class to this

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class DemoMail extends Mailable
{
use Queueable, SerializesModels;

public $mailData;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct($mailData)
{
$this->mailData = $mailData;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Mail from Mansjoer@gmail.com')
->view('emails.demoMail');
}
}

Now create your mail controller using this command php artisan make:controller MailController

Put this into your MainController file

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;
use App\Mail\DemoMail;

class MailController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$mailData = [
'title' => 'Mail from Mansjoer.com',
'body' => 'This is for testing email using smtp.'
];

Mail::to('your_email@gmail.com')->send(new DemoMail($mailData));

dd("Email is sent successfully.");
}
}

Create your route using this code

use App\Http\Controllers\MailController;

Route::get('send-mail', [MailController::class, 'index']);

Then make your MainController view

<!DOCTYPE html>
<html>
<head>
<title>Mansjoer Corporation</title>
</head>
<body>
<h1>{{ $mailData['title'] }}</h1>
<p>{{ $mailData['body'] }}</p>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

<p>Thank you</p>
</body>
</html>

Now you can try to type the given URL and view the app output

http://localhost:8000/send-mail

Gmail SMTP server stopped working as it no longer support Less Secure Apps

Yes, It's not working after removing the option by google. But nothing to worry! It's still very simple to send email. To send email again you need to do this as bellow:

  1. Login to your gmail
  2. Go to Security setting and Enable 2 factor authentication
  3. After enabling this you can see app passwords option. Click here!
  4. And then, from Your app passwords tab select Other option and put your app name and click GENERATE button to get new app password.
  5. Finally copy the 16 digit of password and click done. Now use this password instead of email password to send mail via your app.

Now you can use just email and this generated pass to send email.



Related Topics



Leave a reply



Submit