Best Way of Extracting Key Value from File

What is the best way of extracting a key value pair from a file.

For example I have an email text file which goes like so :


Return-Path: [email protected]
Delivered-To: [email protected]
Date: Sat, 3 May 2008 18:25:13 -0400
From: “Athar Shiraz Siddiqui” [email protected]
To: [email protected]
Subject: Testing your email checking capabilities
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

testing …

Now I would like to extract the usual From: and To: values from this
text file.

YAML seems ideal for this but the only problem is that YAML gives the
following error : ArgumentError (syntax error on line 23, col -1: `To:
[email protected]
Subject: Testing your email checking capabilities
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

testing …
‘):
c:/ruby/lib/ruby/1.8/yaml.rb:133:in load' c:/ruby/lib/ruby/1.8/yaml.rb:133:in load’
/app/controllers/messages_controller.rb:66:in `parseMessage’;

So this is definitely a problem and I can’t use YAML.

I tried regexps but I am new to them and I can only extract the entire
line like so :

result = /^From:.*$/.match(@emailcontent) # where @email content is the
text of the email

Regexp result matchdata object gets the entire From : “…” <> line
including the from. I would like the value of “From:” (NOT including
From: meaning I would like the email address that is after the colon).

What do I use here ? In java we could do string.indexof and substring
etc. Is there something comparable? I am going through the api right now
and thinking of a way of removing the “from:” key from the returned line
; delete won’t work because it deletes all characters in intersection!
so that wont extract the value of “From:” . I am trying gsub(“From:”,
“”) as a work around for now. but any less clunking solutions would be
sweet.

If you just need to read e-mails, you can do that without bothering with
saving them to a file. The to/from fields will be easily accessible
then.

I ended up using an ActionMailer and the plugin linked to here:
http://siannopollo.blogspot.com/2008/02/mail-fetcher-fetch-email-from-ruby-and.html

You’ll need to modify that plugin a bit, however. If its what you’re
looking for, let me know when you get stuck with it, and I’ll try and
help out.

Dan __ wrote:

If you just need to read e-mails, you can do that without bothering with
saving them to a file. The to/from fields will be easily accessible
then.

I ended up using an ActionMailer and the plugin linked to here:
http://siannopollo.blogspot.com/2008/02/mail-fetcher-fetch-email-from-ruby-and.html

You’ll need to modify that plugin a bit, however. If its what you’re
looking for, let me know when you get stuck with it, and I’ll try and
help out.

Precisely I just need to read emails from the inboxes folder/directory
of the James Mail server. (http://james.apache.org)

This however looks promising. It is an SMTP server though (not pop)

Unfortunately, I don’t know how to read emails from an SMTP server :frowning:
The plugin I sent only works with POP and IMAP. I faced this problem a
little while ago (although I was using a POP server), and I still have
bookmarks from when I was searching. Perhaps one of them will help you?

http://wiki.rubyonrails.org/rails/pages/HowToReceiveEmailsWithActionMailer
http://blog.craigambrose.com/past/2008/2/9/respond_toemail_or_how_to_handle/
http://offtheline.net/2008/2/14/receiving-email-with-rails

Most of the examples use SMTP, but not the James Mail server you’re
using. So I think you could use the basic idea, but you’d have to
figure out the specific configurations yourself.