Class: MultiMail::Receiver::Cloudmailin

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/multi_mail/cloudmailin/receiver.rb

Overview

Cloudmailin's incoming email receiver.

Cloudmailin recommends using basic authentication over HTTPS to ensure that a request originates from Cloudmailin.

Instance Method Summary collapse

Methods included from Base

#process, #valid?

Constructor Details

#initialize(options = {}) ⇒ Cloudmailin

Initializes a Cloudmailin incoming email receiver.

Options Hash (options):

  • :http_post_format (String)

    "multipart", "json" or "raw"



18
19
20
21
# File 'lib/multi_mail/cloudmailin/receiver.rb', line 18

def initialize(options = {})
  super
  @http_post_format = options[:http_post_format]
end

Instance Method Details

#spam?(message) ⇒ Boolean



85
86
87
# File 'lib/multi_mail/cloudmailin/receiver.rb', line 85

def spam?(message)
  message['spf-result'] && message['spf-result'].value == 'fail'
end

#transform(params) ⇒ Array<Mail::Message>

Returns messages.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/multi_mail/cloudmailin/receiver.rb', line 28

def transform(params)
  case @http_post_format
  when 'raw', '', nil
    message = self.class.condense(Mail.new(params['message']))

    # Extra Cloudmailin parameters.
    message['spf-result'] = params['envelope']['spf']['result']

    # Discard rest of `envelope`: `from`, `to`, `recipients`,
    # `helo_domain` and `remote_ip`.
    [message]
  when 'multipart', 'json'
    # Mail changes `self`.
    headers = self.class.multimap(params['headers'])
    http_post_format = @http_post_format
    this = self

    message = Mail.new do
      headers headers

      text_part do
        body params['plain']
      end

      if params.key?('html')
        html_part do
          content_type 'text/html; charset=UTF-8'
          body params['html']
        end
      end

      if params.key?('attachments')
        if http_post_format == 'json'
          params['attachments'].each do |attachment|
            add_file(:filename => attachment['file_name'], :content => Base64.decode64(attachment['content']))
          end
        else
          params['attachments'].each do |_,attachment|
            add_file(this.class.add_file_arguments(attachment))
          end
        end
      end
    end

    # Extra Cloudmailin parameters. The multipart format uses CRLF whereas
    # the JSON format uses LF. Normalize to LF.
    message['reply_plain'] = params['reply_plain'].gsub("\r\n", "\n")
    message['spf-result']  = params['envelope']['spf']['result']

    [message]
  else
    raise ArgumentError, "Can't handle Cloudmailin #{@http_post_format} HTTP POST format"
  end
end