Laravel Homestead Swift 在没有发件人地址的情况下无法发送消息

2022-08-30 10:20:46

当我尝试发送密码重置邮件时,我在Laravel 5.1 Homestead中的股票电子邮件设置中遇到此错误。

Swift_TransportException in AbstractSmtpTransport.php line 162:Cannot send message without a sender address

地址在 app/config/mail 中填写.php:

'from' => array('address' => 'myusername@gmail.com', 'name' => 'hawle'),

答案 1

在您的文件中,您需要设置电子邮件帐户的电子邮件地址和密码。您还需要设置正在使用的邮件服务器的主机和端口。.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=25
MAIL_USERNAME= ***USER NAME***
MAIL_PASSWORD= ***PASSWORD***
MAIL_ENCRYPTION=tls

或者确保文件中的所有内容都已完成(请参阅下面的注释)。mail.php

'host' => env('MAIL_HOST', 'smtp.gmail.com'),
/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/
'port' => env('MAIL_PORT', 25),
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/
'from' => ['address' => 'myusername@gmail.com', 'name' => 'hawle'],
/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),

注意:最好使用 .env 文件,因为您的生产环境中很可能具有不同的配置。

如果一切都已完成,但仍然不起作用,则可能是缓存。您可以使用以下命令清除配置缓存:

php artisan config:cache

另请注意:

  • 端口 465 适用于 Gmail。如果它不起作用,则可以使用25。
  • 该文件位于(如OP所说)。mail.php/app/config/mail.php
  • 该文件位于项目的根目录下。.env
  • Mailtrap.io 是用于测试SMTP的服务。它并没有真正发送电子邮件。

正如Viktorminator所提到的:请考虑创建应用程序密码,而不是使用通常的通行证来满足此需求。用于创建密码 myaccount.google.com/apppasswords 的链接


答案 2

确保您已在应用/配置/邮件中设置了“发件人.php

'from' => ['address' => 'myname@gmail.com', 'name' => 'myname']

它将解决问题。


推荐