【问题标题】:ActionMailer without template and multipart email没有模板和多部分电子邮件的 ActionMailer
【发布时间】:2018-09-12 17:49:48
【问题描述】:

如果存在多种模板类型,Rails 将有助于发送多部分电子邮件(例如,.txt.html 文件用于相同的邮件操作)。

但是,如果我想在没有模板的情况下执行此操作怎么办?通常我们指定 body 和 content_type 作为参数:

mail to: 'a@example.com', subject: 'Hello', body: 'Hi', content_type: 'text/html'

那么如何通过多个具有自己类型的主体来实现呢?

【问题讨论】:

    标签: ruby-on-rails email actionmailer multipart


    【解决方案1】:
    class TestMailer < ActionMailer::Base
      def welcome_email
        mail(to: 'example@example.com', subject: 'Welcome to My Awesome Site') do |format|
          format.html { render html: '<h1>Welcome XYZ!</h1>'.html_safe }
          format.text { render plain: 'Welcome XYZ' }
        end
      end
    end
    

    要使用它,请致电:TestMailer.welcome_email.deliver_now

    Documentation,第 2.4 节

    【讨论】:

    • 这在 Rails 4.2 和 5.0 中运行良好,但在 5.1 中失败:ActionView::MissingTemplate。文档section 2.11 解释了要在没有模板的情况下发送,请在 mail() 调用中使用 body: 参数,但它没有解释如何处理多部分电子邮件。
    • @Brent 我用 Rails 5.1.5 进行了检查,没有引发异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 2019-05-02
    • 1970-01-01
    • 2013-02-13
    • 2013-06-17
    • 1970-01-01
    相关资源
    最近更新 更多