You are here

public function NewsletterMailSystem::addNewsletterUrlQuery in Newsletter 7.2

Same name and namespace in other branches
  1. 7 includes/newsletter.mail.inc \NewsletterMailSystem::addNewsletterUrlQuery()

Add the source and nnid to newsletter URLs so we can gather clicks.

1 call to NewsletterMailSystem::addNewsletterUrlQuery()
NewsletterMailSystem::format in includes/newsletter.mail.inc
Format a message composed by drupal_mail() prior sending.

File

includes/newsletter.mail.inc, line 16
Class file for MailSystemInterface implementation.

Class

NewsletterMailSystem
Modify the drupal mail system to send HTML emails.

Code

public function addNewsletterUrlQuery($message) {
  if (isset($message['params']['newsletter']->nnid)) {
    global $base_url;
    $nnid = $message['params']['newsletter']->nnid;
    $dom = new DomDocument();

    // Convert to html entities to avoid text scrumbling
    // due to encoding especially for non-latin characters.
    $message['body'] = mb_convert_encoding($message['body'], 'HTML-ENTITIES', 'UTF-8');
    @$dom
      ->loadHTML($message['body']);
    $links = $dom
      ->getElementsByTagName('a');
    foreach ($links as $link) {
      $href = $link
        ->getAttribute('href');
      if (strpos($href, $base_url) !== FALSE) {
        $href = drupal_parse_url($href);
        $query = array_merge($href['query'], array(
          'source' => 'newsletter',
          'nnid' => $nnid,
        ));
        $link
          ->setAttribute('href', url($href['path'], array(
          'query' => $query,
        )));
      }
    }

    // Converts back from hmtl entities
    return html_entity_decode($dom
      ->saveHTML());
  }
  return $message['body'];
}