开发者

Zend Framework email content generation

开发者 https://www.devze.com 2022-12-17 18:44 出处:网络
With Zend_Framework, I wondered what is considered best practice for building up the content to send in a HTML email. In my case the content of the email that is sent is determined by a number of fact

With Zend_Framework, I wondered what is considered best practice for building up the content to send in a HTML email. In my case the content of the email that is sent is determined by a number of factors, for example the number of returned rows for a specific value in the database. Because of this it makes sense for me that the content is built up within the controller that sends the email which talks to the relevant database models and determines what the content should be. Where i'm not sure this works is that our designers and copyrighters will often want to adjust the copy in emails and this would then require them to make changes to a model or ask me to. Should i be handling this differently? Should i perhaps be storing HTML snippets somewhere containing the different text and then calling these somehow?

EDIT following from the answer by fireeyedboy, would it be acceptable to do something like this. Create a folder inside views called "partials" and use this to store text/html snippets that i can then call in where i need and replace special strings with dynamic values using regexp(or similar).

$nview = new Zend_View();
$nview->setScriptPath(APPLICATION_PATH.'/views/partials/');
$bodytext = $nview->render('response.phtml');
$mail = new Zend_Mail();
$mail->setBodyText($bodytext);
// etc ...

e.g. in this context where two different templates could be used depending on variables returned from a Model:

// within a controller
public function emailAction()
{
    $images = new Model_ApplicationImages();
    $totimages = count($images->fetchImages($wsid));
    $acceptedImages = $images->fetchImages($wsid,'approved');
    $accepted = count($acceptedImages);
    $rejectedImages = $images->fetchImages($wsid,'rejected');
    $rejected = count($rejectedImages);
    $response = ($rejected == $totimages)?'rejected':'approved';
    $nvie开发者_如何学运维w = new Zend_View();
    $nview->setScriptPath(APPLICATION_PATH.'/views/partials/');
    $content = $nview->render($response.'.phtml');
    $mail = new Zend_Mail();
    $mail->setBodyText($content);
    // etc
}

Is there a more elegant way i can/should be doing this?


Not sure if this is best practice, but what I did is extend Zend_Mail with methods like these:

setTemplatePath( $templatePath );
setTemplateHtml( $templateHtml );
setTemplateText( $templateText );
setTemplateArguments( array $templateArguments );

...then at some point in my overwrittensend() I do:

$view = new Zend_View();
$view->setScriptPath( $this->_templatePath );

foreach( $this->_templateArguments as $key => $value )
{
    $view->assign( $key, $value );
}

if( null !== $this->_templateText )
{
    $bodyText = $view->render( $this->_templateText );
    $this->setBodyText( $bodyText );
}

if( null !== $this->_templateHtml )
{
    $bodyHtml = $view->render( $this->_templateHtml );
    $this->setBodyHtml( $bodyHtml );
}

So to utilize this you would do something like:

$mail = new My_Extended_Zend_Mail();
$mail->setTemplatePath( 'path/to/your/mail/templates' );
$mail->setTemplateHtml( 'mail.html.phtml' );
$mail->setTemplateText( 'mail.text.phtml' );
$mail->setTemplateArguments(
    'someModel' => $someFunkyModel,
    /* etc, you get the point */
)
$mail->send();

In other words, with this you can let your designers and copywriters simply edit views (templates) like they are used to already. Hope this helps and has inspired you to come up with something funky that suits your needs.

PS:
Since you mention arbitrary data rows, you can, for instance, utilize the partialLoop view helper that comes with ZF for this. But you probably were aware of this already?

PPS:
I actually agree with chelmertz' comment about not extending Zend_Mail but wrapping it in my own component.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号