Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

SmartyMailer : Use Smarty and CSS stylesheets for e-mails

Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
Smarty Regular

Joined: 26 Apr 2012
Posts: 55

PostPosted: Wed Feb 06, 2013 11:07 am    Post subject: SmartyMailer : Use Smarty and CSS stylesheets for e-mails Reply with quote

Hello Smarty team & Smarty fellows,

For long I wanted to use Smarty for sending beautiful HTML e-mails, using template inheritance and so. With Smarty's Fetch() method, this is quite easy to setup.

However, designing HTML e-mails require you to forget any CSS stylesheets, and use something like <div style=""> instead : that can be very complicated if you want to manage several e-mail templates with the same design : if one day you want to change your design, you'll have to check every style attribute of every HTML element of every template.

Recently I found an amazing PHP class named "CSS to Inline Styles" by Tijs Verkoyen, that you can find here :
With that class, you can merge a HTML file with a CSS file and it will automatically output HTML with the corresponding style="" attributes everywhere after parsing your CSS rules.

For my needs, I wanted to use both Smarty and CSS to Inline styles, for designing my different e-mails with template inheritance, creating different stylesheets, and sending the processed result.

So, I wanted to share with you a class I made and I called SmartyMailer.

Now you have :

    - Templates that inherit
    - CSS that inherit
    - Compiled HTML output

Usage as follows :

$SmartyMailer   =   new SmartyMailer();
$SmartyMailer   ->   SetTemplateDir('MyTemplateDir')      // Smarty template directory
$SmartyMailer   ->   SetCssDir('MyCssDir')            // Css directory
$SmartyMailer   ->   SetTemplateFile('MyTemplate.tpl')   // Smarty template
$SmartyMailer   ->   AddCssFile('Default.css')         // Css styles
$SmartyMailer   ->   AddCssFile('MyStyles.css')         // Additionnal Css styles
$SmartyMailer   ->   Assign('FirstName', 'Ben')         // Smarty variable assignments
$SmartyMailer   ->   Process()   ->   ShowOutput()      // Processes and displays the result

$SmartyMailer   ->   SetSender('')      // Set email's sender
$SmartyMailer   ->   SetRecipient('')      // Set email's recipient
$SmartyMailer   ->   SetSubject('Hello {$FirstName}')   // Set email's subject (can be a Smarty string)
$SmartyMailer   ->   Process()   ->   Send()            // Processes and Sends the e-mail

Since SmartyMailer extends Smarty, you can use any Smarty method (SetCaching, SetCacheLifeTime, IsCached, LoadFilter, ...)

@Smarty team : I tried to do my best for good practices in coding with Smarty. Feel free to tell me what can I improve.

Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP