email templating

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

email templating

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: email templating

Post 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.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: email templating

Post 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
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: email templating

Post 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.)
Post Reply