yes i love technology

Email Interaction with Web Applications (using PHP, Postfix and Regular Expressions)

February 9th, 2007 by Pete

One of the popular facilities on phuser.com is the ability to create polls. Once a poll has been created it can be sent to people by SMS or Email. The recipients can reply with their choice of answer and a optional comment using SMS, Email or phuser web interface, lovely!

Handling of emails replies has easy been the trickiest part of this process. Here is a very basic overview of the steps we have taken:

  1. Have postfix forward the email to a PHP command line script that saves the email to file. Details of how to do this can be found here.
  2. Cron runs a PHP script that processes the email files. It makes sure the emails are valid, strips the users reply from the body of the email, and then adds the email to a database table.
  3. Crons runs another script which queries the database table and processes the emails replies, this is where the actually interaction with the phuser system (the magic) happens!

Today I shall be describing the regular expression that we used for Step 3 as it took a bit of effort to get right. Step 2 will be covered at a later date.Warning: when I cover Step 2 I will be having a rant about my new found hatred of HTML emails!

Note: Steps 1 and 2 are separate because security precautions on our servers prevent the Postfix user from interacting with the Databases.

When someone gets a poll email from phuser the content looks like this:

pete (pete graham) wants your opinion:

Which is your favourite drink?

Vote for one of the options below:

1) Tea
2) Coffee
3) I love them both like they were my children
4) Urgh! I can’t stand either!

——————— YOUR REPLY BELOW THIS LINE ———————

——————— YOUR REPLY ABOVE THIS LINE ———————

Reply using one of the following methods:

o Reply to this email. Include the number of your vote at the start of your message. Any extra text will be added as a comment. (Please do not edit the Subject line).

Now due to the difference in the way email clients work and the ways people think here is a small selection of the possible replies we needed to be able to handle:

  • > 1 I like to drink tea since I am a English Gentleman!
  • 4 I’ll have whats going
  • 3 – I hate them all
  • > 2)
  • 1) tea because I love tea!!!
  • 1 I’m not a big drinker of either since I’m the no-caffs, but like a cup of decaff tea
  • > 1
  • 2

So what we required is a Regular Expression which would correctly grab the number if one was included at the beginning so we could log the voting option. We also needed to grab the text afterwards if any was included so it could be used as a comment. Here’s what we eventually came up with:

CODE:

  1. /^[^\w]*([0-9]+)[^\w]*(\w.*)/ms

And it works, yay! The ‘m’ flag is set so the Regex is multiline , the ’s’ flag makes the dot character match every character including newlines. Here is the page that describes all the PHP regular expression modifiers.

If you want to learn more about regular expressions (and who doesn’t) then loads of information can be found on this site. Another great site is the one that we used to testing our regular expressions, it lets you supply up to 10 different test strings at once.

If you’d like to see this system in action then go to phuser.com and request an invite we are still looking for Beta testers currently, if you sign-up now you can have a real input into the site as it develops and grows.

Ps. The regular expression is in an if statement. If it fails the entire message is put in the comments section on the poll.

# Pete Graham

Posted in Uncategorized, phuser, postfix, php, regex, regular expressions | 8 Comments »

Facebook

January 29th, 2007 by Pete

I signed up for a Facebook account today, partially out of curiosity but also because I wanted to see what type of crossover it has with phuser, the website I’ve been developing.

I’ve been finding it quite interesting so far, there’s a cool feature where if you give Facebook your email address username and password it will go rooting about in your contacts and then show you which of them are on Facebook.

It seems better thought out than mySpace its main rival, but not as fun. Some of the concepts seem to have quite odd names. For example instead of commenting on peoples profiles you add comments to someone’s “wall”. Also I am the only one that finds the idea of “poking” people a little dodgy, check the image below to see what I mean.

poking young girls on facebook

Anyway I think I need to have a bit more time playing with the site before I make my mind up about but its definitely got some interesting ideas in there.

/* Pete Graham */

Posted in Tech, phuser, facebook, poke | No Comments »

What I’ve been doing for the last 6 months

January 17th, 2007 by Pete

Firstly I would like to confirm that despite rumours circulating around Ironbridge this is what I haven’t been doing for the past half a year:

  • Become a Buddhist Monk
  • Had a Sex change
  • Been training to be an astronaut
  • Been cryogenically frozen

What I have been doing is working for Bluefire systems on Phuser.com a new and innovative mobile/social networking website.

I didn’t managed to write a blog yesterday because we launched the Beta version of phuser and I was very busy with that. What we now need is people to trial the site so if your interested then go to the phuser homepage and request an invite.

One of the things that I shall be covering on this blog is how the launch and development of phuser is progressing.

My colleague bearded Internet Entrepeneur George Black is also running his own blog covering the phuser launch from a different perspective, check it out!

# Pete Graham

Posted in Tech, phuser, launch, social network, social networking | 1 Comment »