如何在 CakePHP 3 中使用电子邮件传输 class "Debug"

How to use email transport class "Debug" in CakePHP 3

如何在 CakePHP 3 中使用电子邮件传输 class “Debug” 查看结果(最终邮件)?或者在哪里可以找到返回的结果? the book.

中没有关于电子邮件调试的详细信息

config/app.php中说

// 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

所以我设置

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

并且在测试控制器中:

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(['website@example.com' => 'My Site'])
          ->to('destination@example.com')
          ->subject('Here the subject')
          ->send('Here the mail content'));
  }
}

但是结果(最终邮件)保存或显示在哪里?

我预计 /tmp//logs/ 中的调试结果,但在那里找不到关于最终邮件的任何信息。

如果我在浏览器 (localhost/test/email_test/) 中查看测试页,则不会显示任何内容(因为我不知道要在视图模板中添加什么以进行电子邮件调试)。 CakePHP-DebugKit 中也没有关于邮件的信息...

(如果相关的话,我目前正在使用 CakePHP 3.1 beta 对此进行测试)

Email::send() 方法正在返回结果。它始终 returns 邮件内容,只是 Debug 传输实际上并未发送它。

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

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

我想文档中的一些更多细节不会有什么坏处。

如果您想记录邮件,则必须使用 log 选项 enable/configure 登录传输配置。默认设置为 false.

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

Cookbook > Email > Configuration Profiles

我 运行 今天遇到了同样的问题,我花了一段时间才弄清楚。我认为一个完整的例子会很有用,所以这就是我结束的地方:

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' => 'sender@domain.com',

        //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'], 

    ],

    ...
],

请注意,'log' => true 行出现在电子邮件配置文件中,而不是 T运行sport 配置中。这在文档中并不完全清楚。 另请注意,电子邮件 class 的日志范围为 "email"。所以必须在日志记录配置中定义 - 否则什么也不会显示。 希望这对您有所帮助。

如果您有 debugKit,电子邮件将显示在 cakephp 调试栏中。

不幸的是,大多数情况下,在您发送电子邮件后执行重定向,因此重定向页面中的信息会丢失。很简单:调试时禁用重定向。