# Pleas help for forwarding an email

**URL:** <https://www.ruby-forum.com/t/pleas-help-for-forwarding-an-email/175927>\
**Category:** Ruby\
**Created:** [September 22, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/pleas-help-for-forwarding-an-email/175927 "2009-09-22T23:31:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rabarama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@rabarama](https://www.ruby-forum.com/u/rabarama)\
**Post date:** [September 22, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/pleas-help-for-forwarding-an-email/175927/1 "2009-09-22T23:31:17Z")

</div>

I’m quite new to ruby, and something is driving me crazy.  
I’m working with emails, and i need to fw a email adding an header and a  
footer to the text.  
I’m having a lot of problems with multipart/alternative with attachments  
emails.  
I get the email from a pop mailbox, i get it as a tmail object and then  
i  
try to work with it…  
If i deliver the mail i get changing only “from, to, or subject”  
everythings  
goes right, but i can’t understand how to add someting to the body (in  
the  
html and in the plain part).  
I try to get parts and where a part is multipart i get parts of that  
part  
and so on rebuilding everypart i get to a new tmail object. When i find  
the  
text part i add my header to the body of that part .  
(Honestly i think this can’t be the best way to do this)  
This generates mail that loose html format, in line attachments name or  
attachment files.  
I can’t find any useful documentation, can someone here help me?  
If useful i will post some code…

---

<div class="post-metadata">

**Author:** ![rabarama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@rabarama](https://www.ruby-forum.com/u/rabarama)\
**Post date:** [September 23, 2009, 3:30am UTC](https://www.ruby-forum.com/t/pleas-help-for-forwarding-an-email/175927/2 "2009-09-23T03:30:55Z")

</div>

On Tuesday 22 September 2009 04:30:10 pm rabarama wrote:

> I’m working with emails, and i need to fw a email adding an header and a  
> footer to the text.  
> [snip]  
> I get the email from a pop mailbox, i get it as a tmail object  
> [snip]  
> I try to get parts and where a part is multipart i get parts of that part  
> and so on rebuilding everypart i get to a new tmail object. When i find the  
> text part i add my header to the body of that part .

By “header” do you mean something that should show up at the beginning  
of the  
message, inside the body? If so, that should work.

If you mean “header” as in an actual email header (like From, To, etc),  
you  
should probably be adding that to the message.

> This generates mail that loose html format, in line attachments name or  
> attachment files.

Please post some sample code. It sounds like you’re just trying to  
deliver  
that body. What you should be doing is storing the body back into the  
TMail  
object, then sending that object out.

> I can’t find any useful documentation, can someone here help me?

> **[LMGTFY - Let Me Google That For You](https://lmgtfy.app/?q=tmail%2Bdocumentation)**
>
> For all those people who find it more convenient to bother you with their question rather than to Google it for themselves.

Or, you could be more specific about how the existing documentation  
isn’t  
useful. It was pretty useful to me.

> If useful i will post some code…

I think you’ve got it backwards. You post code, then we can maybe give  
you  
useful advice. Without code, I’m just guessing here.

---

<div class="post-metadata">

**Author:** ![rabarama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@rabarama](https://www.ruby-forum.com/u/rabarama)\
**Post date:** [September 23, 2009, 10:16am UTC](https://www.ruby-forum.com/t/pleas-help-for-forwarding-an-email/175927/3 "2009-09-23T10:16:06Z")

</div>

> I think you’ve got it backwards. You post code, then we can maybe give you  
> useful advice. Without code, I’m just guessing here.

(Right, i want a text before the first line of the email, not an email  
header)  
thanks…  
here is the code i use…

( uses actionmailer and i call it by Mailer\_deliver\_mymailer(tmail) )

It’s a 3 level function couse i continued to add levels i found in the  
emails.  
It should be recoursive, but by now 3 is the max level it seems to me a  
email can reach.

My email test is a multipart/alternative email with 2 inline images and  
2  
attachments.  
I comment attachments code couse working only with parts should work  
also  
att.

In this code is not present the part where i add my text, but when i get  
the  
original mail from this it will be easy go on.

This mailer sends a mail with only text part visible, and an html  
attachment  
with the html version. No other attachment is in the mail.

def mymailer(mail)

```
subject mail.subject
recipients mail.to[0]
from mail.from[0]
sent_on Time.now

if mail.multipart? then
    content_type 'multipart/alternative'
    mail.parts.each do |p|

    if p.content_type=="multipart/related" then
    part 'multipart/related' do |mp|
      p.parts.each do |sp|

      if sp.multipart? then
       mp.content_type 'multipart/alternative'
       mp.part 'multipart/related' do |mmp|
         sp.parts.each do |ssp|
                 mmp.part :content_type => ssp.content_type,
                          :disposition => ssp.disposition,
                        # :charset => ssp.charset,
                          :transfer_encoding => 

```

ssp.transfer\_encoding,  
:body =\> ssp.body

```
                 end

```

# if sp.has\_attachments? then

# sp.attachments.each do |aa|

# mmp.attachment :content\_type =\> aa.content\_type,

# :original\_filename =\>

aa.original\_filename,

# :body =\> aa.read

# 

# end

# 

# end

```
            end

          else

                 mp.part :content_type => sp.content_type,
                          :disposition => sp.disposition,

                          :transfer_encoding => 

```

sp.transfer\_encoding,  
:body =\> sp.body

# if p.has\_attachments? then

# p.attachments.each do |aa|

# mp.attachment :content\_type =\>

aa.content\_type,

# :original\_filename =\>

aa.original\_filename,

# :body =\> aa.read

# end

# end

```
         end# sp multipart?

        end
      end

    else #p not multipart

   part :content_type => p.content_type,
                          :disposition => p.disposition,
                          :transfer_encoding => p.transfer_encoding,
                          :body => p.body

    end # multipart/related
 end # parts cycle

end # if multipart
#here code for non multipart emails...

```

end

---

<div class="post-metadata">

**Author:** ![rabarama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@rabarama](https://www.ruby-forum.com/u/rabarama)\
**Post date:** [September 23, 2009, 11:10am UTC](https://www.ruby-forum.com/t/pleas-help-for-forwarding-an-email/175927/4 "2009-09-23T11:10:33Z")

</div>

i’ve done it in another way (with the same results ☹ )

def partworker(p,mp)  
if p.multipart? then  
mp.part ‘multipart/related’ do |mmp|  
p.parts.each do |pp|  
partworker(pp,mmp)  
end  
end  
else  
mp.part :content\_type =\> p.content\_type,  
:disposition =\> p.disposition,  
:transfer\_encoding =\> p.transfer\_encoding,  
:body =\> p.body  
end  
end

def mailer(mail)  
subject mail.subject  
recipients mail.to[0]  
from mail.from[0]  
sent\_on Time.now  
if mail.multipart? then  
part ‘multipart/related’ do |mp|  
mail.parts.each do |p|  
partworker(p,mp)  
end  
end  
end

end
