Hi, thank you for your wonderful module!

I see that !subscriber_name works perfectly in the body of the newsletter but do not get a value in the title of the received mail (for anonomous users) when adding:

I've tried both: !subscriber_name or [simplenews-receiver-realname]

Is the realname perhaps not available in the title of mail?

Most appreciated :)

CommentFileSizeAuthor
#2 simplenews_realname.module.zip5.21 KBxool
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Liliplanet’s picture

Issue summary: View changes
xool’s picture

To get the !subscriber_name work in the title you need to

change the original "Implementation of hook_mail_alter()." function:

/**
 * Implementation of hook_mail_alter().
 */
function simplenews_realname_mail_alter(&$message) {
  if (strpos($message['id'], 'simplenews') !== FALSE) {
    $realname = _simplenews_realname_get_realname($message['to']);
    // Replace token in the message body.
    $variables = array('!subscriber_name' => $realname);
    if (is_array($message['body'])) {
      foreach ($message['body'] as $key => $value) {
        $message['body'][$key] = strtr($value, $variables);
      }
    }
    else {
      $message['body'] = strtr($message['body'], $variables);
    }
    // Replace receiver's name.
    if (isset($message['params']['context']['account'])) {
      $message['params']['context']['account']->name = $realname;
    }
    // Windows based PHP systems don't accept formatted emails.
    if (substr(PHP_OS, 0, 3) !== 'WIN') {
      $message['to'] = '"' . mime_header_encode($realname) . '" <' . $message['to'] . '>';
    }
  }
}

with the following code that includes a function to alter the mail subject too:

/**
 * Implementation of hook_mail_alter().
 */
function simplenews_realname_mail_alter(&$message) {
  if (strpos($message['id'], 'simplenews') !== FALSE) {
    $realname = _simplenews_realname_get_realname($message['to']);
    // Replace token in the message body.
    $variables = array('!subscriber_name' => $realname);
    if (is_array($message['body'])) {
      foreach ($message['body'] as $key => $value) {
        $message['body'][$key] = strtr($value, $variables);
      }
    }
    else {
      $message['body'] = strtr($message['body'], $variables);
    }

		if (is_array($message['subject'])) {
      foreach ($message['subject'] as $key => $value) {
        $message['subject'][$key] = strtr($value, $variables);
      }
    }
    else {
      $message['subject'] = strtr($message['subject'], $variables);
    }
    // Replace receiver's name.
    if (isset($message['params']['context']['account'])) {
      $message['params']['context']['account']->name = $realname;
    }
    // Windows based PHP systems don't accept formatted emails.
    if (substr(PHP_OS, 0, 3) !== 'WIN') {
      $message['to'] = '"' . mime_header_encode($realname) . '" <' . $message['to'] . '>';
    }
  }
}

Attached is zip file with updated simplenews_realname.module, just change it with the original one and you'll have it working. Tested with simplenews 7.x.1.1 and simplenews_realname 7.x-1.0-alpha2