A recipe for building newsletters in Drupal
Update on April 9, 2013 - the description of Drupal 7 version of this module with links to the module files is available here
I'll start right away by saying that sending newsletters with Drupal is excruciatingly painful, and we do not recommend it at all unless you're subscriber-ship is extremely low. Many other software packages and solution providers do it much better than Drupal ever will. So this isn't about sending your newsletter, it's about how to build your newsletter for email and web delivery.
Our method is simple. Similar to modules like Simplenews we will be creating a newsletter content type that will store the contents of the newsletter. The only difference is that Drupal will not be sending the emails. We will be exporting HTML to our newsletter sending application of choice (We use CiviCRM).
The basic structure of the node depends on the structure of your newsletter. We will use our Freeform newsletter as an example. Our newsletter consists of a number of various sections almost all of which are controlled by CCK fields.
Our newsletter content types consist of:
- Text fields for section headers
- Textarea for header and footer text
- node reference fields for content
In order to make finding node references easier for our clients we usually install the Nodereference explorer module which provides a customizable view in a modal popup where the user can select which item they want from a list.
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.
This is where our custom module Newsletter Export comes in. This module works as follows, in the newsletter export settings page select the type of node you want the ability to 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.
The output can be customized in two ways. First a new Build mode will be available in the "Display Fields" section called "Newsletter", you can choose to exclude fields, or change display modes and it will be reflected in the output. The other way is to edit 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 you like. After our designer created and tested the raw HTML version of our newsletter we took that HTML placed it in the template and replaced the static text with variables from our node. Some small bits of PHP for basic logic (if, foreach, etc) was required, but that is generally allowed in templates.
The result is an easy to export HTML email ready version of our newsletter node that can be used in whatever email newsletter application you want.
Our sandbox project for Newsletter Export can be found here: http://drupal.org/sandbox/tkamen/1321154