Cannot send email in laravel 5.2
I am trying to send an email with laravel 5.2 . This is my first time of sending email in laravel. But it is throwing this error
This is the error
Swift_TransportException in AbstractSmtpTransport.php line 162: Cannot send message without a sender address
This is my code of sending email
Route::get('test',function(){
$message = "hello";
Mail::send('welcome', ['key' => 'value'], function($message)
{
$message->to('iljimae.ic@gmail.com', 'John Smith')->subject('Welcome!');
});
});
This is my email settings in env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=iljimae.ic@gmail.com
MAIL_PASSWORD=xxxxxx
MAIL_ENCRYPTION=null
My welcome view only has a message "Hello world"
I already configured less secure app settings for my email in gmail settings. So please what is wrong with my code ? Why is that throwing that error ?
The error message Cannot send message without a sender address
is clear. You just need to add from
to the message:
Route::get('test',function(){
$message = "hello";
Mail::send('welcome', ['key' => 'value'], function($message)
{
$message->from('myEmail@test.com')
->to('iljimae.ic@gmail.com', 'John Smith')
->subject('Welcome!');
});
});
In order to be able to send the mail you must to change the mail encryption in the .env file to:
MAIL_ENCRYPTION=tls
Your $message chain has 'To' and 'Subject' fields but is missing 'From' field.
Just add ->from() to the chain:
$message->to('iljimae.ic@gmail.com', 'John Smith')
->subject('Welcome!')
->from(Config::get('mail.from.address'), Config::get('mail.from.name'));
Assuming that 'from' is set in your config/mail.php file (where it may refer or may not refer to environment variables). If it's not, you could just specify it directly:
$message->to('iljimae.ic@gmail.com', 'John Smith')
->subject('Welcome!')
->from('iljimae.ic@gmail.com', 'John Smith');
链接地址: http://www.djcxy.com/p/69336.html
上一篇: WP:发送电子邮件后的标题警告