我的 Rails 应用程序出现问题——我在发送电子邮件的 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" <no-reply@appname.com>),
:to => @user.account.email,
:content_type => "text/html" ) do |format|
format.html
format.text
end
end
end
我的 project_member_mailer View 有两个文件:membership_invitation.html.haml
和 membership_invitation.text.erb
(请注意第二个文件使用的是 .erb,但即使为了保持一致性,我将它转换为 .haml 扩展名,我得到了同样的错误)
这是我尝试使用上面的代码发送它时输出的图片。请注意,我删除了一些文字。
基本上它看起来像是在文件的 html 版本之上发送文本版本。有没有另一种方法可以在不发生这种情况的情况下发送纯文本和 HTML 电子邮件?还是我遗漏了什么——比如,这些电子邮件不应该同时发送吗?任何帮助将不胜感激。非常感谢您的宝贵时间和帮助!
最佳答案
根据Action Mailer Rails Guide您不需要使用“格式”方法,也应该删除“内容类型”参数。
邮件将自动检测到同时存在 html 和文本模板,并自动将电子邮件创建为 multipart/alternative
试试看:
mail( :subject => %(Invitation to join project #{@project.business_name}),
:from => %("App" <no-reply@appname.com>),
:to => @user.account.email)
关于html - RoR ActionMailer 问题——无法同时发送纯文本和 HTML 电子邮件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9120322/