views:

437

answers:

3

I am working on a website where Users create their accounts. I need to send email to users on many oceans. For example when signup, forgot password, order summary etc. I want to use emails templates for this. I need your suggestions for this. I want to use a way that If I change any email template or login in less time and changes.

I have thought about the following way:

I have a table for email templates like this:

id
emailSubject
emailBody
emailType

For example when user forgot password:

id:

1

emailSubject:

ABC: Link for Password Change

emailBody:

<table border=0>
<tr><td>
   <b> Dear [[username]] <b/>
</td></tr>

<tr><td>
   This email is sent to you in response of your password recovery request. If you want to change your password, please click the following link:<br />
[[link1]]
<br/>
If you don't want to change your password then click the following link:<br/>
[[link2]]
</tr></td>

<tr><td>
   <b>ABC Team</b>
</td></tr>

</table>

emailType:

ForgotPassword

Prepare email data:

$emailFields["to"] = "[email protected]";
$emailFields["username"] = "XYZ";
$emailFields["link1"] = "http://abc.com/changepassword?code='123'";
$emailFields["link2"] = "http://abc.com/ignorechangepasswordreq?code='123'";
$emailFields["emailTemplate"] = "ForgotPassword";

Now Pass the all fields to this function to send email:

sendEmail( $emailFields ){
 // Get email template from database using emailTemplate name.
 // Replace all required fields(username, link1,link2) in body.
 // set to,subject,body
 // send email using zend or php
}

I planed to use above method. Can you suggest better way or some change in above logic.

Thanks.

+5  A: 

I'd use Zend_View. Store your templates in /views/email/EMAILNAME.phtml, create a Zend_View object with the required email template and pass it the required data.

Benedict Cohen
do you want to do like this: http://stackoverflow.com/questions/1218191/how-can-i-make-email-template-in-zend-framework
NAVEED
In this logic email template is stored in a phtml file. If system admin want to change an email template then what steps system admin have to take to perform this task? System admin have to overwrite the file on server? If template is stored in database then we can create a secure page to edit template html for system admin.
NAVEED
+1  A: 

Off the top of my head, so untested... but something similar should work:

$view = new Zend_View();
$view->setScriptPath( '/path/to/your/email/templates' );
$view->assign( $yourArrayOfEmailTemplateVariables );
$mail = new Zend_Mail();
// maybe leave out phtml extension here, not sure
$mail->setBodyHtml( $view->render( 'yourHtmlTemplate.phtml' ) );
$mail->setBodyText( $view->render( 'yourTextTemplate.phtml' ) );
fireeyedboy
A: 

As previously mentioned, Zend_View is the way. Here is how I do this:

$template = clone($this->view);
$template->variable = $value;
$template->myObject = (object)$array;
// ...
$html = $template->render('/path/filename.phtml');

Use Markdownify to covert to plain text:

$converter = new Markdownify_Extra;
$text = $converter->parserString($html);

Setup mail:

$mail = new Zend_Mail();
$mail->setBodyHtml($html);
$mail->setBodyText($text);

Then setup Transport and send.

takeshin