2

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('salondesbelleslettres@gmail.com')
                ->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.

loicgeek
  • 56
  • 1
  • 6
  • Did you enable less secure app access with your Google account? Learn more about this https://support.google.com/accounts/answer/6010255?hl=en – Tamim Jan 04 '19 at 08:08
  • yes, i have done it, and created password for my laravel app. – loicgeek Jan 04 '19 at 08:14
  • You need to read the documentation first. https://laravel.com/docs/5.7/mail – Kyslik Jan 04 '19 at 08:46
  • Does this answer your question? [How to to send mail using gmail in Laravel 5.1?](https://stackoverflow.com/questions/32515245/how-to-to-send-mail-using-gmail-in-laravel-5-1) – miken32 Feb 20 '20 at 22:30

3 Answers3

6

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.

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
1

These settings worked for me instead.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
Mozammil
  • 8,520
  • 15
  • 29
1

You should try this:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremailaddress@gmail.com
MAIL_PASSWORD=gtxajikwsqmlaqcr  // your app password
MAIL_ENCRYPTION=tls