Email notifier in PHP

March 4th, 2009

I like writing simple and practical scripts that solve real problems. I needed an e-mail monitor for checking new messages with SMS notification. Since SMS messages cost money and I get lots of spams every day, it is very important that I am notified only when an important e-mail arrives.

And the solution:

For sending SMS messages I chose to use SmsBug (smsbug.com) SMS gateway as I am satisfied with its quality and price.
The script includes a class written by me for interacting with SmsBug’s service.
I use PHP IMAP extension to retrieve the list of e-mails. List of e-mails are filtered by the sender’s address. Allowed senders (e-mails from these senders are probably important) are specified by PERL regular expression. Concrete e-mail addresses can be listed or e-mail addresses under a certain domain name. Let us see an example on how to by notified when an e-mail is received from either w3net.eu, cnn.com, or gmail.com domains:

The email’s header is checked for a match with the given regular expression. If matches, the e-mail is considered to be important. I must say that I was lazy to check the sender’s address, I look for matches in the entire e-mail header (maybe in the next version).

Here comes another example. Say we want to be notified when we receive an e-mail from our boss or an e-mail from any gmail account.

Schedule the script

The script always looks for e-mails received yesterday (you can change this). It is sufficient for me to be notified of e-mails received yesterday. I scheduled the script to be executed every day at 10 AM. When I am at work I usually have downloaded my e-mails by 10AM. When the notifier is executed at 10AM, the e-mail box is already empty by that time. But when I am on holiday (or in hospital), e-mails are not downloaded, so the script will find the e-mails and would notify my by SMS message if the e-mail is from an important sender. The SMS message contains only the sender’s e-mail address, time, and subject of the message.

If you wish to monitor your GMail account you need to use the configuration I put between comments. I tested it with my GMail account, it appeared to work fine with.

Leave a Reply