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
Comments 5
Argh what the hell has wordpress done to my formatting on this post!!!!
Posted 09 Feb 2007 at 1:36 pm ¶Ah fixed the formatting now, note to self upgrade to wordpress 2.1 so formatting HTML by hand is less of a pain in the arse!
Posted 09 Feb 2007 at 1:43 pm ¶Just a minor note, but you can use \W instead of [^\w], it’ll run a bit quicker (even though it’s technically equivalent).
Also the last match: (\w.*). Won’t this just match to the end of the email regardless with the s modifier? Hence why you’re getting people’s email signatures included. If you’re stripping signatures out already then no problem but thought I’d point it out . . .
Posted 09 Feb 2007 at 2:20 pm ¶Signatures are stripped at Step 2. This step in being upgraded in the next phuser release so we can deal with top + bottom posting (when people don’t write between the lines).
Didn’t know about ‘\W’ good tip.
Posted 09 Feb 2007 at 2:25 pm ¶online JS regex testing
http://www.pagecolumn.com/tool/regtest.htm
Posted 13 Nov 2008 at 9:04 am ¶Post a Comment