php 试图让电子邮件在 Laravel 5 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29083520/
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
Attempting to get Email to work in Laravel 5
提问by Vantheman6
Well let me first start out by saying I'm pretty new to laravel 5. I've been searching forever on google trying to get just a simple email to send just by typing in the appropriate URL with no luck. Unfortunately, the documentation out there I have found wasn't that helpful, and just gives a broad look(I understand laravel 5 is new, but still frustrating haha). There is nothing fancy about what I'm trying to do, I just want to get that too work before I do anything else. I'm trying to get this to work with just using gmail as of right now, but once I get that down I will of course, try something like Mailgun. This is the code I have as of right now The first one is in mail.php:
好吧,让我首先说我对 laravel 5 还很陌生。我一直在 google 上搜索了很久,试图通过输入适当的 URL 来发送一封简单的电子邮件,但没有运气。不幸的是,我发现的文档并没有那么有用,只是提供了一个广泛的外观(我知道 laravel 5 是新的,但仍然令人沮丧,哈哈)。我正在尝试做的事情并没有什么特别之处,我只是想在做任何其他事情之前先完成它。我现在正在尝试使用 gmail 来解决这个问题,但是一旦我解决了这个问题,我当然会尝试使用 Mailgun 之类的东西。这是我现在的代码第一个在mail.php中:
return [
/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "log"
|
*/
'driver' => env('smtp'),
/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/
'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', 587),
/*
|--------------------------------------------------------------------------
| 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' =>"[email protected]" , 'name' => "example_name"],
/*
|--------------------------------------------------------------------------
| 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' => 'tls',
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/
'username' => env('[email protected]'),
/*
|--------------------------------------------------------------------------
| SMTP Server Password
|--------------------------------------------------------------------------
|
| Here you may set the password required by your SMTP server to send out
| messages from your application. This will be given to the server on
| connection so that the application will be able to send messages.
|
*/
'password' => env('example'),
/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/
'sendmail' => '/usr/sbin/sendmail -bs',
/*
|--------------------------------------------------------------------------
| Mail "Pretend"
|--------------------------------------------------------------------------
|
| When this option is enabled, e-mail will not actually be sent over the
| web and will instead be written to your application's logs files so
| you may inspect the message. This is great for local development.
|
*/
'pretend' => false,
];
This is in my routes:
这是在我的路线:
Route::get('test', function()
{
Mail::send('Email.test', function ($message)
{
$message->to('[email protected]', 'example_name')->subject('Welcome!');
});
});
I also tried the MailController@Sending_Email
for the path as well.
This is in my MailController:
我也尝试了MailController@Sending_Email
for 路径。这是在我的 MailController 中:
class MailController extends Controller{
public function Sending_Email()
{
$this->call('GET','Email.test');
return View('Email.test');
}
}
and my view is this simple code:
我的观点是这个简单的代码:
<html>
<head>
</head>
<body>
<h1>hey this is a test to see if my email system works</h1>
</body>
</html>
This is my error:
Missing argument 1 for Illuminate\Support\Manager::createDriver(), called in /vagrant/leonis/vendor/laravel/framework/src/Illuminate/Support/Manager.php on line 89 and defined
这是我的错误:
Missing argument 1 for Illuminate\Support\Manager::createDriver(), called in /vagrant/leonis/vendor/laravel/framework/src/Illuminate/Support/Manager.php on line 89 and defined
采纳答案by jszobody
You have a couple issues going on. First off:
你有几个问题。首先:
'driver' => env('smtp'),
The env
method looks to your .env file. My guess is you don't have a "smtp" entry in your .env file. I would simply change it to this:
该env
方法查看您的 .env 文件。我的猜测是您的 .env 文件中没有“smtp”条目。我只想把它改成这样:
'driver' => 'smtp',
That should take care of the createDriver()
error.
那应该处理createDriver()
错误。
If you still have issues with the driver, or later on have issues authenticating to your SMTP server, do a quick check of your config at runtime:
如果驱动程序仍有问题,或者稍后在验证 SMTP 服务器时遇到问题,请在运行时快速检查您的配置:
dd(Config::get("mail"));
Since you have env()
checking for .env settings and then falling back to default values, it can be helpful to see what the generated config looks like.
由于您env()
检查了 .env 设置,然后回退到默认值,因此查看生成的配置是什么样的会很有帮助。
Now you still have an issue with how you are calling Mail::send
. This is your code:
现在您仍然对如何调用Mail::send
. 这是你的代码:
Mail::send('Email.test', function ($message)
And this is from the Laravel documentation:
这是来自Laravel 文档:
Mail::send('emails.welcome', ['key' => 'value'], function($message)
Notice that the second argument is an array. The callback function should be the thirdargument.
请注意,第二个参数是一个数组。回调函数应该是第三个参数。
From the docs:
从文档:
The second is the data to be passed to the view, often as an associative array where the data items are available to the view by $key.
第二个是要传递给视图的数据,通常作为关联数组,其中数据项可通过 $key 提供给视图。
So do something like this:
所以做这样的事情:
Mail::send('Email.test', [], function ($message) {
$message->to('[email protected]', 'example_name')->subject('Welcome!');
});
回答by Harry Bosh
You may need to
你可能需要
php artisan config:clear
then
然后
php artisan config:cache