【问题标题】:How to use email transport class "Debug" in CakePHP 3如何在 CakePHP 3 中使用电子邮件传输类“调试”
【发布时间】:2015-08-07 14:37:28
【问题描述】:

如何在 CakePHP 3 中使用电子邮件传输类“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 进行测试)

【问题讨论】:

    标签: cakephp-3.0


    【解决方案1】:

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

    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 行进入电子邮件配置文件而不是传输配置。这在文档中并不完全清楚。 另请注意,电子邮件类日志的范围为“电子邮件”。所以必须在日志配置中定义 - 否则什么都不会出现。 希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      Email::send() 方法正在返回结果。它总是返回邮件内容,只是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 选项在传输配置中启用/配置日志记录。默认设置为false

      'log':记录电子邮件标题和消息的日志级别。 true 将使用 LOG_DEBUG

      Cookbook > Email > Configuration Profiles

      【讨论】:

      • 'log' => true 对我不起作用,我仍然找不到邮件的任何日志条目。无论如何,debug($result) 工作,谢谢。但我认为这并不是很有用,因为调试需要进行很多更改(app.php 中的双重配置,在控制器中添加您的建议代码......)。如果我只需在调用$email = new Email('debug'); 时将控制器中的default 替换为debug 然后获取调试日志条目,那将更加有用...无论如何,谢谢!
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-25
        • 2017-11-15
        • 1970-01-01
        • 2016-12-07
        • 1970-01-01
        • 2015-12-21
        • 2011-01-01
        相关资源
        最近更新 更多