You are here

function simplenews_statistics_mail_alter in Simplenews Statistics 6

Same name and namespace in other branches
  1. 6.3 simplenews_statistics.module \simplenews_statistics_mail_alter()
  2. 6.2 simplenews_statistics.module \simplenews_statistics_mail_alter()
  3. 7.2 simplenews_statistics.module \simplenews_statistics_mail_alter()
  4. 7 simplenews_statistics.module \simplenews_statistics_mail_alter()

Implemetation of hook_mail_alter().

Adds a hidden image to the body and counts the amount of emails send

Parameters

$message: Mail message array.

File

./simplenews_statistics.module, line 90
Gathers newsletter statistics.

Code

function simplenews_statistics_mail_alter(&$message) {
  if ($message['id'] == 'simplenews_node' && $message['params']['context']['node']->simplenews['s_format'] == 'html') {
    global $base_url;
    $body = $message['body']['body'];
    $nid = $message['params']['context']['node']->nid;
    $account = $message['params']['context']['account'];

    //expand all local links using mimemail _mimemail_url function
    require_once drupal_get_path('module', 'mimemail') . '/mimemail.inc';
    $pattern = '/(<a[^>]+href=")([^"]*)/emi';
    $body = preg_replace($pattern, '"\\1"._mimemail_url("\\2")', $body);

    // replace links
    $body = preg_replace($pattern, '"\\1"._simplenews_statistics_replace_url("\\2",$nid,$account->mail)', $body);

    // add hidden image
    $url = url('simplenews/statistics/view', array(
      'absolute' => TRUE,
      'query' => array(
        'nid' => $nid,
        'mail' => $account->mail,
      ),
    ));
    $body .= '<img src="' . $url . '" width="1" height="1">';

    // count
    if (!db_fetch_array(db_query("SELECT * FROM {simplenews_statistics} WHERE nid = %d", $nid))) {
      db_query("INSERT INTO {simplenews_statistics} (nid, send) VALUES (%d, %d)", $nid, 1);
    }
    else {
      db_query("UPDATE {simplenews_statistics} SET send = send+1 WHERE nid = %d", $nid);
    }

    // set new body
    $message['body']['body'] = $body;
  }
}