totymedli
totymedli

Reputation: 31106

Laravel mail pretend prints nothing

I set 'pretend' => true, in the mail.php, created this new.php view:

<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] [] []

I tried with a full HTML view, also with another view that contains only strings, no variables, but the output is the same.

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?

Upvotes: 28

Views: 14373

Answers (5)

Daniel A.A. Pelsmaeker
Daniel A.A. Pelsmaeker

Reputation: 50336

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:

[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:

[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!

 [] []

Upvotes: 66

redsd
redsd

Reputation: 111

I created a package to also include subject and body for laravel 4.2.

The package can be found here: https://packagist.org/packages/peercode/mail

Just enable the package as descripted here: https://github.com/peercode-eric/laravel-maillog and the log will contain the additional information.

Upvotes: 1

foolishoptimist
foolishoptimist

Reputation: 106

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.

Upvotes: 8

sirtimbly
sirtimbly

Reputation: 230

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

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.

Upvotes: 10

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87769

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:

/**
 * 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}");
}

Upvotes: 8

Related Questions