Sha256: 76c4b5cfe2f9fd5a4fc670350c7afba0fad3bec3784f1f29c550f80764852a58

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

require 'govdelivery-tms'
require 'mail'
require 'mail/check_delivery_params'
module GovDelivery::TMS
  module Mail
    # Use TMS from the mail gem or ActionMailer as a delivery method.
    #
    #   # Gemfile
    #   gem 'govdelivery-tms', :require=>'govdelivery-tms/mail/delivery_method'
    #
    #   # config/environment.rb
    #   config.action_mailer.delivery_method = :govdelivery_tms
    #   config.action_mailer.govdelivery_tms_settings = {
    #     :auth_token=>'auth_token',
    #     :api_root=>'https://stage-tms.govdelivery.com'
    #     }
    class DeliveryMethod
      include ::Mail::CheckDeliveryParams

      def initialize(values)
        self.settings = values
      end

      attr_accessor :settings

      def deliver!(mail)
        raise GovDelivery::TMS::Errors::NoRelation.new('email_messages', client) unless client.respond_to?(:email_messages)

        envelope_from = mail.return_path || mail.sender || mail.from_addrs.first

        body = case
                 when mail.html_part
                   mail.html_part.body
                 when mail.text_part
                   mail.text_part.body
                 else
                   mail.body
               end.decoded

        tms_message = client.email_messages.build(
          from_name: mail[:from].display_names.first,
          subject: mail.subject,
          body: body
        )

        mail.to.each { |recip| tms_message.recipients.build(email: recip) }
        tms_message.post!
        tms_message
      end

      def client
        @client ||= GovDelivery::TMS::Client.new(settings[:auth_token], settings)
      end
    end
  end
end

if defined?(ActionMailer)
  ActionMailer::Base.add_delivery_method :govdelivery_tms, GovDelivery::TMS::Mail::DeliveryMethod, {
    auth_token: nil,
    logger: ActionMailer::Base.logger,
    api_root: GovDelivery::TMS::Client::DEFAULTS[:api_root]}
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
govdelivery-tms-0.8.2 lib/govdelivery/tms/mail/delivery_method.rb
govdelivery-tms-0.8.1 lib/govdelivery/tms/mail/delivery_method.rb