2012-02-02 27 views
2

我遇到了我的Rails應用程序的問題 - 我無法發送我的電子郵件的HTML和純文本版本。注:電子郵件確實發送;但是,它的風格並不正確......以下鏈接指向結果。RoR ActionMailer問題 - 無法同時發送純文本和HTML電子郵件。

無處不在,如果你想發送HTML,你也應該發送一個純文本的選擇。不幸的是,似乎我做錯了什麼,因爲我的應用程序不允許我發送HTML和純文本,沒有看起來很奇怪的HTML。

這裏是我的郵件模式:

class ProjectMembersMailer < ActionMailer::Base 

    def membership_invitation(membership) 
    @project = membership.project 
    @user = membership.user 

    mail(:subject => %(Invitation to join project #{@project.business_name}), 
      :from => %("App" <[email protected]>), 
      :to  => @user.account.email, 
      :content_type => "text/html") do |format| 
     format.html 
     format.text 
     end 
    end 

end 

我project_member_mailer的觀點有兩個文件:membership_invitation.html.hamlmembership_invitation.text.erb(請注意,第二個文件是使用.erb,但即使我將其轉換爲.haml延伸爲了一致性,我得到了相同的錯誤)

這裏是圖片顯示,當我嘗試使用上面的代碼發送它時,輸出看起來像。請注意,我刪除了一些文字。 screen

基本上它看起來像是發送文本的html版本以上的文本版本。有沒有這種情況發送明文和HTML電子郵件的替代方法?或者我錯過了某些東西 - 比如,這些電子郵件是不是應該同時發送?任何幫助將不勝感激。非常感謝您的時間和幫助!

+2

不要明確設置內容類型 - 您需要讓rails將其設置爲multipart/mixed – 2012-02-02 21:45:13

回答

0

根據Action Mailer Rails Guide,您不需要使用「format」方法,也應該刪除「content-type」參數。

郵件會自動檢測有HTML和文本模板,並會自動創建電子郵件作爲多/替代

試試看:

mail(:subject => %(Invitation to join project #{@project.business_name}), 
     :from => %("App" <[email protected]>), 
     :to  => @user.account.email) 
0

我有完全相同的問題,它可以只用一件簡單的事就可以解決問題。在format.html上放置format.text

def membership_invitation(membership) 
@project = membership.project 
@user = membership.user 

mail(:subject => %(Invitation to join project #{@project.business_name}), 
     :from => %("App" <[email protected]>), 
     :to  => @user.account.email, 
     :content_type => "text/html") do |format| 
    format.text 
    format.html 
    end 
end 
相關問題