Sha256: 134eb761d10dbaf04cce52f09a581f5b1cae7a1ea983c5adfbc35a11f7d03fa0

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

module MultiMail
  module Receiver
    # Postmark's incoming email receiver.
    class Postmark
      include MultiMail::Receiver::Base

      def transform(params)
        headers = Multimap.new
        params['Headers'].each do |header|
          headers[header['Name']] = header['Value']
        end

        # Due to scoping issues, we can't call `transform_address` within `Mail.new`.
        from = transform_address(params['FromFull'])
        to   = params['ToFull'].map{|hash| transform_address(hash)}
        cc   = params['CcFull'].map{|hash| transform_address(hash)}

        message = Mail.new do
          headers headers

          from      from
          to        to
          cc        cc
          reply_to  params['ReplyTo']
          subject   params['Subject']
          date      params['Date']

          text_part do
            body params['TextBody']
          end

          html_part do
            content_type 'text/html; charset=UTF-8'
            body CGI.unescapeHTML(params['HtmlBody'])
          end

          params['Attachments'].each do |attachment|
            add_file(:filename => attachment['Name'], :content => Base64.decode64(attachment['Content']))
          end
        end

        # Extra Postmark parameters.
        %w(MailboxHash MessageID Tag).each do |key|
          if params.key?(key) && !params[key].empty?
            message[key] = params[key]
          end
        end

        [message]
      end

      # @param [Mail::Message] message a message
      # @return [Boolean] whether the message is spam
      # @see http://developer.postmarkapp.com/developer-inbound-parse.html#spam
      def spam?(message)
        message['X-Spam-Status'].value == 'Yes'
      end

    private

      def transform_address(hash)
        address = Mail::Address.new(hash['Email'])
        address.display_name = hash['Name']
        address.to_s
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
multi_mail-0.1.0 lib/multi_mail/postmark/receiver.rb