邮件程序默认为
Mailer default to
我有一个管理员邮箱:
class AdminMailer < ApplicationMailer
ADMIN_EMAIL = 'admin@gmail.com'
def send1
mail(to: ADMIN_EMAIL, subject: 'You have a new registered user')
end
end
既然是管理员邮箱,请问是否可以让mail默认发邮件给管理员?类似于:
class AdminMailer < ApplicationMailer
default to: 'admin@gmail.com'
我从未见过选项 to
,它存在吗?
是的,默认值可以设置为 :to
参数。
来自 rails 代码库的相关部分 # You can set default values for any of the above headers (except +:date+)
处的代码注释
# 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
编辑:实际上我将 mail
包装到设置默认选项的 mail_to_user
中,因为此方法会发送重复的电子邮件
您可以在后续操作中完成:
class ApplicationMailer < ActionMailer::Base
after_action :set_default_to
after_action :set_default_subject
def set_default_to
mail.to ||= user_email(current_user) if current_user
end
def set_default_subject
mail.subject ||= I18n.t(subject_key)
end
end
我有一个管理员邮箱:
class AdminMailer < ApplicationMailer
ADMIN_EMAIL = 'admin@gmail.com'
def send1
mail(to: ADMIN_EMAIL, subject: 'You have a new registered user')
end
end
既然是管理员邮箱,请问是否可以让mail默认发邮件给管理员?类似于:
class AdminMailer < ApplicationMailer
default to: 'admin@gmail.com'
我从未见过选项 to
,它存在吗?
是的,默认值可以设置为 :to
参数。
来自 rails 代码库的相关部分 # You can set default values for any of the above headers (except +:date+)
# 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
编辑:实际上我将 mail
包装到设置默认选项的 mail_to_user
中,因为此方法会发送重复的电子邮件
您可以在后续操作中完成:
class ApplicationMailer < ActionMailer::Base
after_action :set_default_to
after_action :set_default_subject
def set_default_to
mail.to ||= user_email(current_user) if current_user
end
def set_default_subject
mail.subject ||= I18n.t(subject_key)
end
end