# Email structure

**URL:** <https://www.ruby-forum.com/t/email-structure/155598>\
**Category:** Ruby\
**Created:** [December 14, 2008, 10:56am UTC](https://www.ruby-forum.com/t/email-structure/155598 "2008-12-14T10:56:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 14, 2008, 10:56am UTC](https://www.ruby-forum.com/t/email-structure/155598/1 "2008-12-14T10:56:52Z")

</div>

I have accessed my gmail account and using imap downloaded an email and  
saved it too text.

Im wondering why the body of the message is repeated twice when in gmail  
the message is not repeated - is this a standard for emails? by the way  
my message itself was just a test so doesnt really make sense. One copy  
has \r\n for line breaks whilst the other has   
.

any reasons for this duplication?

Received: by 10.214.79.10 with HTTP; Sun, 14 Dec 2008 00:20:25 -0800  
(PST)

Message-ID:  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Date: Sun, 14 Dec 2008 17:20:25 +0900

From: “XXXXX” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

To: removed\_email\_address@domain.invalid

Subject: new receipt

MIME-Version: 1.0

Content-Type: multipart/alternative;

boundary=“----=\_Part\_24864\_15890710.1229242825307”

Delivered-To: removed\_email\_address@domain.invalid

------=\_Part\_24864\_15890710.1229242825307

Content-Type: text/plain; charset=ISO-8859-1

Content-Transfer-Encoding: 7bit

Content-Disposition: inline

#title:go shop at 2.30

#value:1450.00

#desc:me and teh fmaily will go shopping at the new 109 shops in

shibuya!!!and it will be great

really want to go and looking forward to oit

lets go

#envelope:

------=\_Part\_24864\_15890710.1229242825307

Content-Type: text/html; charset=ISO-8859-1

Content-Transfer-Encoding: 7bit

Content-Disposition: inline

#title:go shop at 2.30  
#value:1450.00  
#desc:me and teh fmaily will  
go shopping at the new 109 shops in shibuya!!!and it will be  
great  
really want to go and looking forward to oit  
lets  
go  
#envelope:

------=\_Part\_24864\_15890710.1229242825307–

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 14, 2008, 11:06am UTC](https://www.ruby-forum.com/t/email-structure/155598/2 "2008-12-14T11:06:56Z")

</div>

It’s not repeating the message, it is giving you a text version and a  
HTML version of the message. Just create a parse method to rip the two  
versions out.

- matt

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 14, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/email-structure/155598/3 "2008-12-14T12:28:15Z")

</div>

ahhh i thought both sections were identical, i didnt notice text and  
html tags in there.

can tmail do this parsing for me, dont really want to reinvent the  
wheel.i looked in teh docs and did a bit of experimenting in irb but  
coudnt find a way to specify if i wanted the whole body or just the text  
or html parts of it.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 15, 2008, 12:48am UTC](https://www.ruby-forum.com/t/email-structure/155598/4 "2008-12-15T00:48:03Z")

</div>

On Dec 14, 2008, at 5:20 AM, Adam A. wrote:

> can tmail do this parsing for me, dont really want to reinvent the  
> wheel.

Sure can.

> i looked in teh docs and did a bit of experimenting in irb but  
> coudnt find a way to specify if i wanted the whole body or just the  
> text  
> or html parts of it.

I use some code like the following to find the text portion of a  
message in the Ruby gateway code:

```
 extract_text = lambda do |message_or_part|
   if message_or_part.multipart?
     message_or_part.each_part { |part| extract_text[part] }
   elsif message_or_part.content_type == "text/plain"
     # use message_or_part here…
   end
 end
 extract_text[message]

```

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 15, 2008, 12:58pm UTC](https://www.ruby-forum.com/t/email-structure/155598/5 "2008-12-15T12:58:15Z")

</div>

Adam A. wrote:

> ahhh i thought both sections were identical, i didnt notice text and  
> html tags in there.
> 
> can tmail do this parsing for me, dont really want to reinvent the  
> wheel.i looked in teh docs and did a bit of experimenting in irb but  
> coudnt find a way to specify if i wanted the whole body or just the text  
> or html parts of it.

If you want to do this in a robust way, google for ruby mime

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [December 15, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/email-structure/155598/6 "2008-12-15T12:36:56Z")

</div>

thanks james for your proc. The rdoc for tmail doesnt have details for  
all the methods so your example has really helped.

Thank you
