Simplenews: Personalize the footer message

Last updated on
12 September 2019

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:

 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.

Help improve this page

Page status: No known problems

You can: