试图让电子邮件在 Laravel 5 中工作
Posted
技术标签:
【中文标题】试图让电子邮件在 Laravel 5 中工作【英文标题】:Attempting to get Email to work in Laravel 5 【发布时间】:2015-05-18 23:06:00 【问题描述】:首先让我说我对 laravel 5 还很陌生。我一直在谷歌上搜索,试图通过输入适当的 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' =>"exmaple@gmail.com" , '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('example@gmail.com'),
/*
|--------------------------------------------------------------------------
| 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,
];
这是我的路线:
Route::get('test', function()
Mail::send('Email.test', function ($message)
$message->to('example@gmail.com', 'example_name')->subject('Welcome!');
);
);
我也尝试了MailController@Sending_Email
作为路径。
这是在我的 MailController 中:
class MailController extends Controller
public function Sending_Email()
$this->call('GET','Email.test');
return View('Email.test');
我的观点是这个简单的代码:
<html>
<head>
</head>
<body>
<h1>hey this is a test to see if my email system works</h1>
</body>
</html>
这是我的错误:
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
【问题讨论】:
我很想说这行得通,但不幸的是它没有。还有其他建议吗? 不,我现在得到这个 传递给 Illuminate\Mail\Mailer::send() 的参数 2 必须是数组类型,给定对象,在 /vagrant/leonis/vendor/laravel/framework/src/Illuminate/Support 中调用/Facades/Facade.php 在第 213 行并定义 你在下面看到我的回答了吗?我浏览了我认为您的多个问题,希望您在完成后会发送邮件。 你可以在 laravel 5.1 上尝试这个过程,这对我有用***.com/a/31193005/2503722 【参考方案1】:您遇到了几个问题。首先:
'driver' => env('smtp'),
env
方法查找您的 .env 文件。我的猜测是您的 .env 文件中没有“smtp”条目。我会简单地把它改成这样:
'driver' => 'smtp',
这应该会处理createDriver()
错误。
如果您的驱动程序仍然存在问题,或者稍后对您的 SMTP 服务器进行身份验证时遇到问题,请在运行时快速检查您的配置:
dd(Config::get("mail"));
由于您有 env()
检查 .env 设置然后回退到默认值,因此查看生成的配置是什么样子会很有帮助。
现在你仍然对如何调用Mail::send
有疑问。这是你的代码:
Mail::send('Email.test', function ($message)
这是来自Laravel documentation:
Mail::send('emails.welcome', ['key' => 'value'], function($message)
请注意,第二个参数是一个数组。回调函数应该是第三个参数。
来自文档:
第二个是要传递给视图的数据,通常作为关联数组,其中数据项可通过 $key 提供给视图。
所以做这样的事情:
Mail::send('Email.test', [], function ($message)
$message->to('example@gmail.com', 'example_name')->subject('Welcome!');
);
【讨论】:
好的,谢谢,我把它修好了。我从来不知道您必须将 .env 文件更改为适当的信息,. 'Mail::send('emails.welcome', ['key' => 'value'], function($message)' 是的,我看到了,并且之前和之后都尝试过。老实说,我不知道'不确切知道那在做什么。我只是想让电子邮件显示视图。假设代表的键和值是什么? 您绝对正确,因为这是我现在遇到的错误。传递给 Illuminate\Mail\Mailer::send() 的参数 2 必须是数组类型,给定对象,在 /vagrant/leonis/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php 中调用213 并定义在旁注中,非常感谢您抽出宝贵的时间提供帮助,我一直在努力尝试完成这项工作,哈哈 键/值是您可能希望传递给视图文件的数据。通常在电子邮件中,您可能会有“您好 customer 这是您的收据”之类的内容。因此,在数组中,您可能有["customer" => "John Doe"]
,以便将其填充到电子邮件中。如果您不使用它,请暂时将其设为空的 []
。
好吧,废话,然后大声笑。它一定是别的东西,因为我确实尝试过 [] 并且我仍然收到我之前发送的那个错误。看起来像这样 Route::get('test', function() Mail::send('Email.test', function ($message) $message->to('example@gmail.com', [] , 'example')->subject('Welcome!'); ); );
显示更新后的代码。如果为第二个参数放入一个空数组,则不可能得到相同的 Argument 2 passed to Illuminate\Mail\Mailer::send() must be of the type array, object given
错误。【参考方案2】:
你可能需要
php artisan config:clear
然后
php artisan config:cache
【讨论】:
只要php artisan config:clear
就足够存储我了。以上是关于试图让电子邮件在 Laravel 5 中工作的主要内容,如果未能解决你的问题,请参考以下文章
Aws ses 电子邮件发件人在本地工作,但在部署时无法在 tomcat 中工作
无法让 Laravel OrderBy 日期在 Eloquent 语句中工作