class Mailpace::DeliveryMethod
MailPace ActionMailer delivery method
Attributes
settings[RW]
Public Class Methods
new(values)
click to toggle source
# File lib/mailpace-rails.rb, line 14 def initialize(values) check_api_token(values) self.settings = { return_response: true }.merge!(values) end
Public Instance Methods
deliver!(mail)
click to toggle source
# File lib/mailpace-rails.rb, line 19 def deliver!(mail) check_delivery_params(mail) result = HTTParty.post( 'https://app.mailpace.com/api/v1/send', body: { from: mail.header[:from]&.address_list&.addresses&.first.to_s, to: mail.to.join(','), subject: mail.subject, htmlbody: mail.html_part ? mail.html_part.body.decoded : mail.body.to_s, textbody: if mail.multipart? mail.text_part ? mail.text_part.body.decoded : nil end, cc: mail.cc&.join(','), bcc: mail.bcc&.join(','), replyto: mail.reply_to, list_unsubscribe: mail.header['list_unsubscribe'].to_s, attachments: format_attachments(mail.attachments), tags: mail.header['tags'].to_s }.delete_if { |_key, value| value.blank? }.to_json, headers: { 'User-Agent' => "MailPace Rails Gem v#{Mailpace::Rails::VERSION}", 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Mailpace-Server-Token' => settings[:api_token] } ) handle_response(result) end
Private Instance Methods
check_api_token(values)
click to toggle source
# File lib/mailpace-rails.rb, line 51 def check_api_token(values) return if values[:api_token].present? raise ArgumentError, 'MailPace API token is not set' end
check_delivery_params(mail)
click to toggle source
# File lib/mailpace-rails.rb, line 57 def check_delivery_params(mail) return unless mail.from.nil? || mail.to.nil? raise ArgumentError, 'Missing to or from address in email' end
format_attachments(attachments)
click to toggle source
# File lib/mailpace-rails.rb, line 71 def format_attachments(attachments) attachments.map do |attachment| { name: attachment.filename, content_type: attachment.mime_type, content: Base64.encode64(attachment.body.encoded), cid: attachment.content_id }.compact end end
handle_response(result)
click to toggle source
# File lib/mailpace-rails.rb, line 63 def handle_response(result) return result unless result.code != 200 # TODO: Improved error handling res = result.parsed_response raise res['error']&.to_s || res['errors']&.to_s end