I would like to create a custom class that will generate an HTML email. I want the content of the email to come from an "email view scripts" directory. So the concept will be that I can create an HTML email view script the same way I would create a normal view script (being able to specify class variables, etc), and the view script would be rendered as the HTML body of the email.
For example, in the controller:
$email = My_Email::specialWelcomeMessage($toEmail, $firstName, $lastName);
$email->send();
The My_Email::specialWelcomeMessage()
function would do something like this:
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));
}
Ideally, it would be best if I could find a way to make the specialWelcomeMessage()
function act as easy as this:
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
}
Which would then render the special-welcome-message.text.phtml and the special-welcome-message.html.phtml scripts:
<p>Thank you <?php echo $this->firstName; ?> <?php echo $this->lastName; ?>.</p>
How would I call the partial view helper from outside of a view script or controller? Am I approaching this the right way? Or is there a better solution to this problem?
What about:
public static function specialWelcomeMessage($toEmail, $firstName, $lastName) {
$view = new Zend_View;
$view->setScriptPath('pathtoyourview');
$view->firstName = $firstName;
$view->lastName = $lastName;
$content = $view->render('nameofyourview.phtml');
$mail = new Zend_Mail();
$mail->setTo($toEmail);
$mail->setFrom($this->defaultFrom);
$mail->setTextBody($content);
}
If you wanted to dynamically change the script path with your action names like you said, why not use get the action name or controller you're calling and send it as a variable, or better still a default parameter. This will help:
http://framework.zend.com/manual/en/zend.controller.request.html