轨道邮件与不同的布局 [英] rails mailer with different layouts
问题描述
我在通知器模型(20多个电子邮件)中为我的所有电子邮件使用一个布局...但是有时我只想发送一个没有布局或HTML的纯文本电子邮件。我似乎无法弄清楚怎么样?如果我尝试发送一个纯文本电子邮件,我仍然会得到电子邮件中的所有HTML。
我使用的是Rails 2.3.8。 >
我在这里阅读了这个猴子补丁,但似乎表明一个较新版本的rails已经过来了?如果我可以避免一个麻烦,我真的不想要猴子补丁。
layoutemail#use email.text。(html | plain).erb作为布局
def welcome_email(property)
主题'新注册'
收件人property.email
from'welcome@test.com'
body:property =>属性
content_typetext / html
end
def send_inquiry(询问)
主题#{inquire.the_subject}
收件人查询.email
来自Test代表#{inquire.name}<#{inquire.email}>
body:inquire =>查询
content_typetext / plain
end
也有2个档案。
email.text.html.erb
email.text.plain.erb
它始终使用text.html.erb ...即使content_type是text / plain
编辑:对于电子邮件模板,布局遵循不同的命名方案。只需重命名它们如下:
layout.text.html.erb => layout.html.erb
layout.text.plain.erb => layout.text.erb
如果您使用这一点,我也犯了手动定义部件的错误:
part:content_type => 'text / plain',
:body => render_message('my_template')
然后Rails无法确定你的部分的content_type,它假定它是HTML。
当我改变了这两件事情,它为我工作!
原来的回覆跟着..
过去很多次,我一直在努力解决这个问题,通常会以某种非干燥的速度和肮脏解。我一直认为我是唯一有这个问题的人,因为Google对这个问题没有任何有用的东西。
这一次我决定挖掘Rails来计算出来到目前为止没有太多的成功,但也许我的发现将帮助别人了解这一点。
我发现是在ActionMailer ::基础#render_message方法被任务确定适当的content_type,并将其分配给@current_template_content_type。 #default_template_format然后为布局返回正确的mime类型,或者如果@current_template_content_type未设置,则默认为:html。
这是ActionMailer :: base#render_message看起来像我的应用程序(2.3.5)
def render_message(method_name,body)
如果method_name .respond_to?(:content_type)
@current_template_content_type = method_name.content_type
end
render:file => method_name,:body =>身体
确保
@current_template_content_type = nil
end
麻烦是method_name看起来是一个字符串(本地视图的名称,在我的例子中是new_password.text.html),字符串当然不会回覆到#content_type,意味着@current_template_content_type将始终保持为零,所以#default_template_format将始终默认为:html。
我不知道更接近实际的解决方案。 ActionMailer内部对我来说太不透明。
I use one layout for all my emails in my Notifier model (20+ emails)... however sometimes I just want to send a plain text email with no layout or html at all. I can't seem to be able to figure out how? If I try to send a plain text email i still get the layout, and all the HTML in the email.
I'm using Rails 2.3.8.
I read about this monkey patch here... but it seemed to indicate a newer version of rails had over come this? And I don't really wanna monkey patch if I can avoid one.
Rails - setting multiple layouts for a multipart email with mailer templates
layout "email" # use email.text.(html|plain).erb as the layout
def welcome_email(property)
subject 'New Signup'
recipients property.email
from 'welcome@test.com'
body :property => property
content_type "text/html"
end
def send_inquiry(inquire)
subject "#{inquire.the_subject}"
recipients inquire.ob.email
from "Test on behalf of #{inquire.name} <#{inquire.email}>"
body :inquire => inquire
content_type "text/plain"
end
I also have 2 files.
email.text.html.erb
email.text.plain.erb
It always uses text.html.erb... even if the content_type is "text/plain"
edit: Figured it out, the layouts follow a different naming scheme to the email templates. Just rename them as follows:
layout.text.html.erb => layout.html.erb
layout.text.plain.erb => layout.text.erb
I also made the mistake of manually defining the parts, if you use this:
part :content_type => 'text/plain',
:body => render_message('my_template')
Then Rails can't determine the content_type for your part and it assumes it's HTML.
After I changed those two things it worked for me!
original reply follows..
I've struggled with this question many times in the past, usually ending up with some sort of non-dry quick and dirty solution. I always thought I was the only one with this problem because Google turns up exactly nothing useful on the subject.
This time I decided to dig into Rails to figure it out but so far without much success, but maybe my findings will help someone else figure this out.
What I found was that in ActionMailer::Base the #render_message method is tasked with determining the proper content_type and should assign it to @current_template_content_type. #default_template_format then either returns the proper mime type for the layout or, if @current_template_content_type isn't set, it will default to :html.
This is what ActionMailer::Base#render_message looks like in my app (2.3.5)
def render_message(method_name, body)
if method_name.respond_to?(:content_type)
@current_template_content_type = method_name.content_type
end
render :file => method_name, :body => body
ensure
@current_template_content_type = nil
end
The trouble is that method_name appears to be a string (the name of the local view, in my case "new_password.text.html") and strings of course do not respond_to #content_type, meaning @current_template_content_type will always remain nil, and so the #default_template_format will always default to :html.
Not much closer to an actual solution, I know. The ActionMailer internals are just too opaque for me.
这篇关于轨道邮件与不同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!