Automatically save/download e-mail body to disk

0

1

Is there a program that will allow me to connect to my mail server (IMAP) and automatically save certain new e-mails to disk? Multiple times a day I receive automated e-mail updates about pending jobs from a system that processes some information for us. The data in these e-mails is written as plain-text within the body of the message. I would like to download the newest message, parse it, and display it on my desktop. The last two parts I can manage ok - it's just the automatic downloading that is posing a challenge.

I don't use Outlook (I do use Thunderbird), but would prefer not to have the client open to make this happen. I'm currently running Win7.

CatamountJack

Posted 2011-02-20T04:14:47.733

Reputation: 328

Answers

0

I'd approach this by writing a script. Ruby has an IMAP library, and I assume Perl and Python do as well if those are more to your tastes.

Here's a rough, untested cut in Ruby (which you'll need to install on your windows machine to use) based on an example from the IMAP docs here

  imap = Net::IMAP.new('mail.example.com')
  imap.authenticate('LOGIN', 'joe_user', 'joes_password')
  imap.examine('INBOX')
  imap.search(["NEW", "FROM", "example.from", "SUBJECT", "example subject"]).each do |message_id|
    body = imap.fetch(message_id, "BODY[TEXT]")[0].attr["BODY[TEXT]"]
    File.open('path to file', 'a') { |f| f.write(body) }
  end

You can change the parameters passed to search as you need to to identify your messages. Install ruby, save this script as a text file and set it up to run as a scheduled task every few minutes and you should get your text file with the message body.

Andy Tinkham

Posted 2011-02-20T04:14:47.733

Reputation: 101

Thanks! I don't have much programming experience beyond AutoHotKey (as much as it can be considered programming), but I might give this a shot and see what happens. – CatamountJack – 2011-02-22T03:15:15.490