【问题标题】:Preview emails in rails with text and html使用文本和 html 在 Rails 中预览电子邮件
【发布时间】:2016-01-27 07:32:58
【问题描述】:

在我们的 Rails 3.2 应用程序中,我们有以下设置来预览电子邮件:

examples_controller

def registration
    mail = EventMailer.registration(registration: EventRegistration.last, cache_email: false)
    return render_mail(mail)
  end

event_mailer.rb

def registration(options = {})
    @registration = options[:registration]
    @event = Event.find(@registration.event_id)
    @subject = options[:subject] || 'Your Learn Registration'

    return_email = mail(to: @registration.email, subject: @subject)

    # the email if we got it
    return_email
  end

我们刚刚添加了电子邮件的文本版本,所以现在在 app/views/event_mailer 我们有registration.html.erb 和registration.text.erb。在添加电子邮件的文本版本之前,用户可以浏览到 /webmail/examples/registration 并查看电子邮件的 html 版本。添加文本电子邮件后,它就坏了。

关于如何解决此问题的想法?我尝试在示例控制器中将 multipart 设置为 false,但这不起作用。

另外,这里是 render_mail 方法...

def render_mail(mail)
    # send it through the inline style processing
    inlined_content = InlineStyle.process(mail.body.to_s,ignore_linked_stylesheets: true)
    render(:text => inlined_content, :layout => false)
  end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 email


    【解决方案1】:

    我认为仅通过查看某些部分来调试所有这些代码并不容易,因此我将建议一个不同的解决方案。

    前段时间我写了一个 gem 来在浏览器中预览电子邮件(附件和多部分):https://github.com/markets/maily

    主要特点:

    • 通过浏览器进行可视化预览(多部分和附件)
    • 模板版(开发)
    • 电子邮件传递
    • 灵活的授权系统
    • 为电子邮件挂钩示例数据的简单方法

    它是一个 Rails 可挂载(默认在 /maily 下)引擎,因此您只需安装、配置它,就可以在浏览器中预览您的电子邮件模板。

    您可以尝试集成 Maily,您会完成很多事情,如果您有任何疑问或建议,请联系我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-02
      • 2017-01-24
      • 1970-01-01
      • 2011-07-08
      • 2023-03-07
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      相关资源
      最近更新 更多