Turn Web HTML Into Newsletter

  1. This how to includes
  2. Creation of a newsletter command line script
  3. Setting up a cron to send the newsletter monthly
  4. Final words

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.

6 0
9 followers
Viewed: 16 920 times
Version: 1.1
Category: How-tos
Written by: yasen
Last updated by: yasen
Created on: Jan 20, 2013
Last updated: 11 years ago
Update Article

Revisions

View all history

Related Articles