HTML Emails with inline CSS on SilverStripe

by frank on May 8, 2011

in SilverStripe

This is a really neat method to include CSS styles into an HTML email inline, which is apparently the best practice for HTML emails.

Instead of tediously adding inline styles Mark Guinn wrote a class that extends SilverStripe’s Email class and injects CSS rules inline automatically using Emogrifier.

This is awesome because you can keep the seperation between presentation and content, and simply integrate a stylesheet into an HTML email automatically just before sending. As a basic example without changing Mark’s code at all:

$email = new ProcessedEmail(
  $from = '',
  $to = '', 
  $subject = '', 
  $body = ''
);
 
$css = file_get_contents(Director::getAbsFile($this->ThemeDir().'/css/Email.css'));
 
$email->populateTemplate(
	array(
	  'InlineCSS' => "<style>$css</style>"
	)
);

Then just include $InlineCSS in the email template and the styles will be made inline automatically when ProcessedEmail-> parseVariables() is called. Very cool!

Was this article useful?

rss feed icon

Email this article to yourself or...

rss feed icon

Subscribe to the RSS feed for more useful articles and tips.

Share this article with others

  • del.icio.us
  • Twitter
  • Reddit
  • StumbleUpon
  • Facebook
  • Digg