SMS alerts for new email via Twitter

As I have mentioned previously, Twitter has many uses. Today, I used it to implement a quick and simple solution for SMS alerts. A certain someone (a client of ours) wanted to receive an SMS message on his phone every time an email arrives to his mailbox. Sounds simple, right? Well, here in Cyprus we lack free and simple solutions for mass SMS messages. So, it wasn’t so simple until Twitter came along. Here is how I did.

I’ve registered two new accounts on Twitter (they are free, aren’t they?). One account is used for the client himself, and has his mobile configured, complete with “Don’t send SMS during these hours…” feature. The second account is used by the mail server to send notifications. Now, this second account is set to be protected in Twitter, so that only manually accepted people can follow and receive updates and notifications.

Then I wrote a really really simple Perl script (I called it twit_mail.pl). Most of the work is done by a Net::Twitter perl module, so I didn’t have to do much.

The script is designed to be used with mail aliases. For example, if I was to use for user@mysystem mailbox, I’d create the following aliases in /etc/aliases:

user:  user,twitter
twitter: "|/path/to/twit_mail.pl username password verbose"

The first line here tells the system to copy an incoming message to another account – twitter. This is needed so that the original email is received untouched by the user, and my twit_mail.pl script had something to play with.

The second line creates a pipe alias. Each message that comes to twitter@mysystem is piped to the twit_mail.pl script. The script requires two parameters – username and password for Twitter account to use – and allows for verbosity parameter. If verbose is set, then the script will send an informative status update to Twitter, something like “Message from a@b.com to user@mysystem about ‘some subject'”. If verbosity is off, a generic “You’ve got mail” will be sent.

You can easily find the appropriate lines in the script and change them for fixed values, if you only want to send notifications for one account. I chose to go with parameters, so that I could use the same copy of the script for several accounts.

Once you have the script set, aliases configured, and Twitter accounts created and linked, all you are left with is a test message. And that’s about it.

3 thoughts on “SMS alerts for new email via Twitter”


  1. There’s also a way for people that can’t or don’t want to write any code to do it, though it uses a few more resources:

    Forward all mails to a secret mailbucket account. Use a high security name for this account so that nobody will guess it and read your mail.

    Then have twitterfeed grab the RSS feed from mailbucket and send the updates to the “second” twitter account -- I used two twitter accounts in my method as well. I’ve been using this for the past week or so now and its working pretty swell, at least when twitter, twitterfeed, mailbucket and gmail are all working as they should -- being dependent on four different free services will cause this to fail more often then regular things.


  2. Hoo Hoo Nick,

    thanks for the tip! Yes, indeed, it is possible to route emails to twitter via RSS. There are several options for this. IIRC, even gmail has some sort of RSS feed. I just don’t remember if it is for inbox or if you can have it for specific label.

Leave a Comment