【发布时间】:2011-03-17 03:43:51
【问题描述】:
我想创建一个自定义类来生成 HTML 电子邮件。我希望电子邮件的内容来自“电子邮件视图脚本”目录。所以概念是我可以创建一个 HTML 电子邮件视图脚本,就像我创建一个普通的视图脚本(能够指定类变量等)一样,并且视图脚本将呈现为电子邮件的 HTML 正文。
例如在控制器中:
$email = My_Email::specialWelcomeMessage($toEmail, $firstName, $lastName);
$email->send();
My_Email::specialWelcomeMessage() 函数会做这样的事情:
public static function specialWelcomeMessage($toEmail, $firstName, $lastName) {
$mail = new Zend_Mail();
$mail->setTo($toEmail);
$mail->setFrom($this->defaultFrom);
$mail->setTextBody($this->view->renderPartial('special-welcome-message.text.phtml', array('firstName'=>$firstName, 'lastName'=>$lastName));
}
理想情况下,如果我能找到一种方法让specialWelcomeMessage() 函数像这样简单地运行,那将是最好的:
public static function specialWelcomeMessage($toEmail, $firstName, $lastName) {
$this->firstName = $firstName;
$this->lastName = $lastName;
//the text body and HTML body would be rendered automatically by being named $functionName.text.phtml and $functionName.html.phtml just like how controller actions/views happen
}
然后会渲染 special-welcome-message.text.phtml 和 special-welcome-message.html.phtml 脚本:
<p>Thank you <?php echo $this->firstName; ?> <?php echo $this->lastName; ?>.</p>
如何从视图脚本或控制器外部调用局部视图助手?我以正确的方式接近这个吗?或者有没有更好的办法解决这个问题?
【问题讨论】:
标签: php email zend-framework partial-views html-email