2014-08-14 19:47:38 -04:00
|
|
|
require 'delegate'
|
|
|
|
|
2014-08-13 17:50:38 -04:00
|
|
|
module ActionMailer
|
2014-08-20 08:34:37 -04:00
|
|
|
|
2014-08-21 17:24:22 -04:00
|
|
|
# The <tt>ActionMailer::MessageDelivery</tt> class is used by
|
|
|
|
# <tt>ActionMailer::Base</tt> when creating a new mailer.
|
|
|
|
# <tt>MessageDelivery</tt> is a wrapper (+Delegator+ subclass) around a lazy
|
|
|
|
# created <tt>Mail::Message</tt>. You can get direct access to the
|
|
|
|
# <tt>Mail::Message</tt>, deliver the email or schedule the email to be sent
|
|
|
|
# through Active Job.
|
2014-08-20 08:34:37 -04:00
|
|
|
#
|
2014-08-21 17:24:22 -04:00
|
|
|
# Notifier.welcome(User.first) # an ActionMailer::MessageDelivery object
|
2014-08-20 12:41:00 -04:00
|
|
|
# Notifier.welcome(User.first).deliver_now # sends the email
|
2014-08-21 17:24:22 -04:00
|
|
|
# Notifier.welcome(User.first).deliver_later # enqueue email delivery as a job through Active Job
|
2014-08-20 12:41:00 -04:00
|
|
|
# Notifier.welcome(User.first).message # a Mail::Message object
|
2014-08-13 17:50:38 -04:00
|
|
|
class MessageDelivery < Delegator
|
2014-08-20 08:34:37 -04:00
|
|
|
def initialize(mailer, mail_method, *args) #:nodoc:
|
2014-08-13 17:50:38 -04:00
|
|
|
@mailer = mailer
|
|
|
|
@mail_method = mail_method
|
|
|
|
@args = args
|
|
|
|
end
|
|
|
|
|
2014-08-20 08:34:37 -04:00
|
|
|
def __getobj__ #:nodoc:
|
2015-10-30 17:53:05 -04:00
|
|
|
@obj ||= begin
|
|
|
|
mailer = @mailer.new
|
|
|
|
mailer.process @mail_method, *@args
|
|
|
|
mailer.message
|
|
|
|
end
|
2014-08-13 17:50:38 -04:00
|
|
|
end
|
|
|
|
|
2014-08-20 08:34:37 -04:00
|
|
|
def __setobj__(obj) #:nodoc:
|
2014-08-13 17:50:38 -04:00
|
|
|
@obj = obj
|
|
|
|
end
|
|
|
|
|
2014-08-20 08:34:37 -04:00
|
|
|
# Returns the Mail::Message object
|
|
|
|
def message
|
2014-08-13 17:50:38 -04:00
|
|
|
__getobj__
|
|
|
|
end
|
|
|
|
|
2014-08-21 17:24:22 -04:00
|
|
|
# Enqueues the email to be delivered through Active Job. When the
|
|
|
|
# job runs it will send the email using +deliver_now!+. That means
|
|
|
|
# that the message will be sent bypassing checking +perform_deliveries+
|
|
|
|
# and +raise_delivery_errors+, so use with caution.
|
2014-08-20 08:34:37 -04:00
|
|
|
#
|
2014-09-02 04:57:23 -04:00
|
|
|
# Notifier.welcome(User.first).deliver_later!
|
2014-09-04 01:08:06 -04:00
|
|
|
# Notifier.welcome(User.first).deliver_later!(wait: 1.hour)
|
|
|
|
# Notifier.welcome(User.first).deliver_later!(wait_until: 10.hours.from_now)
|
2014-08-20 08:34:37 -04:00
|
|
|
#
|
2014-08-21 17:24:22 -04:00
|
|
|
# Options:
|
|
|
|
#
|
2014-09-04 01:08:06 -04:00
|
|
|
# * <tt>:wait</tt> - Enqueue the email to be delivered with a delay
|
|
|
|
# * <tt>:wait_until</tt> - Enqueue the email to be delivered at (after) a specific date / time
|
2014-10-18 20:35:00 -04:00
|
|
|
# * <tt>:queue</tt> - Enqueue the email on the specified queue
|
2014-08-13 17:50:38 -04:00
|
|
|
def deliver_later!(options={})
|
2014-08-20 04:34:57 -04:00
|
|
|
enqueue_delivery :deliver_now!, options
|
2014-08-13 17:50:38 -04:00
|
|
|
end
|
|
|
|
|
2014-08-21 17:24:22 -04:00
|
|
|
# Enqueues the email to be delivered through Active Job. When the
|
|
|
|
# job runs it will send the email using +deliver_now+.
|
2014-08-20 08:34:37 -04:00
|
|
|
#
|
2014-08-20 12:41:00 -04:00
|
|
|
# Notifier.welcome(User.first).deliver_later
|
2014-09-04 01:08:06 -04:00
|
|
|
# Notifier.welcome(User.first).deliver_later(wait: 1.hour)
|
|
|
|
# Notifier.welcome(User.first).deliver_later(wait_until: 10.hours.from_now)
|
2014-08-20 08:34:37 -04:00
|
|
|
#
|
2014-08-21 17:24:22 -04:00
|
|
|
# Options:
|
|
|
|
#
|
2015-09-28 11:47:44 -04:00
|
|
|
# * <tt>:wait</tt> - Enqueue the email to be delivered with a delay.
|
|
|
|
# * <tt>:wait_until</tt> - Enqueue the email to be delivered at (after) a specific date / time.
|
|
|
|
# * <tt>:queue</tt> - Enqueue the email on the specified queue.
|
2014-08-13 17:50:38 -04:00
|
|
|
def deliver_later(options={})
|
2014-08-20 04:34:57 -04:00
|
|
|
enqueue_delivery :deliver_now, options
|
|
|
|
end
|
|
|
|
|
2014-08-21 17:24:22 -04:00
|
|
|
# Delivers an email without checking +perform_deliveries+ and +raise_delivery_errors+,
|
|
|
|
# so use with caution.
|
2014-08-20 08:34:37 -04:00
|
|
|
#
|
2014-08-20 12:41:00 -04:00
|
|
|
# Notifier.welcome(User.first).deliver_now!
|
2014-08-20 08:34:37 -04:00
|
|
|
#
|
2014-08-20 04:34:57 -04:00
|
|
|
def deliver_now!
|
|
|
|
message.deliver!
|
|
|
|
end
|
|
|
|
|
2014-08-21 17:24:22 -04:00
|
|
|
# Delivers an email:
|
2014-08-20 08:34:37 -04:00
|
|
|
#
|
2014-08-20 12:41:00 -04:00
|
|
|
# Notifier.welcome(User.first).deliver_now
|
2014-08-20 08:34:37 -04:00
|
|
|
#
|
2014-08-20 04:34:57 -04:00
|
|
|
def deliver_now
|
|
|
|
message.deliver
|
|
|
|
end
|
|
|
|
|
2014-08-13 17:50:38 -04:00
|
|
|
private
|
2014-08-20 08:34:37 -04:00
|
|
|
|
|
|
|
def enqueue_delivery(delivery_method, options={})
|
2015-07-07 16:11:20 -04:00
|
|
|
args = @mailer.name, @mail_method.to_s, delivery_method.to_s, *@args
|
2014-09-04 01:08:06 -04:00
|
|
|
ActionMailer::DeliveryJob.set(options).perform_later(*args)
|
2014-08-13 17:50:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|