An updated recipe for building newsletters in Drupal 7
We created the Newsletter Export module so that we could bundle Drupal website content into a newsletter. We previously blogged about the Drupal 6 version here. The module allows website managers to choose from existing website pages (any content type) which they'd like to broadcast and put it into one newsletter node for exporting. The resulting HTML content can then be imported into their usual mailing application such as CiviCRM or MailChimp.
Note that this module doesn't enable you to send your newsletter through Drupal. Neither does it allow you to maintain a list of contacts. Instead you will be required to import it into another application of your choice.
After installing the module we choose which content type we want to use as the base for our newsletters (under Configuration > Media > Newsletter Export). You can choose more than one which could be useful if you have different layouts for different newsletters. For our example we usually create a "Newsletter" content type and then choose it in the configuration.
The basic structure of the exported HTML depends on the structure of your content type you selected to be the base content. To simplify things we will refer to that content type from now on as "Newsletter" since in our example that is what we call this content type.
Our Newsletter content type consists of an introduction and article teasers. To accomplish that, we added to the Newsletter content type a textarea field (for introduction section) and an entity reference field for linking to an unlimited number of articles. This is basic Drupal content type creation and manipulation - you can select any type of fields you want here.
The "juice" of the Newletter content are the references to other site content so that they can become newsletter teasers. We often customize the views to pull just the right content to the newsletter (for example, only last month content or content of specific type). Often, we create several entity reference fields to create sections like "News", "Blog posts", "Events" etc.
After adding an entity reference field we configure the display of the newsletter content type. Under the Manage Display tab the module supplies a view mode called "newsletter" so that there can be a view mode that is separate from what is seen by viewing the website. We configure the view mode so it shows the teaser of the rendered article that is linked through the entity reference.
You will notice that we can also choose the view mode of the referenced article node. We again choose the newsletter view mode here, but that means we also have to configure the displays for the article content type.
We then create a newsletter node, let's just call it "Test Newsletter" to be boring. We also separately create a couple articles called "First article" and "Second article" so that we can reference them in the newsletter.
Once our newsletter is populated with our content we now have a published newsletter node. Awesome! Except now we need to export this to email client friendly HTML for our email blast. While testing the layout we first prefer to see the html inline for convenience so we change the newsletter export configuration to inline (under Configuration > Media > Newsletter Export). Users with appropriate permissions will then see a "Newsletter Export" link in the links area of content of that type. Clicking on the export link will generate an HTML file with the output of just that node.
Only after some inline testing do we change the settings for downloading the HTML and test it in our mass mailing application.
Our HTML output can also be customized by editing the included newsletter-export.tpl.php file that comes with the module. Copy it to your theme directory and modify the output.
The template has three variables which it can access
- $raw_markup - The standard drupal output of the node (default behaviour)
- $fields - an array of all the fields including body and title of the node
- $node - the full node object
Using these variables you can manipulate and create the output however it works best. We can usually go a long way by just using the Drupal GUI, but sometimes it requires diving into the code.
Email clients are notorious for ignoring or messing up most of the styles that work in the browsers. Instead we have to create a lot of inline css and test in a number of different email clients to ensure the newsletter looks fine. This includes major clients like Gmail, Outlook and Yahoo. One approach that might be useful is to upload the HTML into a third-party application which turns your HTML file into inline styles that will look good in most email clients. For example, Premailer, Inlinestyler.
Or you can incorporate a script like CssToInlineStyles to transform the HTML to inline styles before downloading. We explain more in our module's README file.
Our github project for Newsletter Export can be found here: https://github.com/freeform/newsletter_export