module SparkpostRails
Constants
- VERSION
Public Instance Methods
fetch_email_and_name(address)
click to toggle source
Fetch email address and name from Mail::Address object.
@param [Mail::Address] address
@return [Hash]
# File lib/sparkpost_rails/mailer.rb, line 77 def fetch_email_and_name(address) hash = {} hash[:email] = address.address if address.address hash[:name] = address.name if address.name hash end
transform_attachments(attachments)
click to toggle source
Transform array of Mail::Attachment object to array of Hash with base64 encoded attachment content
@param [Array<Mail::Attachment>] attachments
@return [Array<Hash>]
# File lib/sparkpost_rails/mailer.rb, line 62 def transform_attachments(attachments) attachments.map do |attachment| { name: attachment.filename, type: attachment.content_type, data: Base64.encode64(attachment.decoded) } end end
transform_mail(mail)
click to toggle source
Transform Mail::Message
object to Hash that will be send as request payload
@param [Mail::Message] mail
@return [Hash]
# File lib/sparkpost_rails/mailer.rb, line 29 def transform_mail(mail) content = {} sp_object = {} sp_object[:recipients] = mail[:to].address_list.addresses.map { |recipient| {address: fetch_email_and_name(recipient)} } if mail.to sp_object[:return_path] = mail[:return_path].address_list.addresses[0].raw if mail.return_path content[:from] = fetch_email_and_name(mail[:from].address_list.addresses[0]) if mail.from && mail.from[0] content[:subject] = mail.subject if mail.subject content[:reply_to] = mail[:reply_to].address_list.addresses[0].raw if mail.reply_to && mail.reply_to[0] content[:attachments] = transform_attachments(mail.attachments) if !mail.attachments.empty? ["html", "text"].each do |part| mail_part = mail.send("#{part}_part") content[part] = mail_part.decoded if mail_part end [:cc, :bcc].each do |rcpt_type| content[:headers] ||= {} rcpt_type_val = mail.send(rcpt_type) content[:headers][rcpt_type] = mail.send(rcpt_type).join(",") if rcpt_type_val end sp_object[:content] = content sp_object.deep_merge!(mail.sparkpost_payload) if mail.sparkpost_payload sp_object end