Rails mailer mimepart visible as text in message body
Asked Answered
O

1

8

I'm sending test mail using ActionMailer. The template is being rendered and mail is being delivered fine. The only problem is the mimepart and other header data is displayed by Google in message body.

Here is the code that mails..

def testing

    mail(:to => "[email protected]",:subject => "html mailer", :content_type => "text/html") do |format|
          format.html { render 'testing' }
          format.text { render :text => "bing" }
    end
end

and Here's the email received.

----==_mimepart_508fd46252b8c_8023fe595835ad0479a6 Date: Tue, 30 Oct 2012 18:51:38 +0530     
Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit
Content-ID: <508fd46254ea7_8023fe595835ad0480b8@Apoorv-Parijats-MacBook-Pro-2.local.mail> 
bing ----==_mimepart_508fd46252b8c_8023fe595835ad0479a6 Date: Tue, 30 Oct 2012 18:51:38 
+0530 Mime-Version: 1.0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding:  
7bit Content-ID: <508fd46256465_8023fe595835ad04819c@Apoorv-Parijats-MacBook-Pro-
2.local.mail> Hi bing
column 1    column 2
----==_mimepart_508fd46252b8c_8023fe595835ad0479a6--

Output of the console -

 Loading development environment (Rails 3.2.2)
 1.9.3-p125 :001 > RankMailer.testing.deliver
 I, [2012-10-30T18:51:38.331238 #2050]  INFO -- :   Rendered rank_mailer/testing.html.erb           
 (1.8ms)
 I, [2012-10-30T18:51:38.333117 #2050]  INFO -- :   Rendered text template (0.0ms)
 I, [2012-10-30T18:51:45.824962 #2050]  INFO -- : 
 Sent mail to [email protected] (7484ms)
 D, [2012-10-30T18:51:45.825125 #2050] DEBUG -- : Date: Tue, 30 Oct 2012 18:51:38 +0530
 From: [email protected]
 To: [email protected]
        Message-ID: <508fd462572ec_8023fe595835ad0482c0@Apoorv-Parijats-MacBook-Pro-2.local.mail>
    Subject: html mailer
    Mime-Version: 1.0
    Content-Type: text/html;
     charset=UTF-8
    Content-Transfer-Encoding: 7bit



    ----==_mimepart_508fd46252b8c_8023fe595835ad0479a6
    Date: Tue, 30 Oct 2012 18:51:38 +0530
    Mime-Version: 1.0
    Content-Type: text/plain;
     charset=UTF-8
    Content-Transfer-Encoding: 7bit
    Content-ID: <508fd46254ea7_8023fe595835ad0480b8@Apoorv-Parijats-MacBook-Pro-2.local.mail>

    bing

    ----==_mimepart_508fd46252b8c_8023fe595835ad0479a6
    Date: Tue, 30 Oct 2012 18:51:38 +0530
    Mime-Version: 1.0
    Content-Type: text/html;
     charset=UTF-8
    Content-Transfer-Encoding: 7bit
    Content-ID: <508fd46256465_8023fe595835ad04819c@Apoorv-Parijats-MacBook-Pro-2.local.mail>

    Hi bing

    <table style="border:1px solid red">
        <tr>
            <td>column 1</td>
            <td>column 2</td>
        </tr>
    </table>

    ----==_mimepart_508fd46252b8c_8023fe595835ad0479a6--

     => #<Mail::Message:70255316899740, Multipart: false, Headers: <Date: Tue, 30 Oct 2012 18:51:38 +0530>, <From: [email protected]>, <To: [email protected]>, <Message-ID: <508fd462572ec_8023fe595835ad0482c0@Apoorv-Parijats-MacBook-Pro-2.local.mail>>, <Subject: html mailer>, <Mime-Version: 1.0>, <Content-Type: text/html>, <Content-Transfer-Encoding: 7bit>>
Osugi answered 30/10, 2012 at 13:36 Comment(0)
R
9

Don't specify :content_type => "text/html" in your mail method. Since you're using format block, rails will automatically pick up mime type.

MORE DETAILS:

Try this to send out multipart email (ie. both html and text formats of email). Notice the order of formats.

mail(:to => "[email protected]", :subject => "html mailer") do |format|
    format.text { render :text => "bing" }
    format.html { render 'testing' }
end
Reld answered 1/11, 2012 at 12:45 Comment(2)
The problem was the :content_type. I removed format.text and left the :content_type as it is. HTML email is being sent without any error. Though I'm yet to figure out that what :content_type should be used if I've to send the fallback text as well in the email.Osugi
You can user multiple formats. Then Mailer will create multipart email, with all user formats. User's client or web interface will pick appropriate format automatically. To use more formats, remove the ":content_type" parameter. I just added example to the reply. Take a look at this rails guide for more details and ordering info.Reld

© 2022 - 2024 — McMap. All rights reserved.