ruby-on-rails - 邮件程序默认为

标签 ruby-on-rails ruby-on-rails-4

我有一个管理员电子邮件:

class AdminMailer < ApplicationMailer
  ADMIN_EMAIL = 'admin@gmail.com'

  def send1
    mail(to: ADMIN_EMAIL, subject: 'You have a new registered user')
  end

end

因为它是一个管理员电子邮件,我想知道,是否可以让邮件默认将电子邮件发送给管理员?像这样的东西:

class AdminMailer < ApplicationMailer
  default to: 'admin@gmail.com'

我从未见过 to 选项,它存在吗?

最佳答案

是的,默认值可以设置为 :to 参数。

来自 rails 代码库的相关部分 # 你可以为上面的任何 header 设置默认值(+:date+ 除外)

请参阅 https://github.com/rails/rails/blob/f6b21d48ef15d4f39a530653c2ce7d0cfb458b46/actionmailer/lib/action_mailer/base.rb#L702-L729 处的代码注释

# The main method that creates the message and renders the email templates. There are
# two ways to call this method, with a block, or without a block.
#
# It accepts a headers hash. This hash allows you to specify
# the most used headers in an email message, these are:
#
# * +:subject+ - The subject of the message, if this is omitted, Action Mailer will
#   ask the Rails I18n class for a translated +:subject+ in the scope of
#   <tt>[mailer_scope, action_name]</tt> or if this is missing, will translate the
#   humanized version of the +action_name+
# * +:to+ - Who the message is destined for, can be a string of addresses, or an array
#   of addresses.
# * +:from+ - Who the message is from
# * +:cc+ - Who you would like to Carbon-Copy on this email, can be a string of addresses,
#   or an array of addresses.
# * +:bcc+ - Who you would like to Blind-Carbon-Copy on this email, can be a string of
#   addresses, or an array of addresses.
# * +:reply_to+ - Who to set the Reply-To header of the email to.
# * +:date+ - The date to say the email was sent on.
#
# You can set default values for any of the above headers (except +:date+)
# by using the ::default class method:
#
#  class Notifier < ActionMailer::Base
#    default from: 'no-reply@test.lindsaar.net',
#            bcc: 'email_logger@test.lindsaar.net',
#            reply_to: 'bounces@test.lindsaar.net'
#  end

关于ruby-on-rails - 邮件程序默认为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27831411/

相关文章:

regex - Rails验证RGB十六进制

ruby-on-rails - 根据属性值从 Ruby 中的数组中删除/删除对象

ruby-on-rails - 如何在 Rails 3 中使用 i18n 键作为默认翻译?

ruby-on-rails - 为什么 YAML.safe_load 在 YAML 别名上失败?

ruby-on-rails - Rails 4 表单数据保存为 Nil

ruby-on-rails - RoR 中的 View 和布局有什么区别?

ruby-on-rails-4 - Rails 4 - 如何为嵌套资源添加索引路由,以便列出独立于父资源的所有项目

ruby-on-rails-4 - HTML 中的 React 代码出现神秘的跨度

ruby-on-rails-4 - Doorkeeper::AuthorizationsController#new 中的 NoMethodError - Doorkeeper

ruby-on-rails - 如何呈现 json : in all controller actions Rails 4. 2.1