Module for emailing entire nodes?

jergason - July 20, 2009 - 21:22

We have some nodes that we would like to mail out periodically in a newsletter. Is there a good solution for just e-mailing the nodes, instead of having to create a new newsletter node and e-mailing that?

...

roopletheme - July 20, 2009 - 22:20

I'm not aware of an off-the-shelf solution for this. However, it is fairly easy to get the SimpleNews module to provide this behavior (assuming you have a decent grasp on PHP, Drupal, template files, and the SimpleNews module.) I've done this for a handful of sites with good results.

Essentially, you want to add fields (using CCK) to the SimpleNews node type that allow you to attach nodes, and then customize the node-simplenews.tpl.php template file to format the attached contents to your liking.

One way would be to simply add a node reference field to the simplenews node type, and then reference the node(s) you want to include in the newsletter. Another (slightly more complex) option would be to set up a list of nodes using the Node Queue module to designate which nodes should be displayed for each newsletter.

Great thing about this is that you still get to use the SimpleNews module features for managing subscriptions, generating emails, etc. The only real work is in customizing the node-simplenews template to display the nodes within the newsletter the way you want them.

Thanks!

jergason - July 21, 2009 - 15:40

Thank you for the answer. I had taken a look at SimpleNews originally, but dismissed it without even thinking of adding node reference fields. I will take a closer look at this.

double post

jergason - July 21, 2009 - 15:41

Overly anxious clicking.

Followup Question re: node reference fields

jergason - July 21, 2009 - 21:10

I am still trying to get this to work correctly. I have added the node reference field to my newsletter, and I made sure to change the "Display Fields" setting to include the whole node. However, when I send the newsletter, the actual e-mail only contains links to the nodes. The node created on the site displays the entire nodes correctly, however. Any idea on what might be wrong? Is there something else I need to do in the the template file? Thanks for your help.

...

roopletheme - July 21, 2009 - 22:19

Yep, you're gonna' have to do some work on the template files...

When you view a newsletter online, it goes through the 'normal' node template process. As a result, you can modify the look of the online newsletters by copying your theme's node.tpl.php file to node-newsletter.tpl.php, and then altering this template to your liking. Better yet, use something like this to create a page-newsletter.tpl.php file where you can customize the entire page layout of online newsletters. But if the newsletters already display online the way you want, you may not have to do anything here.

As for the actual emailed newsletters, you'll need to modify the simplenews template files. The important one is simplenews-newsletter-body.tpl.php. And Drupal probably won't look for this template in your theme's folder, so you'll have to modify the one in the simplenews module folder.

As for the actual changes, you're going to want to add to or replace the:

<?php
print $body;
?>

statement from the template with something else. Say, for example, that you add a node reference field called 'field_newsletter_story' to your simplenews content type, and you allow multiple values to be specified (thus allowing you to add an arbitrary number of nodes to your newsletter)... then the following code would display each of these nodes in your newsletter (with node titles as links, no less):

<?php
 
foreach ($node->field_newsletter_story as $story) {
   
$next_node = node_load($story[nid]);
   
$node_link = $GLOBALS['base_url'] . '/node/' . $next_node->nid;
    print 
'<a href="' . $node_link . '"><h2>'. $next_node->title . '</h2></a>';
    print
$next_node->body;
  }
?>

With a little bit more code, you can customize the newsletter to include multiple node types and an entirely distinct layout. I've used this method to create entirely unique page layouts for emailed newsletters.

Keep in mind that formatting HTML for email clients is a whole different world than formatting HTML for web pages. Take a look at:

http://www.sitepoint.com/article/code-html-email-newsletters/

Perfect!

jergason - July 23, 2009 - 16:25

Wow, that was exactly what I was looking for. I am just getting into theming, so it is very helpful to have someone hold my hand through this complicated process.

 
 

Drupal is a registered trademark of Dries Buytaert.