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,
Confirmation for [simplenews-category:name] from [site:name]
We have received a request to subscribe [simplenews-subscriber:mail] to the [simplenews-category:name] newsletter. To confirm please use the link below.
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 http://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.
Also check our Leveraging CKeditor template to theme Drupal contents post to know how we leveraged Simplenews for our recent project.