>/D_
Published on

HTML Emails with inline CSS on SilverStripe

Authors
  • avatar
    Name
    Frank
    Twitter

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!