A dear friend of mine recently needed to recover all email from his mailbox. Normally this wouldn’t be a problem, there are plenty of options in any sane mail application – export or archive mailbox, select-all messages and “Send Again”/Redirect/Bounce to another address or at the very worst, select-all and forward. Most of these options are available with desktop mail applications – Pine, Squirrelmail, IMP, Outlook, Outlook Express, Windows Mail, Mail.app, Thunderbird, Eudora and I’m sure loads of others.
Unfortunately the only access provided was through Microsoft’s Outlook Web Access (2007). This, whilst being fairly pretty in Lite (non-Internet Explorer browsers) mode and prettier/heavier in MSIE, does not have any useful bulk forwarding or export functionality at all. None. Not desperately handy, to be sure.
Ok, so my first port of call was to connect my Mail.app which supports Exchange OWA access. No dice – spinning, hanging, no data. Hmm – odd. Ok, second I tried fetchExc a Java commandline tool which promised everything I needed but in the end delivered pretty obtuse error messages. After an hour’s fiddling I gave up with fetchExc and tried falling back to Perl with Email::Folder::Exchange. This had very similar results to fetchExc but a slightly different set of errors.
After much swearing and a lot more poking, probing and requesting of tips from other friends (thanks Ze) the OWA service was also found to be sitting behind Microsoft’s Internet Security and Acceleration server. This isn’t a product I’ve used before but I can only assume it’s an expensive reverse proxy, the sort of thing I’d compare to inexpensive Apache + mod_proxy + mod_security on a good day. This ISA service happened to block all remote SOAP (2000/2003) and WebDAV (2007/2010) access too. Great! No remote service access whatsoever.
Brute force to the rescue. I could, of course go in and manually forward each and every last mail, but that’s quite tedious and a huge amount of clicking and pasting in the same email address. Enter Selenium IDE.
Selenium is a suite of tools for remote controlling browsers, primarily for writing tests for interactive applications. I use it in my day to day work mostly for checking bits of dynamic javascript, DHTML, forms etc. are doing the right things when clicked/pressed/dragged and generally interacted with. OWA is just a (really badly written) webpage to interact with, after all.
I downloaded the excellent sideflow.js plugin which provides loop functionality not usually required for web app testing and after a bit of DOM inspection on the OWA pages I came up with the following plan –
- click the subject link
- click the “forward” button
- enter the recipient address
- click the send button
- select the checkbox
- press the “delete” button
- repeat 500 times
The macro looked something like this:
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">owa-selenium-macro</td></tr>
</thead><tbody>
<tr>
<td>getEval</td>
<td>index=0</td>
<td></td>
</tr>
<tr>
<td>while</td>
<td>index<500</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>index</td>
<td>value</td>
</tr>
<tr>
<td>echo</td>
<td>${value}</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>//table[1]/tbody/tr[2]/td[3]/table/tbody/tr[2]/td/div/table//tbody/tr[3]/td[6]/h1/a</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>id=lnkHdrforward</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>id=txtto</td>
<td>newaddress@gmail.com</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>id=lnkHdrsend</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>name=chkmsg</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>id=lnkHdrdelete</td>
<td></td>
</tr>
<tr>
<td>getEval</td>
<td>index++</td>
<td></td>
</tr>
<tr>
<td>endWhile</td>
<td></td>
<td></td>
</tr>
</tbody></table>
So I logged in, opened each folder in turn and replayed the macro in Selenium IDE as many times as I needed to. Bingo! Super kludgy but it worked well, was entertaining to watch and ultimately did the job.