A place to share technical solutions

This issue is mostly well documented elsewhere on the internet, but I recently found an unusual cause of this problem which I felt was worth documenting.

The following list should hopefully help you to isolate this problem, but if I’ve missed anything please let me know via comments:

Hopefully this will help at least one person.

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).

The idea of this blog is to detail solutions to anything technical I’ve tried that hasn’t been straightforward. What better way to start than with instructions on installing wordpress:

Instructions for Ubuntu 7.10 using bash, but should be similar for any *nix install.

Assuming the following:

Downloading and extracting the latest version:

sudo su
cd /tmp/
wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
mv wordpress/* /var/www/
cd /var/www/

Configuring the mysql database:

mysql -u root -p
CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* TO ‘wordpress’@'localhost’ IDENTIFIED BY ’somepassword’;
FLUSH PRIVILEGES;
EXIT;

Moving the default config file:

mv wp-config-sample.php wp-config.php
nano wp-config

Change it to look like this:

define(’DB_NAME’, ‘wordpress’);
define(’DB_USER’, ‘wordpress’);
define(’DB_PASSWORD’, ’somePassword’);
define(’SECRET_KEY’, ‘make this unique!’);

Next go to yourhostname.com/wp-admin/install.php and follow the steps. Once you have logged in it should take you to a configuration portal and yourhostname.com should be your blog - have fun!

If you get a directory listing instead of a portal, you need to ensure apache is configured to recognise index.php as an index file. Ensure the /etc/apache2/httpd.conf (or the vhost configuration file if you are using one) looks something like this:

DirectoryIndex index.php index.html index.htm

If you had to change this file, you need to reload apache:

/etc/init.d/apache2 reload