php 试图让 Laravel 5 电子邮件工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29100877/
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
Trying to get Laravel 5 email to work
提问by Vantheman6
I'm trying to send an email to a specified user by typing in the URL, but I'm getting the following error:
我正在尝试通过输入 URL 向指定用户发送电子邮件,但出现以下错误:
Swift_TransportException in AbstractSmtpTransport.php line 383: Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
AbstractSmtpTransport.php 第 383 行中的 Swift_TransportException:预期响应代码为 250,但得到代码“530”,消息为“530 5.7.1 Authentication required
So far I'm just trying to get it to work with Gmail. How can I get this to work?
到目前为止,我只是想让它与 Gmail 一起使用。我怎样才能让它发挥作用?
This is what I have so far: 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' =>"[email protected]" , 'name' => "example"],
'encryption' => 'tls',
'username' => env('[email protected]'),
'password' => env('MyPassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
This is what I have in the routes:
这是我在路线中的内容:
Route::get('test', function() {
Mail::send('Email.test', [], function ($message) {
$message->to('[email protected]', 'HisName')->subject('Welcome!');
});
});
This is what I have in my controller:
这是我的控制器中的内容:
class MailController extends Controller
{
public function Sending_Email()
{
$this->call('GET','Email.test');
return View('Email.test');
}
}
And this is what is in my .env file:
这就是我的 .env 文件中的内容:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=MyPassword
回答by Osei-Bonsu Christian
I know it's working for you now @Vantheman6 but this is what worked for me in case it's the same for someone else.
我知道它现在对你有用 @Vantheman6 但这对我有用,以防其他人也一样。
I added to my .env file the details of the mail service I am using. So make sure the following details
我在 .env 文件中添加了我正在使用的邮件服务的详细信息。所以请确保以下详细信息
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=MyPassword
in the .env file are accurate.
在 .env 文件中是准确的。
NOTE: Don't forget to restart your server after editing the .env file so it will pick the new data that you put in there.
注意:不要忘记在编辑 .env 文件后重新启动您的服务器,以便它会选择您放入其中的新数据。
If you don't restart your server, the .env file will still continue to present the old mail data to the app even though you have made changes which can cause this error.
如果您不重新启动服务器,即使您进行了可能导致此错误的更改,.env 文件仍将继续向应用程序显示旧邮件数据。
回答by Amir
My .env file configuration is like this for laravel 5.1
我的 .env 文件配置对于 laravel 5.1 是这样的
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=****************
MAIL_ENCRYPTION=tls
here most important thing is that I created gmail application specific password(16 digit).
这里最重要的是我创建了 gmail 应用程序特定的密码(16 位)。
I need to reveal one more thing since no luck for any of configuration. That is, whenever I changed .env file need to run this command
我需要再透露一件事,因为任何配置都没有运气。也就是说,每当我更改 .env 文件时都需要运行此命令
php artisan config:cache
And this is most most most important because without this command laravel executes previous settings from it's cache. It's required me more than 10 hours to figure out.
这是最重要的,因为如果没有这个命令,laravel 会从它的缓存中执行之前的设置。我花了10多个小时才弄明白。
回答by jszobody
You are getting an authentication error because the username and password in your config file is setup wrong.
您收到身份验证错误,因为配置文件中的用户名和密码设置错误。
Change this:
改变这个:
'username' => env('[email protected]'),
'password' => env('MyPassword'),
To this:
对此:
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
The env
method checks your .env file. In your .env file you call these MAIL_USERNAME
, so that's what you need to pass to the env
method.
该env
方法会检查您的 .env 文件。在您的 .env 文件中,您调用这些MAIL_USERNAME
,因此这就是您需要传递给env
方法的内容。
One troubleshooting tip: add dd(Config::get('mail'));
so that you can see the actual generated config. This will help you spot issues like this, and know exactly what information Laravel is going to try and use. So you may want to stop that in your test route temporarily to examine what you have:
一个故障排除提示:添加dd(Config::get('mail'));
以便您可以看到实际生成的 config。这将帮助你发现这样的问题,并确切地知道 Laravel 将尝试和使用哪些信息。因此,您可能希望在测试路线中暂时停止它以检查您拥有的内容:
Route::get('test', function()
{
dd(Config::get('mail'));
});
回答by zainudin noori
the problem will be solved by clearing cache
问题将通过清除缓存解决
php artisan config:cache
回答by yaksowmin
Finally I got! Just to share, there are more config than just .env
我终于得到了!只是为了分享,还有比 .env 更多的配置
This is .env
这是 .env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=********
MAIL_ENCRYPTION=tls
config/mail.php also need to input address & name for it to work.
config/mail.php 还需要输入地址和名称才能工作。
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => '[email protected]' , 'name' => 'YourName' ],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
回答by Nabil
If you ever have the same trouble and try everything online and it doesn't work, it is probably the config cache file that is sending the wrong information. You can find it in bootstrap/cache/config.php. Make sure the credentials are right in there. It took me a week to figure that out. I hope this will help someone someday.
如果您曾经遇到同样的问题并在线尝试所有方法但它不起作用,则可能是配置缓存文件发送了错误的信息。您可以在 bootstrap/cache/config.php 中找到它。确保凭据在那里。我花了一个星期才弄明白。我希望有一天这会对某人有所帮助。
回答by Bhupesh Shrestha
For development purpose https://mailtrap.io/provides you with all the settings that needs to be added in .env file. Eg:
出于开发目的,https://mailtrap.io/ 为您提供了需要在 .env 文件中添加的所有设置。例如:
Host: mailtrap.io
Port: 25 or 465 or 2525
Username: cb1d1475bc6cce
Password: 7a330479c15f99
Auth: PLAIN, LOGIN and CRAM-MD5
TLS: Optional
Otherwise for implementation purpose you can get the smtp credentials to be added in .env file from the mail (like gmail n all)
否则,出于实施目的,您可以从邮件中获取要添加到 .env 文件中的 smtp 凭据(如 gmail n all)
After addition make sure to restart the server
添加后一定要重启服务器
回答by Advaith
It happens to me also.
它也发生在我身上。
It is because when we edit the
.env
file we need to restart the server. Just stop the currentphp artisan serve
and start it once again. This will work for sure.
这是因为当我们编辑
.env
文件时,我们需要重新启动服务器。只需停止电流php artisan serve
并再次启动即可。这肯定会起作用。
If still not worked try doing php artisan config:cache
如果仍然没有工作尝试做 php artisan config:cache
回答by GabMic
For future reference to people who come here.after you run the command that was given in the third answer here(I am using now Laravel 5.3).
供以后来这里的人参考。在您运行此处第三个答案中给出的命令后(我现在使用的是 Laravel 5.3)。
php artisan config:cache
You may encounter this problem:
你可能会遇到这个问题:
[ReflectionException]
Class view does not exist
In that case, you need to add it back manually to your provider list under the app.php file. GO-TO:
app->config->app.php->'providers[]'
and add it, like so:
在这种情况下,您需要手动将其添加回 app.php 文件下的提供程序列表中。转到:
app->config->app.php->'providers[]'
并添加它,如下所示:
Illuminate\View\ViewServiceProvider::class,
Hope That helps someone.
希望这对某人有帮助。
回答by mohaddese abbasi
I've had the same problem; my MAIL_ENCRYPTION
was tls
on mail.php
but it was null
on .env
file.
So I changed null
to tls
and it worked!
我遇到了同样的问题;我MAIL_ENCRYPTION
是tls
上mail.php
,但它是null
在.env
文件中。
所以我改成null
了tls
,它奏效了!