<commit_before># By default ActionMailer delivery methods are caught by Postage.
# It makes it easy to convert without the need to interface with Postage
# directly. However, if you contruct your calls manually (not that hard, 
# check documentation) you can disable ActiveMailer on your app completely.
# Like so:
#   config.frameworks -= [ :action_mailer ]

require 'base64'

module Postage::Mailer
  
  def self.included(base)
    base.send(:include, InstanceMethods)
  end

  module InstanceMethods
    
    def perform_delivery_postage(mail)
      
      arguments = {
        :headers => {
          'Subject' => self.subject, 
          'From'    => self.from
        }.merge(self.headers),
        :parts => { }
      }
      
      # Collect the parts
      if self.parts.blank?
        arguments[:parts][self.content_type] = self.body
      else
        self.parts.each do |part|
          case part.content_disposition
          when 'inline'
            arguments[:parts][part.content_type] = part.body
          when 'attachment'
            arguments[:parts][:attachments] ||= { }
            arguments[:parts][:attachments][part.filename] = {
              :content_type => part.content_type,
              :content      => Base64.encode64(part.body)
            }
          end
        end
      end
      
      logger.info  "Sending mail via Postage..." unless logger.nil?
      
      response = Postage.send_message(
        :content    => arguments[:parts],
        :recipients => self.recipients,
        :headers    => arguments[:headers]
      )
      
      unless logger.nil?
        logger.info  "Mail successfully sent. Check postage_#{Rails.env}.log for more details. UID: #{response.response[:uid]}"
      end
      
      return response
      
    rescue => e
      Postage.log.error "Failed to perform delivery with postage: \n#{e.inspect}"
      raise e
    end
  end
end

ActionMailer::Base.send :include, Postage::Mailer
ActionMailer::Base.delivery_method = :postage unless Rails.env.test?

# Violent override of the default ActionMailer deliver! method
# maybe there's a better way of doing this.
class ActionMailer::Base
  def deliver!(mail = @mail)
    raise "no mail object available for delivery!" unless mail
    
    begin
      response = __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
    rescue Exception => e  # Net::SMTP errors or sendmail pipe errors
      raise e if raise_delivery_errors
    end
    # this is the key overide. Instead of returning somewhat useless TMail object, we are more
    # interested in the PostageApp's response
    return response
  end
end<commit_msg>checking if Rails constant is defined<commit_after># By default ActionMailer delivery methods are caught by Postage.
# It makes it easy to convert without the need to interface with Postage
# directly. However, if you contruct your calls manually (not that hard, 
# check documentation) you can disable ActiveMailer on your app completely.
# Like so:
#   config.frameworks -= [ :action_mailer ]

require 'base64'

module Postage::Mailer
  
  def self.included(base)
    base.send(:include, InstanceMethods)
  end

  module InstanceMethods
    
    def perform_delivery_postage(mail)
      
      arguments = {
        :headers => {
          'Subject' => self.subject, 
          'From'    => self.from
        }.merge(self.headers),
        :parts => { }
      }
      
      # Collect the parts
      if self.parts.blank?
        arguments[:parts][self.content_type] = self.body
      else
        self.parts.each do |part|
          case part.content_disposition
          when 'inline'
            arguments[:parts][part.content_type] = part.body
          when 'attachment'
            arguments[:parts][:attachments] ||= { }
            arguments[:parts][:attachments][part.filename] = {
              :content_type => part.content_type,
              :content      => Base64.encode64(part.body)
            }
          end
        end
      end
      
      logger.info  "Sending mail via Postage..." unless logger.nil?
      
      response = Postage.send_message(
        :content    => arguments[:parts],
        :recipients => self.recipients,
        :headers    => arguments[:headers]
      )
      
      unless logger.nil?
        logger.info  "Mail successfully sent. Check postage_#{Rails.env}.log for more details. UID: #{response.response[:uid]}"
      end
      
      return response
      
    rescue => e
      Postage.log.error "Failed to perform delivery with postage: \n#{e.inspect}"
      raise e
    end
  end
end

ActionMailer::Base.send :include, Postage::Mailer
ActionMailer::Base.delivery_method = :postage if defined?(Rails) && !Rails.env.test?

# Violent override of the default ActionMailer deliver! method
# maybe there's a better way of doing this.
class ActionMailer::Base
  def deliver!(mail = @mail)
    raise "no mail object available for delivery!" unless mail
    
    begin
      response = __send__("perform_delivery_#{delivery_method}", mail) if perform_deliveries
    rescue Exception => e  # Net::SMTP errors or sendmail pipe errors
      raise e if raise_delivery_errors
    end
    # this is the key overide. Instead of returning somewhat useless TMail object, we are more
    # interested in the PostageApp's response
    return response
  end
end
{'fraction_non_alphanumeric': 0.09111277072442121, 'fraction_numerical': 0.0022404779686333084, 'mean_word_length': 2.4561290322580644, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5032936', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1486, 'n_words': 453}