class Mail::Message

Attributes

delivered[RW]
postmark_response[RW]
template_model[W]

Public Instance Methods

body_html() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 129
def body_html
  if multipart? && html_part
    html_part.decoded
  elsif html?
    decoded
  end
end
body_text() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 137
def body_text
  if multipart? && text_part
    text_part.decoded
  elsif text? && !html?
    decoded
  elsif !html?
    body.decoded
  end
end
delivered?() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 6
def delivered?
  self.delivered
end
export_attachments() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 147
def export_attachments
  export_native_attachments + postmark_attachments
end
export_headers() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 151
def export_headers
  [].tap do |headers|
    self.header.fields.each do |field|
      key, value = field.name, field.value
      next if reserved_headers.include? key.downcase
      headers << { "Name" => key, "Value" => value }
    end
  end
end
html?() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 125
def html?
  text? && !!(sub_type =~ /^html$/i)
end
message_stream(val = nil) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 75
def message_stream(val = nil)
  self.message_stream = val unless val.nil?
  header['MESSAGE-STREAM'].to_s
end
message_stream=(val) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 80
def message_stream=(val)
  header['MESSAGE-STREAM'] = val
end
metadata(val = nil) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 36
def metadata(val = nil)
  if val
    @metadata = val
  else
    @metadata ||= {}
  end
end
metadata=(val) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 44
def metadata=(val)
  @metadata = val
end
postmark_attachments() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 55
def postmark_attachments
  return [] if @_attachments.nil?
  Kernel.warn("Mail::Message#postmark_attachments is deprecated and will " \
              "be removed in the future. Please consider using the native " \
              "attachments API provided by Mail library.")

  ::Postmark::MessageHelper.attachments_to_postmark(@_attachments)
end
postmark_attachments=(value) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 48
def postmark_attachments=(value)
  Kernel.warn("Mail::Message#postmark_attachments= is deprecated and will " \
              "be removed in the future. Please consider using the native " \
              "attachments API provided by Mail library.")
  @_attachments = value
end
prerender() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 88
def prerender
  raise ::Postmark::Error, 'Cannot prerender a message without an associated template alias' unless templated?

  unless delivery_method.is_a?(::Mail::Postmark)
    raise ::Postmark::MailAdapterError, "Cannot render templates via #{delivery_method.class} adapter."
  end

  client = delivery_method.api_client
  template = client.get_template(template_alias)
  response = client.validate_template(template.merge(:test_render_model => template_model || {}))

  raise ::Postmark::InvalidTemplateError, response unless response[:all_content_is_valid]

  self.body = nil

  subject response[:subject][:rendered_content]

  text_part do
    body response[:text_body][:rendered_content]
  end

  html_part do
    content_type 'text/html; charset=UTF-8'
    body response[:html_body][:rendered_content]
  end

  self
end
tag(val = nil) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 10
def tag(val = nil)
  default 'TAG', val
end
tag=(val) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 14
def tag=(val)
  header['TAG'] = val
end
template_alias(val = nil) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 64
def template_alias(val = nil)
  return self[:postmark_template_alias] && self[:postmark_template_alias].to_s if val.nil?
  self[:postmark_template_alias] = val
end
template_model(model = nil) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 70
def template_model(model = nil)
  return @template_model if model.nil?
  @template_model = model
end
templated?() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 84
def templated?
  !!template_alias
end
text?() click to toggle source
Calls superclass method
# File lib/postmark/message_extensions/mail.rb, line 117
def text?
  if defined?(super)
    super
  else
    has_content_type? ? !!(main_type =~ /^text$/i) : false
  end
end
to_postmark_hash() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 161
def to_postmark_hash
  ready_to_send!
  ::Postmark::MailMessageConverter.new(self).run
end
track_opens(val = nil) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 27
def track_opens(val = nil)
  self.track_opens=(val) unless val.nil?
  header['TRACK-OPENS'].to_s
end
track_opens=(val) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 32
def track_opens=(val)
  header['TRACK-OPENS'] = (!!val).to_s
end

Protected Instance Methods

export_native_attachments() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 172
def export_native_attachments
  attachments.map do |attachment|
    basics = {"Name" => attachment.filename,
              "Content" => pack_attachment_data(attachment.body.decoded),
              "ContentType" => attachment.mime_type}
    specials = attachment.inline? ? {'ContentID' => attachment.url} : {}

    basics.update(specials)
  end
end
pack_attachment_data(data) click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 168
def pack_attachment_data(data)
  ::Postmark::MessageHelper.encode_in_base64(data)
end
reserved_headers() click to toggle source
# File lib/postmark/message_extensions/mail.rb, line 183
def reserved_headers
  %q[
    return-path  x-pm-rcpt
    from         reply-to
    sender       received
    date         content-type
    cc           bcc
    subject      tag
    attachment   to
    track-opens  track-links
    postmark-template-alias
    message-stream
  ]
end