email templating
Posted: Wed Apr 07, 2010 12:11 am
When I first started sending emails and writing php, i'd hardcode my emails into the script and send them like this:
That got a bit old quickly as I'd often need to edit the emails and query for additional data to put in there. So i started saving the emails in the database and using {tags} for replacement.
I'd made a custom mailing class to handle the tags and replacing. I'd just pass the tags in like this:
This solved a lot of problems except for the need to query data and hardcode the variables and their values into the mailing function.
So, I evolved a bit. I'd use tags that were the same names as the fields in the database and pass in the corresponding table names to my function, so my emailing class could do the querying for whatever tags were in there, like so:
That worked a bit better, unless I added a new tag to the email then I'd still have to edit the code and pass in the value I wanted from the database.
so here's my new idea
I make tags like this in the emails {users|firstname}, {users|lastname}, {users|email}
This way I can match everything between {} as tags, then explode on the | and have the table name and field name to query for the value! I could even make special tags for POST, GET, COOKIE, and config, such as {.post|fieldname} {.get|fieldname} {.cookie|cookiename} and {.config|varname}. Then I could pass in special formatted values to the function and use them only when I have to.
In the end I could send an email like this without having to query for any data:
Your thoughts? Do you do email templating? How do you do it? What's the best procedures?
Code: Select all
<?php
$res = mysql_query("SELECT `firstname`, `lastname`, `username` FROM `users` WHERE `id` = 1");
$arr = mysql_fetch_assoc($res);
$text = 'hello, ' . $arr['firstname'] . '
Your last name is ' . $arr['lastname'] . '
and your username is ' . $arr['username'];
//mail hereI'd made a custom mailing class to handle the tags and replacing. I'd just pass the tags in like this:
Code: Select all
<?php
$res = mysql_query("SELECT `firstname`, `lastname`, `username` FROM `users` WHERE `id` = 1");
$arr = mysql_fetch_assoc($res);
$email->send('sample-email', array('{firsntame}', '{lastname}', '{username}'), array($arr['firstname'], $arr['lastname'], $arr['username']);So, I evolved a bit. I'd use tags that were the same names as the fields in the database and pass in the corresponding table names to my function, so my emailing class could do the querying for whatever tags were in there, like so:
Code: Select all
<?php
$email->send('sample-email', $toAddress, array('users' => array('firstname', 'lastname', 'username')));so here's my new idea
I make tags like this in the emails {users|firstname}, {users|lastname}, {users|email}
This way I can match everything between {} as tags, then explode on the | and have the table name and field name to query for the value! I could even make special tags for POST, GET, COOKIE, and config, such as {.post|fieldname} {.get|fieldname} {.cookie|cookiename} and {.config|varname}. Then I could pass in special formatted values to the function and use them only when I have to.
In the end I could send an email like this without having to query for any data:
Code: Select all
Hi {users|firstname},
Your lastname is: {users|lastname}
Your username is: {users|username}
Please login at {.config|SITE_URL}login.php
You have {messages|num_messages} awaiting!
{.config|emailsig}