class MultiMail::Message::Mailgun

@see documentation.mailgun.com/api-sending.html

Attributes

content_id_map[RW]
stripped_html[RW]
stripped_signature[RW]
stripped_text[RW]

Public Instance Methods

mailgun_attachments() click to toggle source

Returns the message's attachments in Mailgun format.

@return [Multimap] the attachments in Mailgun format @see documentation.mailgun.com/user_manual.html#inline-image

# File lib/multi_mail/mailgun/message.rb, line 32
def mailgun_attachments
  map = Multimap.new
  attachments.each do |attachment|
    key = attachment.content_type.start_with?('image/') ? 'inline' : 'attachment'
    map[key] = Faraday::UploadIO.new(StringIO.new(attachment.body.decoded), attachment.content_type, attachment.filename)
  end
  map
end
mailgun_headers() click to toggle source

Returns the message headers in Mailgun format.

@return [Hash] the message headers in Mailgun format

# File lib/multi_mail/mailgun/message.rb, line 10
def mailgun_headers
  map = Multimap.new
  hash = Hash.new

  header_fields.each do |field|
    key = field.name.downcase
    unless %w(from to cc bcc subject tag).include?(key)
      if key == 'reply-to'
        hash["h:#{field.name}"] = field.value
      else
        map["h:#{field.name}"] = field.value
      end
    end
  end

  map.to_hash.merge(hash)
end
to_mailgun_hash() click to toggle source

Returns the message as parameters to POST to Mailgun.

@return [Hash] the message as parameters to POST to Mailgun @see documentation.mailgun.com/user_manual.html#tagging

# File lib/multi_mail/mailgun/message.rb, line 45
def to_mailgun_hash
  map = Multimap.new
  hash = Hash.new

  %w(from subject).each do |field|
    if self[field]
      map[field] = self[field].value
    end
  end

  %w(to cc bcc).each do |field|
    if self[field]
      if self[field].value.respond_to?(:each)
        self[field].value.each do |value|
          map[field] = value
        end
      else
        map[field] = self[field].value
      end
    end
  end

  if body_text && !body_text.empty?
    map['text'] = body_text
  end
  if body_html && !body_html.empty?
    map['html'] = body_html
  end

  tags.each do |tag|
    map['o:tag'] = tag
  end

  normalize(map.merge(mailgun_attachments).to_hash.merge(mailgun_headers).merge(hash))
end