How to use templates or dynamic content for php email sending -


phpmailer option send email, , mail() function too, thing generating dynamic content email body, , subject not best.

for example i've created php file body templates or class same, difficult maintain.

what recomend organizing code?

is there way create email templates? (like twig). how organize folders , files? there doc recommendation that? help

it's no different you're doing in php generate dynamic html. except instead of sending generated html output via web server client ua, you're sending email ua via mta.

20 years ago thought invent templating engine generate dynamic content (it called php). turns out it's still incredibly useful today.

let's have template file looks email.

<table>     <?php foreach($rows $row) { ?>     <tr>         <?php foreach($row $column) { ?>         <td><?=$column?></td>         <?php } ?>     </tr>     <?php } ?> </table> 

let's have templating system renders these templates perhaps this.

class template {     protected $templatefile = "";     protected $templatevars = [];     public function __construct($templatefile, array $templatevars= []) {         $this->templatevars = $templatevars;         $this->templatefile = $templatefile;     }     public function __tostring() {         export($this->templatevars, extr_skip);         ob_start();         include $this->templatefile;         return ob_get_clean();     } } 

now, expand upon simple abstraction of templating bit further include things email subject line, sender email address, etc...

class sendemail {     public function __construct($to, $subject, $template, array $data) {         $template = new template($file, $data); // create email template         $this->emailbody = (string) $template; // generate content         $this->to = $to;         $this->subject = $subject;     }     public function send() {         // send email using php mailer or whatever here     } } 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -