How to use email transport class "Debug" in CakePHP 3
Asked Answered
H

3

5

How can I view the result (final mail) with the email transport class "Debug" in CakePHP 3? Or where can I find the returned result? There's no detailled information about email debugging in the book.

In config/app.php it says

// Each transport needs a `className`. Valid options are as follows:
// - Mail  : Send using PHP mail function
// - Smtp  : Send using SMTP
// - Debug : Do not send the email, just return the result

So I set

'EmailTransport' => [
  'default' => [
    'className' => 'Debug',
  ],
],

And in a test controller:

namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Network\Exception\NotFoundException;
use Cake\Mailer\Email;

class TestsController extends AppController {
  public function email_test() {
    $email = new Email('default');
    $email->from(['[email protected]' => 'My Site'])
          ->to('[email protected]')
          ->subject('Here the subject')
          ->send('Here the mail content'));
  }
}

But where is the result (final mail) saved or shown?

I expected the Debug result in /tmp/ or /logs/ but could't find any information about the final mail there.

If I view the testpage in Browser (localhost/test/email_test/) nothing is displayed (as I don't know what to add in the view template for email debugging). There's also no information about the mail in the CakePHP-DebugKit...

(I'm currently testing this with CakePHP 3.1 beta if that is relevant)

Hanker answered 7/8, 2015 at 14:37 Comment(0)
B
6

The result is being returned by the Email::send() method. It always returns the mail content, it's just that the Debug transport doesn't actually send it.

$result = $email->...->send('Here the mail content');
debug($result);

https://github.com/cakephp/.../3.1.0-beta2/src/Mailer/Transport/DebugTransport.php#L36

Some more details in the docs wouldn't hurt I guess.

If you'd wanted to have the mails logged, you'd have to enable/configure logging in the transport configuration using the log option. By default it is set to false.

'log': Log level to log the email headers and message. true will use LOG_DEBUG

Cookbook > Email > Configuration Profiles

Beitris answered 7/8, 2015 at 14:46 Comment(1)
'log' => true didn't work for me, I still couldn't find any log entries of the mail. Anyway, the debug($result) worked, thank you. But I think this is not really usefull, because the debugging requires to many changes (double configuration in app.php, add your sugested code in controller...). It would be more usefull, if I just have to replace default to debug in the controller once when calling $email = new Email('debug'); and then get a debug log entry... Anyway, thank you!Fencesitter
P
3

I ran into this same problem today and it took me while to figure out. I think a complete example would be useful, so this is where I ended up:

app.php:

/**
 * Email configuration.
 */
'EmailTransport' => [
    'default' => [

        //This disables the actual sending of the mail
        'className' =>"Debug", 

        'host' => 'smtp.office365.com',
        'port' => 587,
        'timeout' => 30,
        'username' => 'username',
        'password' => 'secret',
        'client' => null,
        'tls' => true,
        'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
    ],
],
/**
 * Email delivery profiles
 */
'Email' => [
    'default' => [
        'transport' => 'default',
        'from' => '[email protected]',

        //this enables logging of the mail
        'log' => true, 
    ],
],
/**
 * Configures logging options
 */
'Log' => [
    //Setup a special log config for mails
    'email' => [ 
        'className' => FileLog::class,
        'path' => LOGS,
        'file' => 'email', //filename for logging
        'url' => env('LOG_DEBUG_URL', null),
        'levels' => ['notice', 'info', 'debug'],

        //Set the scope to email
        'scopes' => ['email'], 

    ],

    ...
],

Note that the 'log' => true line goes in the email profile NOT the Transport config. That's not completely clear in the docs. Also note that the email class logs with the scope "email". So that MUST be defined in the logging config - otherwise nothing shows up. Hope this is helpful.

Podesta answered 16/3, 2020 at 8:54 Comment(0)
G
3

If you have the debugKit the email is shown in the cakephp debug bar.enter image description here

Unfortunately most times after you send the email you perform a redirect, so the information is lost in the redirected page. Easy enough: disable the redirect while debugging.

Giro answered 31/7, 2020 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.