Sha256: 345e921dc90d1ff6ea763d3f9a3c6013cfcacfbf7bc76b5583ecf3378c925260

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

# This represents a particular mail object which will need to be sent
# A mail_object requires the mail attributes and the delivery_settings

module Sinatra
  module MailerPlugin
    class MailObject
      def initialize(mail_attributes={}, smtp_settings={})
        @mail_attributes = mail_attributes.dup
        @smtp_settings = smtp_settings.dup if smtp_settings.present?
      end

      # Constructs the delivery attributes for the message and then sends the mail
      # @mail_object.deliver
      def deliver
        @mail_attributes.reverse_merge!(:via => self.delivery_method.to_sym)
        @mail_attributes.reverse_merge!(:smtp => @smtp_settings) if using_smtp?
        self.send_mail(@mail_attributes)
      end

      protected

      # Returns the delivery method to use for this mail object
      # @mo.delivery_method => :smtp || :sendmail
      def delivery_method
        @mail_attributes[:via] || (@smtp_settings.present? ? :smtp : :sendmail)
      end

      # Returns true if the mail object is going to be delivered using smtp
      def using_smtp?
        delivery_method.to_s =~ /smtp/
      end

      # Performs the actual email sending using a third-party library
      def send_mail(delivery_attributes)
        Pony.mail(delivery_attributes) && true
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
darkhelmet-sinatra_more-0.3.36 lib/sinatra/mailer_plugin/mail_object.rb
darkhelmet-sinatra_more-0.3.35 lib/sinatra/mailer_plugin/mail_object.rb
darkhelmet-sinatra_more-0.3.34 lib/sinatra/mailer_plugin/mail_object.rb
darkhelmet-sinatra_more-0.3.33 lib/sinatra/mailer_plugin/mail_object.rb