Yii 1.1: Turn Web HTML Into Newsletter

11 followers

This how to includes

  • Creation of a newsletter command line script.
  • Parsing a web page and turning its CSS into inline one via MailChimp API inlineCss() function.
  • Setting up a cron to send the newsletter monthly.

These newsletters have always been hard to create as regular web HTML is not properly read by web email interfaces and email clients. I thought that the simplest solution would be to use a web page that would serve as a web version of the newsletter and turn it somehow into newsletter HTML.

The newsletter will be sent via cron that would run on every 1st day of the month and sent mail with the products of the previous month.

Creation of a newsletter command line script

  1. Register a free account at http://mailchimp.com/ and Create an API key
  2. Download their PHP API wrapper from http://apidocs.mailchimp.com/api/downloads/mailchimp-api-class.zip and put MCAPI.class.php as MCAPI.php in protected/extensions/
  3. Create protected/commands/NewsletterCommand.php
class NewsletterCommand extends CConsoleCommand{
    public function run($args){
        define('HOST_URL','http://my-cool-yii-site.com/');
        define('MAILCHIMP_API_KEY','mailchimp-api-key-goes-here');
 
        // fetch css files content and combine it
        $cssFiles = array('/css/screen.css','/css/style.css','/css/layout.css');
        $cssFilesContent = '';
        foreach($cssFiles as $file)
            $cssFilesContent .= file_get_contents(HOST_URL.$file);
        $cssFilesContent = str_replace('url(../images','url('.HOST_URL.'images',$cssFilesContent);
        Yii::import('ext.MCAPI');
        $mcapi = new MCAPI(MAILCHIMP_API_KEY);
        $date = new DateTime();
        $date->modify('-1 month');
        // fetch the HTML from http://my-cool-yii-site.com/2013-01/
        $url = HOST_URL.$date->format('Y-m').'/';
        // replace relative paths with absolute
        foreach(array('assets','images') as $dir)
            $productsHtml =     str_replace('src="/'.$dir.'/','src="'.HOST_URL.$dir.'/',$productsHtml);
        foreach(array('css') as $dir)
            $productsHtml = str_replace('href="/'.$dir.'/','href="'.HOST_URL.$dir.'/',$productsHtml);
        // add all the CSS on top of the HTML
        $productsHtml = '<style>'.$cssFilesContent.'</style>'.$productsHtml;
        $productsHtml = $mcapi->inlineCss($productsHtml,true);
        // this email class is custom extended from phpmailer
        // add link to web version on top of the email
        Email::send($emailRecipients, 'Newsletter title', '<div align="center">If you cannot view this email properly, please open the <a href="'.$url.'">online version</div>'.$productsHtml);
    }
}

Setting up a cron to send the newsletter monthly

Finally set up cron to run on the 1st day of each month

Minute Hour Day Month Weekday Command

0 0 1 * * /home/agroofer/public_html/protected/yiic newsletter

Final words

It would be good to add a link at the bottom letting users unsubscribe, for example:

If you want to unsubscribe, please login into <?php echo CHtml::link('your profile',HOST_URL.'site/login/');?> and change the notification settings.

Another way to turn web HTML into newsletter one is manually through The MailChimp CSS Inliner Tool. Hope this solution will work for you as well. Please share any comments or suggestions. If you have any questions please direct them to the Yii Forum. Happy coding.

Update: Worth also looking at https://github.com/christiaan/InlineStyle InlineStyle provides an easy way to apply embedded and external stylesheets directly as inline styles on the HTML tags. This is especially targetted at mail clients which mostly dont support stylesheets but do support the style attribute for HTML tags.

Total 1 comment

#16739 report it
Bjorn at 2014/03/24 09:04am
InlineStyle christiaan

How can I use this InlineStyle class in Yii since it requires Symfony CssSelector?

Leave a comment

Please to leave your comment.

Write new article