What is the best way to use email template in Zend / PHP

I am working on a site where users create their accounts. I need to send email to users in many oceans. For example, during registration, you forgot your password, order summary, etc. I want to use email templates for this. I need your suggestions. I want to use a way that if I change any email template or login in less time and change.

I thought of the following:

I have a table for email templates like this:

id emailSubject emailBody emailType 

For example, when a user forgot a password:

ID:

 1 

post message:

 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"] = " user@abc.com "; $emailFields["username"] = "XYZ"; $emailFields["link1"] = "http://abc.com/changepassword?code='123'"; $emailFields["link2"] = "http://abc.com/ignorechangepasswordreq?code='123'"; $emailFields["emailTemplate"] = "ForgotPassword"; 

Now pass all the 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 planned to use the above method. Can you suggest a better way or some kind of change in the logic above.

Thanks.

+4
source share
3 answers

I would use Zend_View . Save your templates in /views/email/EMAILNAME.phtml , create a Zend_View object with the required email template and pass the necessary data to it.

+8
source

On top of my head, it’s so unverified ... but something like this 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' ) ); 
+5
source

As mentioned earlier, Zend_View is the way to go. Here is how I do it:

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

Use Markdownify for hidden text:

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

Mail setup:

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

Then configure the transport and send.

0
source

Source: https://habr.com/ru/post/1303650/


All Articles