Skip to main content
url shortner

Replacing lengthy URLs in simplenews newsletter email with Bit.ly short URLs

Simplenews, a newsletter module allows you to send customized confirmation emails on subscribe and unsubscribe actions. The default singe email confirmation for subscribe looks as below,

Subject:

Confirmation for [simplenews-category:name] from [site:name]

Body:

We have received a request to subscribe [simplenews-subscriber:mail] to the [simplenews-category:name] newsletter. To confirm please use the link below.

[simplenews-subscriber:subscribe-url]

It is known that tokens [simplenews-subscriber:mail], [simplenews-category:name],... will get replaced to respective values as needed before the email is sent out. 

For instance token [simplenews-subscriber:subscribe-url] will be replaced with https://example.com/newsletter/confirm/add/674b257f475t2 in the actual email

In our recent work, we wanted token [simplenews-subscriber:subscribe-url] to be replaced with bit.ly short url. 

https://bitly.com/ provides Web services to return the short version of any given valid URLs by making API calls. Tokens and PHP APIs combo did the trick.

First step in this process is to sign up for an account at https://bitly.com/ and create app key. Then using the same in below hook_tokens_alter() and helper function did give us the desired result.

/**
 * Implements hook_tokens_alter() 
 */
function my_module_tokens_alter(&$replacements, $context) {
  if (isset($context['type']) && $context['type'] == 'simplenews-subscriber') {
    if (isset($replacements['[simplenews-subscriber:subscribe-url]'])) {
      $subscribe_url = $replacements['[simplenews-subscriber:subscribe-url]'];
      $subscription_short = my_module_get_bitly_url($subscribe_url);
      if (valid_url($subscription_short)) {
        $replacements['[simplenews-subscriber:subscribe-url]'] = $subscription_short;
      }
    }
    if (isset($replacements['[simplenews-subscriber:unsubscribe-url]'])) {
      $unsubscribe_url = $replacements['[simplenews-subscriber:unsubscribe-url]'];
      $unsubscription_short = my_module_get_bitly_url($unsubscribe_url);
      if (valid_url($unsubscription_short)) {
        $replacements['[simplenews-subscriber:unsubscribe-url]'] = $unsubscription_short;
      }
    }
  }
}

/**
 * Helper function to convert site URL to shorten bitly URL
 */
function my_module_get_bitly_url($url) {
  $login = MD_SIMPLENEWS_BITLY_USER;
  $appkey = MD_SIMPLENEWS_BITLY_APIKEY;
  $format = 'json';
  $version = '2.0.1';  
  $bitly = 'https://api.bit.ly/shorten?version=' . $version . '&longUrl=' . urlencode($url) . '&login='. $login . '&apiKey=' . $appkey . '&format=' . $format;    
  $response = file_get_contents($bitly);  
  if (strtolower($format) == 'json') {
    $json = @json_decode($response,true);
    return $json['results'][$url]['shortUrl'];
  }  
  else  {
    $xml = simplexml_load_string($response);
    return 'https://bit.ly/'.$xml->results->nodeKeyVal->hash;
  }
}

Also check our Leveraging CKeditor template to theme Drupal contents post to know how we leveraged Simplenews for our recent project.