class Mailgems::Message

Constants

ERROR_MSG

Attributes

api_key[R]
mail[R]
sandbox[R]

Public Class Methods

new(mail, settings) click to toggle source
# File lib/mailgems/message.rb, line 8
def initialize(mail, settings)
  @mail = mail
  @api_key = settings[:api_key]
  @sandbox = settings[:sandbox]

  raise ArgumentError, ERROR_MSG % 'Api key' unless @api_key
end

Public Instance Methods

send() click to toggle source
# File lib/mailgems/message.rb, line 16
def send
  Mail.new(api_key: api_key).send_mail(params)
end

Private Instance Methods

attachment_body(attachment) click to toggle source
# File lib/mailgems/message.rb, line 94
def attachment_body(attachment)
  {
    filename: attachment.filename,
    content: Base64.strict_encode64(attachment.decoded)
  }
end
attachments() click to toggle source
# File lib/mailgems/message.rb, line 75
def attachments
  mail.attachments.map do |attachment|
    if inline?(attachment)
      inline_attachment_body(attachment)
    else
      attachment_body(attachment)
    end
  end.compact
end
bcc() click to toggle source
# File lib/mailgems/message.rb, line 71
def bcc
  mail[:bcc]&.addrs&.map(&:address)
end
cc() click to toggle source
# File lib/mailgems/message.rb, line 67
def cc
  mail[:cc]&.addrs&.map(&:address)
end
content() click to toggle source
# File lib/mailgems/message.rb, line 53
def content
  return mail.html_part.decoded if mail.html_part

  mail.text_part&.decoded
end
from_email() click to toggle source
# File lib/mailgems/message.rb, line 39
def from_email
  mail[:from].addrs.first.address
end
from_name() click to toggle source
# File lib/mailgems/message.rb, line 43
def from_name
  mail[:from].addrs.first.name
end
inline?(attachment) click to toggle source
# File lib/mailgems/message.rb, line 101
def inline?(attachment)
  attachment.content_disposition.starts_with? 'inline'
end
inline_attachment_body(attachment) click to toggle source
# File lib/mailgems/message.rb, line 85
def inline_attachment_body(attachment)
  {
    filename: attachment.filename,
    content: Base64.strict_encode64(attachment.decoded),
    content_id: attachment['Content-ID'].element.message_ids.first,
    inline: true
  }
end
params() click to toggle source
# File lib/mailgems/message.rb, line 22
def params
  {
    recipients: recipients, from_email: from_email, from_name: from_name,
    subject: subject, content: content, server: server, cc: cc, bcc: bcc,
    attachments: attachments, template: template, is_test: sandbox
  }
end
recipients() click to toggle source
# File lib/mailgems/message.rb, line 30
def recipients
  mail.to.map do |to|
    {
      email: to,
      attributes: mail.header['merge_data']&.unparsed_value
    }
  end
end
server() click to toggle source
# File lib/mailgems/message.rb, line 63
def server
  mail.header['server']&.value
end
subject() click to toggle source
# File lib/mailgems/message.rb, line 47
def subject
  return nil if content.nil? && !(template.nil? || template.empty?)

  mail.subject
end
template() click to toggle source
# File lib/mailgems/message.rb, line 59
def template
  mail.body&.raw_source
end