This is a simple email parser. It is primarily for demonstration purposes, as it makes some assumptions for the sake of simplicity. Some of these assumptions are faulty.
Require the necessary files in your project, and instantiate a new Email object:
email = GangstaMail::Email.new(your_raw_email_text)
You can then access the email fields through the accessor methods and instance methods of your new email object.
-
We don’t need to optimize for speed.
-
Email body is separated from header by one blank line.
-
There are no multi-line fields in email header.
-
There’s only one Received field. The code shouldn’t break if there are. several Received fields, but I’m not keeping track of them all.
-
We don’t need to access every field in the email header.
-
The raw email does not start with sevaral newline characters.
-
Create a command-line interface to read text files with the raw email text
-
Separate name from email address in To: and From: fields
-
Handle multi-line values in header
-
Handle different MIME parts in the body
-
Parse nested parts of body
-
Handle attachments
I’d also gemify this code to make it easier for people to use.