Search code examples
ruby-on-railsruby-on-rails-3emailactionmailermultipart

How to see the multipart/alternative email in Rails?


When sending a multipart/alternative email, the body is empty:

m = send_mail
=> <Mail::Message:70121014358060, Multipart: true, Headers: <Date: Tue, 06 Nov 2012 09:18:06
-0800>, <From: [email protected]>, <To: [email protected]>, 
<Message-ID: <[email protected]>>, <Subject: 
Test>, <Mime-Version: 1.0>, <Content-Type: 
multipart/alternative; boundary="--==_mimepart_5099422e825b0_71113fc654c34ce068277";
charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>

m.body
=>

How to inspect the body or the different parts?

Rails 3.2.2


Solution

  • From https://github.com/mikel/mail:

    mail = Mail.read('multipart_email')
    
    mail.multipart?          #=> true
    mail.parts.length        #=> 2
    mail.preamble            #=> "Text before the first part"
    mail.epilogue            #=> "Text after the last part"
    mail.parts.map { |p| p.content_type }  #=> ['text/plain', 'application/pdf']
    mail.parts.map { |p| p.class }         #=> [Mail::Message, Mail::Message]
    mail.parts[0].content_type_parameters  #=> {'charset' => 'ISO-8859-1'}
    mail.parts[1].content_type_parameters  #=> {'name' => 'my.pdf'}