Pleas help for forwarding an email

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…

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?

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.

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

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

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