loicgeek
loicgeek

Reputation: 56

How to send mail with laravel through gmail smtp?

I want to make contact Us page in my website, i have successfully do it with mailtrap, but when i want to send with gmail smtp server , i receive nothing and i get no error. this is my .env mail config :

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=myemail
MAIL_PASSWORD=my_app_password
MAIL_ENCRYPTION=ssl

and code i use to send email:

    $obj=new \stdClass();       
    $obj->contenu=$request->input('contenu');
    $obj->objet=$request->input('objet');
    $obj->email=$request->input('email');
    $data=array('contenu'=>$obj->contenu,'email'=>$obj->email);
    Mail::send("front.contactUs",$data,function ($message) use ($obj){
        $message->from($obj->email);
        $message->to('[email protected]')
                ->subject($obj->objet);
    });
    if(count(Mail::failures())>0){
        return redirect()->back()->with('error','Votre demande na pas ete soumise');
    }else{
        return redirect()->back()->with('success','Merci de Nous Avoir Contacté');
    }

Thanks.

Upvotes: 2

Views: 8358

Answers (3)

Udhav Sarvaiya
Udhav Sarvaiya

Reputation: 10111

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

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
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.

Upvotes: 6

user10186369
user10186369

Reputation:

You should try this:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=gtxajikwsqmlaqcr  // your app password
MAIL_ENCRYPTION=tls

Upvotes: 1

Mozammil
Mozammil

Reputation: 8750

These settings worked for me instead.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

Upvotes: 1

Related Questions