Jul
7
Use PHP to text you everytime you get a new email
Filed Under PHP
This tutorial explains how to connect to gmail with php, and have an SMS alert sent to your mobile phone using Twitter every time you receive a new email. I used PHP 5.2.3 on Ubuntu 7.10 with MySQL 5.0.45 , but other combinations may work.
Firstly you need to open two twitter accounts (there is probably a way to do it with just one, but I already had one open so I used that): The first account (say ‘account1′) should be your main account and you should follow the introductory instructions to register your mobile phone with the account (it’s free, as is receiving text messages). The second account (’account2′) should be set to private feeds (you don’t want everyone being able to read your email). Login to ‘account1′ and follow ‘account2′, then login to ‘account2′ and accept the request. Finally, text “ON account2″ to twitter from your registered mobile number. Twitter is now setup for the script to work.
Next ensure gmail is configured to allow IMAP connections to your account by logging in, and ensuring IMAP is enabled in Settings->Forwarding and POP3/IMAP.
Now we need a database to store email IDs in (so you don’t get alerted about the same email twice). Also we create a second table here to hold the most recently updated status - this is because Twitter won’t allow a status update that is the same as the last status, but you might receive two emails with the same subject (especially if you are having a gmail conversation) so if the status matches the previous status a hyphen is appended just to make it unique.
mysql -u root -p
CREATE DATABASE Email;
USE Email;
CREATE TABLE TwitterEmail(email VARCHAR(20), id VARCHAR(980), PRIMARY KEY (email, id));
CREATE TABLE TwitterSubject (email varchar(20) PRIMARY KEY NOT NULL, last_subject text, FOREIGN KEY(email) REFERENCES TwitterEmail(email));
GRANT ALL TO ‘db_user’@'localhost’ IDENTIFIED BY ‘db_password’;
EXIT;
Save Email2Twitter.php into /home/user/ and ensure it is executable by you but not readable by anyone else (as it has your passwords in it):
chmod 700 Email2Twitter.php
which php # note the output here as we need it for crontab in a second
crontab -e
In the nano window that comes up, add a new crontab entry to run the program as often as you’d like, making sure to specify the full path of both php and Email2Twitter.php. For example:
00 * * * * /usr/bin/php /home/username/Email2Twitter.php
The above example will cause the program to run once an hour providing that machine stays turned on.
Troubleshooting:
If the machine can’t find ‘php’, ensure that php5-cli is installed. Likewise, if you get an error message complaining about IMAP functions not being found ensure php5-imap is installed.
I had a program running this script on one machine where it had trouble connecting to imap.gmail.com yet I could still telnet to it (so the firewall wasn’t causing the problem). If anyone can suggest why this particular machine was causing an issue it would be very much appreciated.
All comments, suggestions and corrections welcome.
UPDATE: Due to a change in the service offered by Twitter, this no longer works unless you are in the United States, Canada or India. It’s quite obvious this was coming as their SMS deliveries have been unreliable to say the least over the past months.
When I have time I’ll look into another free way of doing this (it may not be possible), and soon I’m planning on writing about controlling a linux machine using a mobile phone via Twitter (which should still work).