NYCPHP Meetup

NYPHP.org

[nycphp-talk] Multi-part Email Template System

Cliff Hirsch cliff at pinestream.com
Tue Sep 19 16:17:01 EDT 2006


As some of you may recall in prior threads, I have been struggling to
develop an email template methodology that achieves the following:
 
1.	Supports multi-part emails (both text and html in the same
message).
2.	Separates email content from the code as much as possible
without being a total pig
3.	Is simple and easy to modify in the future (shifting templates
to a DB, making the text part a template too, hardcoding for speed,
reducing the # of include files, etc.)
 
I'm doing this because my system generates numerous custom email
messages, each with a unique format and lots of embedded variables, and
modifying "embedded" emails is gruesome. Here's what I came up with:
 
An email directory contains a "master" Smarty html template for the main
html email look. Each email message than has two files that use the same
base name.
 
Example 'order_receipt' would have the following
 
Smarty html template: order_receipt_html.tpl
 
Text function file: order_receipt_text.php  -- this has a function named
order_receipt that returns the text body and subject line. (This was
done to accommodate order_receipt_text.tpl if warranted in the future,
but I couldn't see how to get a line break in a text template using
Smarty. Also seems like overkill to use Smarty to render a text
template.)
 
Here's the "master function":
 
static public function SendEmailTemplate($email, $template, $params) {
            $subject = '';
            $textbody = '';
            $htmlbody = '';
                        
// fetch the email text body and subject line
if (file_exists(EMAIL_TEMPLATE_DIR.$template.'_text.php')) {
 
include(EMAIL_TEMPLATE_DIR.$template.'_text.php');
                        if (function_exists($template))
                                    list($subject, $textbody) =
call_user_func($template, $params);
            }
 
            // fetch the email html body
 if (file_exists(EMAIL_TEMPLATE_DIR.$template.'_html.tpl')) {
                        $page = $GLOBALS['page']; // reference to a
global Smarty object
                        $page->assign('params', $params);
            $page->assign('emailPageContent',
EMAIL_TEMPLATE_DIR.$template.'_html.tpl');
                        $htmlbody =
$page->fetch(EMAIL_TEMPLATE_DIR.$template.'_html.tpl');
            }
 
// Send the email
            if (self::MailMime($email, $subject, $textbody, $htmlbody))
                        return true;
            else
                        return false;
}
 
My question is simply thoughts, comments? It seems like a reasonable
strategy, but I'm getting a little worried about performance. Plus the #
of includes in my system seems to be multiplying like rabbits -- the
problem of maintainability by the organic processor versus performance
for the inorganic processor.
 
Cliff
_______________________________
Pinestream Communications, Inc.
Publisher of Semiconductor Times & Telecom Trends
52 Pine Street, Weston, MA 02493 USA
Tel: 781.647.8800, Fax: 781.647.8825
 <http://www.pinestream.com/> http://www.pinestream.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20060919/9a514203/attachment.html>


More information about the talk mailing list