Community Documentation

Simplenews: Personalize the footer message

Last updated July 24, 2012. Created by pillarsdotnet on October 14, 2008.
Edited by authentictech, Aaron Stanush. Log in to edit this page.

I use Simplenews with Mime Mail on several Drupal-6 sites. Recently, I decided to improve the footer message, which defaults to a simple "Unsubscribe from this newsletter" link.

The good news is that the simplenews module already supports a theme override for the footer. The bad news is that the only information available to the theme override function is:

$format
Text string: either 'html' or 'plain'. No other possible values.
$hash
Text string: cryptographic hash used for unsubscribe links.
$test
Boolean: TRUE if this is a test send to the test email address; FALSE for a real newsletter sending.
$language
Text string: translation language (or NULL if translations are not supported)

Obviously, this isn't going to be very helpful in creating a personalized, informative footer message. But wait! A further examination of the code reveals that the $hash variable is composed of four parts:

  1. A ten-character checksum
  2. The subscriber ID from the {simplenews_subscriptions} table.
  3. The letter "t" as a delimiter
  4. The term id from the {term_data} table.

From this, I can determine the subscriber email address, the newsletter name, and description. If the subscriber is a registered user, I can also get the username and other profile information. To do this, I added a function to my template.php file as follows:

<?php
function phptemplate_get_newsletter_data($hash) {
 
$data = explode('t',$hash);
 
$tid = $data[1];
 
$newsletter = db_fetch_array(db_query('SELECT name,description FROM {term_data} WHERE tid=%d',$tid));
 
$newsletter['path'] = drupal_get_path_alias("taxonomy/term/$tid");
 
$snid = substr($data[0],11);
 
$subscription = db_fetch_array(db_query('SELECT mail,uid FROM {simplenews_subscriptions} WHERE snid=%d',$snid));
 
$newsletter['mail'] = $subscription['mail'];
  if (
$subscription['uid']) {
   
$newsletter['user'] = user_load($subscription['uid']);
  }
  return
$newsletter;
}
?>

Then I created a template file in my theme directory called "simplenews-newsletter-footer.tpl.php" like the following:

<?php if (!$test):
 
$newsletter = phptemplate_get_newsletter_data($hash);
  global
$base_root;
  global
$base_url;
  if (
$format == 'html'): ?>

<p class="newsletter-footer">
  You received this because your email address <?php print $newsletter['mail'] ?>
  is subscribed to the
  <a href="<?php print "$base_root/$newsletter[path]"; ?>"><?php print $newsletter['name']; ?> Newsletter</a>.
  <br /><br />
  <?php print $newsletter['description']; ?>
  <br /><br />
  <a href="<?php print "!confirm_unsubscribe_url"; ?>">To unsubscribe, click here.</a>
  <br /><br />
  <a href="<?php print "$base_url/newsletter/subscriptions"; ?>">To manage your subscriptions, click here.</a>
</p>
<?php else: ?>

--
You received this because your email address <?php print $newsletter['mail'] ?>
is subscribed to the <?php print $newsletter['name']; ?> Newsletter at
<?php print "$base_root/$newsletter[path]"; ?>

<?php print $newsletter['description']; ?>

To unsubscribe from this newsletter, you may use the following link:

     <?php print "!confirm_unsubscribe_url"; ?>

To manage your subscriptions on this website, go here:

     <?php print "$base_url/newsletter/subscriptions"; ?>
<?php endif; endif; ?>

After adding the template file, I had to visit the admin/settings/performance page to clear the cache and rebuild the theme registry.

Now my Newsletter emails have personalized footer messages.

Comments

Odd, for some reason (on

Odd, for some reason (on Drupal 6):

<?php print "$base_url/newsletter/confirm/remove/$hash"; ?>

The $hash did not work but below worked fine for me:

<a href="!confirm_unsubscribe_url">To unsubscribe, click here.</a>

Placed in the *.tpl.php file.

Anyway, thanks for the writeup, it was very useful.

unsubscribe link

Wow, I didn't know that would work. I like it. Your solution doesn't rely on my particular URL-addressing setup.

(editing the original article because I like your way better...)

Thanks!

The web is like usenet, but
the elephants are untrained.

Good. — Fast. — Cheap.
(Pick any two.)

Actually, only after writing

Actually, only after writing the above. I realised in Drupal 6 all one need to do is copy the *.tpl.php file from simplenews module and place it into your theme directory and it should just render the simplenews template from your theme directory instead.

There are two template files:

simplenews-newsletter-body.tpl.php
simplenews-newsletter-footer.tpl.php

Open up those files and read the instructions. The variables from the add/edit form of simplenews can be used in those files too. It's a lot more straightforward and you can also style the body part as well using simplenews-newsletter-body.tpl.php.

Remember, use inline CSS or plain old HTML4, most if not all email services (Gmail, Hotmail, Yahoo and etc.) don't like any CSS being specified in the HTML doc, simply because this might cause the CSS to also change the styling of the UI (or webpage) of these email services so to prevent this happening it strips them out.

The versions i used are Drupal 6.8 and simplenews-6.x-1.0-rc1

However, i'm not sure about Drupal 5 though.

Thanks,

Fatal Error

Hello,

unfortunately i get a error after insert all the code in the tpl files!

Fatal error: Call to undefined function phptemplate_get_newsletter_data() in .../sites/all/modules/test/simplenews-6.x-1.0-rc6/simplenews/simplenews-newsletter-footer.tpl.php on line 28

can you help me pls?

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.