无法在Laravel 5.2中发送电子邮件

问题描述 投票:0回答:3

我正在尝试通过Laravel 5.2发送电子邮件。这是我第一次在Laravel中发送电子邮件。但它抛出此错误

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

这是我发送电子邮件的代码:

Route::get('test',function(){
    $message  = "hello";
    Mail::send('welcome', ['key' => 'value'], function($message)
    {

       $message->to('[email protected]', 'John Smith')->subject('Welcome!');
    });
});

这是我在环境文件中的电子邮件设置

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=xxxxxx

MAIL_ENCRYPTION=null

我的欢迎视图仅显示一条消息“ Hello world”

我已经在Gmail设置中为我的电子邮件配置了不太安全的应用程序设置。那么,我的代码有什么问题,为什么会抛出该错误呢?

php email laravel-5.2
3个回答
2
投票

错误消息Cannot send message without a sender address已清除。您只需要在消息中添加from

Route::get('test',function(){
    $message  = "hello";
    Mail::send('welcome', ['key' => 'value'], function($message)
    {

       $message->from('[email protected]')
           ->to('[email protected]', 'John Smith')
           ->subject('Welcome!');
    });
});

为了能够发送邮件,您必须将.env文件中的邮件加密更改为:

MAIL_ENCRYPTION=tls

0
投票

您的$ message链具有“收件人”和“主题”字段,但缺少“发件人”字段。

只需将-> from()添加到链中:

$message->to('[email protected]', 'John Smith')
    ->subject('Welcome!')
    ->from(Config::get('mail.from.address'), Config::get('mail.from.name'));

假设您在config / mail.php文件中设置了“ from”(它可能引用或可能不引用环境变量)。如果不是,您可以直接指定它:

$message->to('[email protected]', 'John Smith')
    ->subject('Welcome!')
    ->from('[email protected]', 'John Smith');

0
投票

如果未发送您的邮件,请从.env文件中删除此参数“ MAIL_FROM_ADDRESS = null”。这对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.