Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailer default to

I have an Admin emailer:

class AdminMailer < ApplicationMailer
  ADMIN_EMAIL = '[email protected]'

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

end

Since it's an admin emailer, I wonder, is it possible to make mail send the emails to admin by default? Something like:

class AdminMailer < ApplicationMailer
  default to: '[email protected]'

I've never ever seen the option to, does it exist?

like image 575
Incerteza Avatar asked Aug 11 '26 06:08

Incerteza


1 Answers

Yup, default value can be set to :to parameter.

From the relevant part of the rails code base # You can set default values for any of the above headers (except +:date+)

See the code comments at 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: '[email protected]',
#            bcc: '[email protected]',
#            reply_to: '[email protected]'
#  end
like image 76
Prakash Murthy Avatar answered Aug 12 '26 20:08

Prakash Murthy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!