php 预期响应代码 220,但在 Laravel 中得到代码“”,消息为“”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32693528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected response code 220 but got code "", with message "" in Laravel
提问by dev90
I am using Laravel Mail function to send email. The following is my app/config/mail.php
file settings.
我正在使用 Laravel 邮件功能发送电子邮件。以下是我的app/config/mail.php
文件设置。
'driver' => 'sendmail',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => '[email protected]', 'name' => 'MyName'),
'encryption' => 'tls',
'username' => 'myUsername',
'password' => "password",
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
Controller Mail Method
控制器邮件方法
//Send Mail
Mail::send('sendMail', array('key' => 'value'), function($message)
{
$message->to('[email protected]', 'Sender Name')->subject('Welcome!');
});
When I run the code it gives me following error message:
当我运行代码时,它给了我以下错误消息:
Swift_TransportException
Expected response code 220 but got code "", with message ""
Swift_TransportException
预期响应代码 220,但得到代码“”,消息为“”
I have created a SendMail.php
file in view which contains some data.
我SendMail.php
在视图中创建了一个文件,其中包含一些数据。
How do I resolve this error message?
如何解决此错误消息?
回答by Sid
This problem can generally occur when you do not enable two step verification for the gmail
account (which can be done here) you are using to send an email
. So first, enable two step verification
, you can find plenty of resources for enabling two step verification. After you enable it, then you have to create an app password
. And use the app password
in your .env
file. When you are done with it, your .env
file will look something like.
当你不启用两步验证通常会出现此问题gmail
的帐户(这是可以做到在这里使用的是发送)email
。所以首先,启用two step verification
,您可以找到大量资源来启用两步验证。启用它后,您必须创建一个app password
. 并app password
在您的.env
文件中使用。完成后,您的.env
文件将看起来像。
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=<<your email address>>
MAIL_PASSWORD=<<app password>>
MAIL_ENCRYPTION=tls
and your mail.php
和你的 mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => '<<your email>>', 'name' => '<<any name>>'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
After doing so, run php artisan config:cache
and php artisan config:clear
, then check, email should work.
这样做之后,运行php artisan config:cache
和php artisan config:clear
,然后检查,电子邮件应该可以工作。
回答by Gayan Kavirathne
In my case I had to set the
在我的情况下,我必须设置
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465 <<<<<<<------------------------- (FOCUS THIS)
MAIL_USERNAME=<<your email address>>
MAIL_PASSWORD=<<app password>>
MAIL_ENCRYPTION= ssl <<<<<<<------------------------- (FOCUS THIS)
to work it.. Might be useful. Rest of the code was same as @Sid said.
工作它.. 可能有用。其余代码与@Sid 所说的相同。
And I think that editing both environment file and app/config/mail.php is unnecessary. Just use one method.
而且我认为编辑环境文件和 app/config/mail.php 是不必要的。只需使用一种方法。
Edit as per the comment by @Zan
根据@Zan 的评论进行编辑
If you need to enable tlsprotection use following settings.
如果您需要启用tls保护,请使用以下设置。
MAIL_PORT=587
MAIL_ENCRYPTION= tls
See herefor some other gmail settings
请参阅此处了解其他一些 Gmail 设置
回答by smoothie
What helped me... changing sendmail parameters from -bs to -t.
是什么帮助了我...将 sendmail 参数从 -bs 更改为 -t。
'sendmail' => '/your/sendmail/path -t',
回答by Chavez Harris
if you are using Swift Mailer: please ensure that your $transport variable is similar to the below, based on tests i have done, that error results from ssl and port misconfiguration. note: you must include 'ssl' or 'tls' in the transport variable.
如果您使用的是 Swift Mailer:请确保您的 $transport 变量与以下类似,根据我所做的测试,该错误是由 ssl 和端口配置错误导致的。 注意:您必须在传输变量中包含“ssl”或“tls”。
EXAMPLE CODE:
示例代码:
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
->setUsername([email protected])
->setPassword(password)
;
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$message = (new Swift_Message('News Letter Subscription'))
->setFrom(['[email protected]' => 'A Name'])
->setTo(['[email protected]' => 'A Name'])
->setBody('your message body')
;
// Send the message
$result = $mailer->send($message);
回答by Jan ?ankowski
For me the problem was the port. I first incorrectly used port 465, which works for SSL
but not TLS
. So the key thing was changing the port to 587.
对我来说,问题是端口。我第一次使用不正确的端口465,其适用于SSL
但不是TLS
。所以关键是将端口更改为 587。
回答by Ahmed Numaan
That error message means that there was not response OR the server could not be connected.
该错误消息意味着没有响应或无法连接服务器。
The following settings worked on my end:
以下设置对我有用:
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
]
Note that my SMTP settings are:
请注意,我的 SMTP 设置是:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=[full gmail address]
MAIL_PASSWORD=[App Password obtained after two step verification]
MAIL_ENCRYPTION=ssl
回答by Sumit Kumar Gupta
I was facing the same issue. After researching too much on Google and StackOverflow. I found a solution very simple
我面临着同样的问题。在谷歌和 StackOverflow 上研究了太多之后。我找到了一个非常简单的解决方案
First, you need to set environment like this
首先,您需要像这样设置环境
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=example44
MAIL_ENCRYPTION=tls
[email protected]
you have to change your Gmail address and password
您必须更改您的 Gmail 地址和密码
Now you have to on less secure apps by following this link https://myaccount.google.com/lesssecureapps
现在您必须通过点击此链接https://myaccount.google.com/lesssecureapps来使用安全性较低的应用程序
回答by Sunil Kumar
I did as per sid saying my env after updating is
我按照 sid 说我的 env 更新后是
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=<mygmailaddress>
MAIL_PASSWORD=<gmailpassword>
MAIL_ENCRYPTION=tls
this did work without 2 step verification. with 2 step verification enabled it did not work for me.
这在没有两步验证的情况下确实有效。启用两步验证后,它对我不起作用。