没有模板和多部分电子邮件的 ActionMailer

ActionMailer without template and multipart email

如果存在多个模板类型,

Rails 将有助于发送多部分电子邮件(例如,.txt.html 文件用于相同的邮件操作)。

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

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

那么如何使用具有自己类型的多个主体来实现这一点呢?

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 节