class DMCourier::Services::Sendgrid

Attributes

api_key[R]
mail[R]
options[R]

Public Class Methods

new(mail, options = {}) click to toggle source
# File lib/dm_courier/services/sendgrid.rb, line 13
def initialize(mail, options = {})
  @mail = mail
  @api_key = options.fetch(:api_key)
  @options = options
end

Public Instance Methods

deliver!() click to toggle source
# File lib/dm_courier/services/sendgrid.rb, line 23
def deliver!
  sendgrid_api = ::SendGrid::Client.new(api_key)
  sendgrid_api.send(sendgrid_message)
end
name() click to toggle source
# File lib/dm_courier/services/sendgrid.rb, line 19
def name
  :sendgrid
end
sendgrid_message() click to toggle source
# File lib/dm_courier/services/sendgrid.rb, line 28
def sendgrid_message
  message = { to: (mail[:to].formatted if mail[:to]),
              cc: (mail[:cc].formatted if mail[:cc]),
              bcc: return_string_value(:bcc_address),
              from: from_email,
              from_name: from_name,
              subject: subject,
              html: html_part,
              text: text_part,
              reply_to: reply_to }

  message[:attachments] = regular_attachments

  ::SendGrid::Mail.new(message) do |mail|
    inline_attachments.each do |attachment|
      mail.contents << attachment
    end
  end
end

Private Instance Methods

inline_attachments() click to toggle source
# File lib/dm_courier/services/sendgrid.rb, line 66
def inline_attachments
  mail.attachments.map do |attachment|
    next unless attachment.inline?

    { file: Faraday::UploadIO.new(StringIO.new(attachment.body.decoded),
                                  attachment.mime_type,
                                  attachment.filename),
      cid: attachment.cid,
      name:  attachment.filename }
  end.compact
end
regular_attachments() click to toggle source
# File lib/dm_courier/services/sendgrid.rb, line 55
def regular_attachments
  mail.attachments.map do |attachment|
    next if attachment.inline?

    { file: Faraday::UploadIO.new(StringIO.new(attachment.body.decoded),
                                  attachment.mime_type,
                                  attachment.filename),
      name:  attachment.filename }
  end.compact
end
reply_to() click to toggle source
# File lib/dm_courier/services/sendgrid.rb, line 50
def reply_to
  value = mail["Reply-To"] || options[:reply_to]
  value.to_s if value
end