2012-02-02 46 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 
相关问题