2

我对使用 Cake 的 (2.3) 电子邮件类有点困惑。看来我们能够定义“模板”、“布局”和“主题”,而我只了解布局的用法(位于 /app/View/Layouts/Emails 中)。

似乎一切都可以在布局中定义,但模板似乎确实是必要的(至少是一个空文件),但我不明白上下文,因为对我来说,我放在那里似乎并不重要。

主题的概念对我来说更加模糊。也许有人可以在这里给我一个提示。我在一个邮件列表中发现了一个讨论,这并不是真正有启发性。文档也没有透露这一点。

http://book.cakephp.org/2.0/en/core-utility-libraries/email.html

--
编辑:修正了令人困惑的错字。
Edit2:直接使用 CakeEmail - 不是组件。

4

1 回答 1

3

模板是视图(就普通页面而言)电子邮件的布局是视图的布局(就普通页面而言)

布局应该包含一些常见的元素,如标志等

您可以将数据推送到模板,例如推送数据以从控制器查看

请检查以下示例:

来自自定义 EmailComponent

public function restore_password($user_to_send_restore_link) {
    $email = new CakeEmail('default');
    $email->emailFormat('both');
    $email->template('restore_password', 'emaillayout');

    $email->to(array($user_to_send_restore_link['User']['email']));
    $email->from(array(GENERAL_FROM_EMAIL => 'seqrd support team'));
    $subject = 'Restore password link';
    $email->subject($subject);

    $email_data = array(
        'hash' => $user_to_send_restore_link['User']['hash']);
    $email->viewVars($email_data);

    return $email->send();
}

应用程序/查看/电子邮件/html/restore_password.ctp

<p> Please, follow link <?php echo $this->Html->link('restore password link', Router::url(array('controller' => 'users', 'action' => 'restore_password_form', $hash), true)); ?> to restore password</p>

应用程序/视图/布局/电子邮件/html/emaillayout.ctp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
    <title><?php echo $title_for_layout;?></title>
</head>
<body>
    <?php echo $this->fetch('content');?>

</body>
</html>

主题是抽象的下一步,您可以快速更改所有电子邮件的整体样式,但不会显着更改代码。

注意: viewVars方法不仅将变量传递给模板,还传递给电子邮件布局。

于 2013-07-18T09:20:53.257 回答