Sha256: a2ae4a869efae5017d29434bab77ab0346203e3b9c93d3a4e08bad98438391ac

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

require 'smtp_tls' unless Net::SMTP.instance_methods.include?("enable_starttls_auto")

##
# Adds sending email through an ActiveRecord table as a delivery method for
# ActionMailer.
#

class ActionMailer::Base

  ##
  # Set the email class for deliveries. Handle class reloading issues which prevents caching the email class.
  #
  @@email_class_name = 'Email'

  def self.email_class=(klass)
    @@email_class_name = klass.to_s
  end

  def self.email_class
    @@email_class_name.constantize
  end

  ##
  # Adds +mail+ to the Email table.  Only the first From address for +mail+ is
  # used.

  def perform_delivery_activerecord(mail)
    mail.destinations.each do |destination|
      self.class.email_class.create :mail => mail.encoded, :to => destination, :from => mail.from.first
    end
  end

  def perform_delivery_smtp(mail)
    destinations = mail.destinations
    mail.ready_to_send
    # via https://rails.lighthouseapp.com/projects/8994/tickets/2340
    # via http://github.com/rails/rails/commit/da61a6c9671239dbb4a926c3e161ca8663fa0e3f
    sender = (mail['return-path'] && mail['return-path'].spec) || Array(mail.from).first

    user_names = (smtp_settings[:user] || smtp_settings[:user_name]).to_a
    settings = [
      smtp_settings[:domain],
      user_names[rand(user_names.size)],
      smtp_settings[:password],
      smtp_settings[:authentication]
    ]

    smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
    if smtp.respond_to?(:enable_starttls_auto)
      smtp.enable_starttls_auto unless smtp_settings[:tls] == false
    else
      settings << smtp_settings[:tls]
    end

    smtp.start(*settings) do |smtp|
      smtp.sendmail(mail.encoded, sender, destinations)
    end
  end

  def perform_delivery_smtp_failover_activerecord(mail)
    perform_delivery_smtp mail
  rescue Exception => e
    perform_delivery_activerecord mail
    logger.error e
    e.backtrace.each { |line| logger.error line}
    after_failed_delivery_smtp_mail(e) if defined? self.after_failed_delivery_smtp_mail
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
maedana-ar_mailer-2.1.6.2 lib/action_mailer/ar_mailer.rb