加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 具有不同布局的导轨邮件程序

发布时间:2020-12-17 04:30:51 所属栏目:百科 来源:网络整理
导读:我在Notifier模型中使用了一个布局用于我的所有电子邮件(20封电子邮件)…但有时我只想发送一个没有布局或html的纯文本电子邮件.我似乎无法弄清楚如何?如果我尝试发送纯文本邮件,我仍然可以获得布局,以及电子邮件中的所有 HTML. 我正在使用Rails 2.3.8. 我在
我在Notifier模型中使用了一个布局用于我的所有电子邮件(20封电子邮件)…但有时我只想发送一个没有布局或html的纯文本电子邮件.我似乎无法弄清楚如何?如果我尝试发送纯文本邮件,我仍然可以获得布局,以及电子邮件中的所有 HTML.

我正在使用Rails 2.3.8.

我在这里读到了这个猴子补丁……但它似乎表明有更新版本的导轨已经过来了吗?如果我可以避免补丁,我真的不想要补丁.

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

我也有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.

在我改变了这两件事后,它对我有用!

原始回复如下..

我过去曾经多次努力解决这个问题,通常最终会遇到某种非干燥的快速和肮脏的解决方案.我一直认为我是唯一一个有这个问题的人,因为谷歌在这个问题上没有任何用处.

这次我决定深入研究Rails,但到目前为止还没有取得多大成功,但也许我的研究结果将帮助其他人解决这个问题.

我发现在ActionMailer :: Base中,#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)
    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

问题是method_name似乎是一个字符串(本地视图的名称,在我的情况下是“new_password.text.html”),字符串当然不响应#to_content_type,这意味着@current_template_content_type将始终保持为零,因此#default_template_format将始终默认为:html.

我知道,并没有更接近实际的解决方案. ActionMailer内部对我来说太不透明了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读