Sha256: b700d22ea9953c4754a1530ffc8f74414b1e04050646b449f7c50f42a5bc167b

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

module Padrino
  module Mailer
    ##
    # This represents a particular mail object which will need to be sent
    # A mail_object requires the mail attributes and the delivery_settings
    #
    class MailObject
      ##
      # Initialize a new MailObject
      #
      # ==== Examples
      #
      #   Padrino::Mailer::MailObject.new(
      #     :subject => "Hey this is my subject",
      #     :to => "info@padrinorb.org",
      #     :from => "foo@bar.com",
      #     :body => "This is my body"
      #   ).deliver
      #
      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
      #
      # ==== Examples
      #
      #   @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
        #
        # ==== Examples
        #
        # @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
        #
        def send_mail(delivery_attributes)
          Delivery.mail(delivery_attributes) && true
        end
    end # MailObject
  end # Mailer
end # Padrino

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
padrino-mailer-0.9.10 lib/padrino-mailer/mail_object.rb
padrino-mailer-0.9.9 lib/padrino-mailer/mail_object.rb
padrino-mailer-0.9.7 lib/padrino-mailer/mail_object.rb