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

时间:2019-01-21 04:14:53

标签: cakephp cakephp-3.7

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

$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 => []
}

2 个答案:

答案 0 :(得分:3)

我知道了。 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');

答案 1 :(得分:0)

$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';
            }