Make confirmation emails from simplenews obey newsletter settings

January 24, 2014 | 2 Comments | Submitted by Ganesan

Simplenews is one of the solid Newsletter management modules for Drupal. It allows a Drupal site to have multiple newsletters at once, nodes can be sent out as newsletter to multiple recipients on a single cron run. It plays well with HTML emails as well with Editors like WYSIWYG, Drupal mail system and other contrib modules like Mime Mail. Besides these, it allows to have specific sender name and email address per newsletter.

But the sign up and opt out confirmation email doesn't obey the sender information settings, perhaps this is by design of the module but not quite right with respect to our requirements. This blog tries to provide a simple fix for the same using Drupal's standard mail system hook.

Though we configure each newsletter in "admin/config/services/simplenews/categories/%/edit" to have specific sender name and email address, it always takes the sender name and email address from "admin/config/services/simplenews/settings" which is the global details for all the newsletters.

So to avoid this, we need to use hook_mail_alter(). The following simple code snippet will give the solution to this problem,

/**
 * Implements hook_mail_alter().
 *
 * Mail alter to change from address of subscription confirm mail.
 * By default global from address is used instead of per newsletter category.
 */
function module_name_mail_alter(&$message) {
  if ($message['id'] == 'simplenews_subscribe' || $message['id'] == 'simplenews_unsubscribe') {
    $message['from'] = $message['params']['context']['category']->from_address;
  }
}
So using this hook, we can have specific from address for each newsletter category. Hope this will help you to get the solution.

 



2 Comments

Please submit this as a patch, sounds like a useful thing to do by default. It will obviously not work for the multi-subscribe feature, but that's OK.

sivaji's picture

@Berdir, sure will do :) Thanks for the suggestion!