Email Interaction with Web Applications (using PHP, Postfix and Regular Expressions)
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:
- 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.
- 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.
- 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:
-
/^[^\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 »

