Page 1 of 1

email templating

Posted: Wed Apr 07, 2010 12:11 am
by s.dot
When I first started sending emails and writing php, i'd hardcode my emails into the script and send them like this:

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 here
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:

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']);
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:

Code: Select all

<?php

$email->send('sample-email', $toAddress, array('users' => array('firstname', 'lastname', 'username')));
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:

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}
:) Your thoughts? Do you do email templating? How do you do it? What's the best procedures?

Re: email templating

Posted: Wed Apr 07, 2010 1:31 am
by Christopher
That does not seem like an "email templating" system, but more a general templating system. I think it is an interesting idea to allow access to objects and arrays like you have done.

Re: email templating

Posted: Wed Apr 07, 2010 9:18 am
by alex.barylski
Agreed. Sounds like a general templating system, almost Smarty like.

I would use a template system like Smarty or PHPAS (alternative syntax) type template and just fill in the blanks dynamically and send away.

Cheers,
Alex

Re: email templating

Posted: Sun Apr 25, 2010 7:38 pm
by allspiritseve
s.dot wrote::) Your thoughts? Do you do email templating? How do you do it? What's the best procedures?
I would prefer to fetch the data before sending the message. What I've been doing is creating a template like I would for any other page, setting data as needed, and then rendering the template and passing the body as a string to the mailer. For example:

Code: Select all

$user = $userGateway->findById(1);
$email = new Template('Emails/Sample.tpl');
$email->set('user',$user);
$mailer->send($email->render());
This has the advantage that the only thing you need to modify when adding more data to the email is the template itself. (Unless you need more than a user, in which case you grab that data as well before creating the email.)