cakephp 3.7 如何设置电子邮件布局和模板

cakephp 3.7 How to set email layout and template

如何设置电子邮件布局和模板

$email = new Email('default');
$email->setFrom($from)
      ->setTo('test@gmail.com')
      ->setSubject('Test email')
      ->setEmailFormat('html')
      ->viewBuilder()->setLayout('my-email-layout')
      ->setViewVars([
            'name' => Alex
       ])
       ->send('My message');

邮件打印

[protected] _viewBuilder => object(Cake\View\ViewBuilder) {
    [protected] _templatePath => null
    [protected] _template => ''
    [protected] _plugin => null
    [protected] _theme => null
    [protected] _layout => 'default'
    [protected] _autoLayout => null
    [protected] _layoutPath => null
    [protected] _name => null
    [protected] _className => 'Cake\View\View'
    [protected] _options => []
    [protected] _helpers => [
        (int) 0 => 'Html'
    ]
    [protected] _vars => []
}

我明白了。 viewBuilder 必须是最后一个参数。并且 send() 必须单独调用。

$email = new Email('default');
$email->setFrom($from)
      ->setTo('test@gmail.com')
      ->setSubject('Test email')
      ->setEmailFormat('html')
      ->setViewVars([
            'name' => Alex
       ])
      ->viewBuilder()
          ->setLayout('my-email-layout')
          ->setTemplate('default');

$email->send('My message');
$email = new Email('default');  //To load a predefined configuration

$email->setFrom(['connect@vishalmathur.in' => 'vishalmathur.in'])
            ->setTo('connect@vishalmathur.in', 'Vishal Mathur Gmail')
            ->addTo('mathurvishal@outlook.com', 'Vishal Mathur Outlook')
            ->setBcc('bcc@vishalmathur.in')
            ->setCc('cc@vishalmathur.in')
            ->setEmailFormat('html')
            ->setSubject('About test email')
            ->viewBuilder()->setTemplate('view_welcome') //use src/Template/Email/html/view_welcome.ctp
            ->setLayout('layout_fancy'); //src/Template/Layout/Email/html/fancy.ctp for the layout.

            if ($email->send('My message')){
                echo 'success';
            }
            else{
                echo 'fail';
            }