blog-banner

Make Confirmation Emails From Simplenews Obey Newsletter Settings

  • Drupal 7
  • Drupal Planet
  • Newsletter

Newsletter Confirmation Email

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 newsletters 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 having a 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 the 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 a 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 a specific address for each newsletter category. Hope this will help you to get the solution.

 

Get awesome tech content in your inbox