如何通过gmail smtp与laravel发送邮件? [英] How to send mail with laravel through gmail smtp?

查看:699
本文介绍了如何通过gmail smtp与laravel发送邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网站上与我们联系页面,我已经成功地通过mailtrap进行了操作,但是当我想通过gmail smtp服务器发送邮件时,我什么也没收到,也没有收到任何错误. 这是我的.env邮件配置:

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é');
    }

谢谢.

推荐答案

需要将您的.env文件更改为以下内容:

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

完成.env编辑后,请在您的终端中输入以下命令以清除缓存:

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

php artisan config:cache

您需要生成应用密码,并且可以在.env文件中使用该应用密码.

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

如何生成应用密码:

  1. 转到您的 Google帐户

在左侧导航面板上,单击安全性.

On the left navigation panel, click Security.

登录 Google 面板上,单击应用密码. (注意:如果您无法进入页面,则两步验证为: 未设置您的帐户, 仅设置安全密钥)

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)

在底部,单击选择应用程序",然后选择您正在使用的应用程序.

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

完成后,您将不会再看到该应用的密码.

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.

这篇关于如何通过gmail smtp与laravel发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆