如何通过gmail smtp与laravel发送邮件?

编程入门 行业动态 更新时间:2024-10-24 15:24:02
本文介绍了如何通过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 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') ->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 MAIL_PORT=587 MAIL_USERNAME=myemail@gmail 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.

如何生成应用密码:

  • 转到您的 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发送邮件?

    本文发布于:2023-10-13 00:14:19,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1486273.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:发送邮件   gmail   smtp   laravel

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!