2012-10-30 76 views
6

我使用ActionMailer發送測試郵件。模板正在呈現,郵件正在正常傳遞。唯一的問題是Google在郵件正文中顯示mimepart和其他標題數據。軌道郵件mimepart可視爲郵件正文中的文本

下面是郵件的代碼..

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 

,這裏是收到的電子郵件。控制檯的

----==_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: <[email protected].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: <[email protected] 
2.local.mail> Hi bing 
column 1 column 2 
----==_mimepart_508fd46252b8c_8023fe595835ad0479a6-- 

輸出 -

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: <[email protected].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: <[email protected].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: <[email protected].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: <[email protected].local.mail>>, <Subject: html mailer>, <Mime-Version: 1.0>, <Content-Type: text/html>, <Content-Transfer-Encoding: 7bit>> 

回答

6

不要指定在郵件法:content_type => "text/html"。由於您使用的是格式塊,導軌會自動選取MIME類型。

更多細節:

嘗試使用此方法發送郵件多(即HTML和電子郵件的文本格式)。注意格式的順序。

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

問題是':content_type'。我刪除了'format.text'並保持':content_type'原樣。 HTML電子郵件正在發送,沒有任何錯誤。儘管我還沒有弄清楚,如果我還要在電子郵件中發送回退文本,應該使用什麼':content_type'。 –

+0

您可以使用多種格式。然後,Mailer將創建包含所有用戶格式的多部分電子郵件。用戶的客戶端或Web界面將自動選擇適當的格式。要使用更多格式,請刪除「:content_type」參數。我只是在回覆中添加了示例。看看這個[rails guide](http://guides.rubyonrails.org/action_mailer_basics.html#sending-multipart-emails)瞭解更多詳情和訂購信息。 – rdamborsky

相關問題