# Wrong content-type when trying to parse emails with Net::IMA

**URL:** <https://www.ruby-forum.com/t/wrong-content-type-when-trying-to-parse-emails-with-net-ima/70486>\
**Category:** Ruby\
**Created:** [September 29, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/wrong-content-type-when-trying-to-parse-emails-with-net-ima/70486 "2006-09-29T17:08:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [September 29, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/wrong-content-type-when-trying-to-parse-emails-with-net-ima/70486/1 "2006-09-29T17:08:50Z")

</div>

i’m trying to parse an email that is supposed to be a multipart/mixed  
mail but i am not getting back the expected results.

data = imap.fetch(7, [“ENVELOPE”, “RFC822.HEADER”, “BODYSTRUCTURE”,  
“BODY[1]”])

[RFC822.HEADER]  
…[some headers removed to protect the innocent]…  
From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: removed\_email\_address@domain.invalid  
Date: Thu, 31 Aug 2006 19:03:07 -05:00  
Content-type: Multipart/Mixed; boundary=Message-Boundary-22611  
X-finfo: DOS,“FOOBAR.ZIP”,  
Subject: foobar inc. in stock inventory  
Priority: normal  
X-mailer: Pegasus Mail v3.50  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[BODYSTRUCTURE]  
#\<struct Net::IMAP::BodyTypeText media\_type=“TEXT”, subtype=“PLAIN”,  
param={“CHARSET”=\>“US-ASCII”}, content\_id=nil, description=nil,  
encoding=“7BIT”, size=218060, lines=2959, md5=nil, disposition=nil,  
language=nil, extension=nil\>

[BODY[1]  
–Message-Boundary-22611  
Content-type: text/plain; charset=US-ASCII  
Content-description: blah blah blah

blah blah blah  
–Message-Boundary-22611  
Content-Type: Application/Octet-stream; name=FOOBAR.ZIP  
Content-transfer-encoding: BASE64

…[base64 encoded data]…  
–Message-Boundary-22611–

as you can see above, the content-type is clearly multipart/mixed, but  
yet, when i dump the bodystructure, i get text/plain. the size and  
lines corresponds to the size/lines of the body of the email.

anyone got any ideas on this one?

Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [September 29, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/wrong-content-type-when-trying-to-parse-emails-with-net-ima/70486/2 "2006-09-29T22:45:42Z")

</div>

Chris H. wrote:

> i’m trying to parse an email that is supposed to be a multipart/mixed  
> mail but i am not getting back the expected results.

What did you expect?

/ …

> as you can see above, the content-type is clearly multipart/mixed, but  
> yet, when i dump the bodystructure, i get text/plain. the size and  
> lines corresponds to the size/lines of the body of the email.
> 
> anyone got any ideas on this one?

Sure. In e-mails everything is plain text – it has to be, in order to  
be  
acceptable to the protocol. Therefore the content encoding is set up to  
consist entirely of ASCII characters. It is up to the receiving software  
to  
convert this plain-text form into graphics or whatever the content  
actually  
represents.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [September 30, 2006, 1:06am UTC](https://www.ruby-forum.com/t/wrong-content-type-when-trying-to-parse-emails-with-net-ima/70486/3 "2006-09-30T01:06:46Z")

</div>

On 29-Sep-06, at 4:45 PM, Paul L. wrote:

> 

What paul means is that the Net::IMAP just returns the text, and  
you’ll have to parse the MIME attachments from the text that is in  
the body

Just out of curiosity, is there a lib in ruby to do this?

> –  
> Paul L.  
> [http://www.arachnoid.com](http://www.arachnoid.com)

Matt

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [September 30, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/wrong-content-type-when-trying-to-parse-emails-with-net-ima/70486/4 "2006-09-30T16:56:28Z")

</div>

ok, maybe i overexplained…

if i look at the RFC822.HEADER, it says

Content-type: Multipart/Mixed; boundary=Message-Boundary-22611

however, BODYSTRUCTURE says otherwise

#\<struct Net::IMAP::BodyTypeText media\_type=“TEXT”, subtype=“PLAIN”,  
param={“CHARSET”=\>“US-ASCII”}, content\_id=nil, description=nil,  
encoding=“7BIT”, size=218060, lines=2959, md5=nil, disposition=nil,  
language=nil, extension=nil\>

the size and lines correspond to the size of the entire mail.

BODYSTRUCTURE should be Net::IMAP::BodyTypeMultipart

BODY[1] shows:

–Message-Boundary-22611  
Content-type: text/plain; charset=US-ASCII  
Content-description: blah blah blah

blah blah blah  
–Message-Boundary-22611  
Content-Type: Application/Octet-stream; name=FOOBAR.ZIP  
Content-transfer-encoding: BASE64

…[base64 encoded data]…  
–Message-Boundary-22611–

if BODYSTRUCTURE were multipart, the above would have been parsed by  
Net::IMAP into the corresponding parts for me to parse, each part  
being BodyTypeText and BodyTypeBasic respectively.

now, what i would EXPECT, is that the BODYSTRUCTURE corresponds with  
the header and should be multipart/mixed, which would then allow me to  
grab the parts described above, but as i have explained this is not  
the case. I deal with other emails that are structured the same way  
and the BODYSTRUCTURE does in fact show as multipart/mixed

so the only thing i can surmise from this is that Net::IMAP is not  
interpreting the content-type of the mail correctly and defaults to  
text/plain us-ascii, as described in the RFC. now whether it’s a  
problem internal to Net::IMAP or perhaps the message itself (it was  
sent using Pegasus Mail v3.50, a DOS application), i have no way of  
knowing, and was looking for insight.

Chris
