Laravel 邮件假装什么都不打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18532353/
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
Laravel mail pretend prints nothing
提问by totymedli
I set 'pretend' => true,
in the mail.php
, created this new.php
view:
我'pretend' => true,
在 中设置mail.php
,创建了这个new.php
视图:
<body>
<div>
E-mail: {{ $user->email }}<br>
User: {{ $user->username }}<br>
Pass: {{ $user->password }}<br>
</div>
</body>
Then in my controller I use this code to "send" the mail:
然后在我的控制器中,我使用此代码“发送”邮件:
$data['user'] = $user;
Mail::send('emails.new', $data, function($message) use ($user)
{
$message->to('[email protected]', $user->username)->subject('Account');
});
The output in the log file is only this:
日志文件中的输出只有这样:
[2013-08-30 11:27:56] log.INFO: Pretending to mail message to: [email protected] [] []
[2013-08-30 11:27:56] log.INFO:假装将邮件发送至:[email protected] [] []
I tried with a full HTML view, also with another view that contains only strings, no variables, but the output is the same.
我尝试了一个完整的 HTML 视图,还有另一个只包含字符串、没有变量的视图,但输出是一样的。
Is this the way how this should work? Shouldn't it print the whole message, title, etc? Is there a problem with the code or this is the proper output?
这是应该如何工作的方式吗?它不应该打印整个消息、标题等吗?代码有问题还是这是正确的输出?
回答by Daniel A.A. Pelsmaeker
If you set 'pretend' => true
in app/config/mail.php
then no mail is ever sent, you get just a message in the log, like this:
如果您设置'pretend' => true
,app/config/mail.php
则不会发送任何邮件,您只会在日志中收到一条消息,如下所示:
[2014-07-17 14:15:07] production.INFO:
Pretending to mail message to: [email protected] [] []
However, if you leave 'pretend' => false
and instead use the log
driver ('driver' => 'log'
, available since Laravel 4.2), then instead of sending the mail, you'll get the whole mail content written into the log:
但是,如果您离开'pretend' => false
并使用log
驱动程序('driver' => 'log'
从 Laravel 4.2 开始可用),那么您将不会发送邮件,而是将整个邮件内容写入日志:
[2014-07-17 14:15:14] production.DEBUG:
Message-ID: <[email protected]>
Date: Thu, 17 Jul 2014 14:15:15 +0000
Subject: Welcome!
From: Ahmad <[email protected]>
To: John Smith <[email protected]>
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Order confirmed!
[] []
回答by sirtimbly
If you actually want to view the contents of the message (for instance when testing user account verification or password reset emails) in the /storage/logs logfile; You can modify your local copy of vendor/laravel/framework/src/Illuminate/Mail/Mailer.php and in the logMessages function modify it to look like
如果您确实想查看 /storage/logs 日志文件中的消息内容(例如在测试用户帐户验证或密码重置电子邮件时);您可以修改 vendor/laravel/framework/src/Illuminate/Mail/Mailer.php 的本地副本,并在 logMessages 函数中将其修改为
protected function logMessage($message)
{
$emails = implode(', ', array_keys((array) $message->getTo()));
$body = $message->getBody();
$this->logger->info("Pretending to mail message to: {$emails} :-: {$body}");
}
Then you will see the body of the message in the logs.
然后您将在日志中看到消息的正文。
回答by foolishoptimist
I include this in my send callbacks:
我将其包含在我的发送回调中:
Mail::send(array('emails.html','emails.text'),$data,function($message) use($data)
{
//Set $message data
if(Config::get('mail.pretend')) Log::info(View::make('emails.html',$data)->render());
}
Alternately, you can pass the views render into another view to see the html rendered in your browser.
或者,您可以将视图渲染传递到另一个视图以查看在浏览器中渲染的 html。
回答by Antonio Carlos Ribeiro
This is the normal behaviour of pretend in the Laravel Mailer system. It will not render your message anywhere, not even in the log, it will just log that a mail message was pretended to be sent. Look at the related source code:
这是 Laravel Mailer 系统中伪装的正常行为。它不会在任何地方呈现您的消息,甚至不在日志中,它只会记录假装发送的邮件消息。看相关源码:
/**
* Send a Swift Message instance.
*
* @param Swift_Message $message
* @return void
*/
protected function sendSwiftMessage($message)
{
if ( ! $this->pretending)
{
return $this->swift->send($message);
}
elseif (isset($this->logger))
{
$this->logMessage($message);
}
}
/**
* Log that a message was sent.
*
* @param Swift_Message $message
* @return void
*/
protected function logMessage($message)
{
$emails = implode(', ', array_keys($message->getTo()));
$this->logger->info("Pretending to mail message to: {$emails}");
}
回答by redsd
I created a package to also include subject and body for laravel 4.2.
我创建了一个包,还包含 laravel 4.2 的主题和正文。
The package can be found here: https://packagist.org/packages/peercode/mail
该包可以在这里找到:https: //packagist.org/packages/peercode/mail
Just enable the package as descripted here: https://github.com/peercode-eric/laravel-maillogand the log will contain the additional information.
只需按照此处的描述启用包:https: //github.com/peercode-eric/laravel-maillog,日志将包含附加信息。