Class: MultiMail::Receiver::Mailgun

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

Overview

Mailgun's incoming email receiver.

Instance Method Summary collapse

Methods included from Base

included, #process

Constructor Details

#initialize(options = {}) ⇒ Mailgun

Initializes a Mailgun incoming email receiver.

Parameters:

  • options (Hash) (defaults to: {})

    required and optional arguments

Options Hash (options):

  • :mailgun_api_key (String)

    a Mailgun API key

  • :http_post_format (String)

    "parsed" or "raw"



14
15
16
17
18
# File 'lib/multi_mail/mailgun/receiver.rb', line 14

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

Instance Method Details

#spam?(message) ⇒ Boolean

Note:

You must enable spam filtering for each domain in Mailgun's Control Panel.

Note:

We may also inspect X-Mailgun-SScore and X-Mailgun-Spf, whose possible values are "Pass", "Neutral", "Fail" and "SoftFail".

Returns whether a message is spam.

Parameters:

Returns:

  • (Boolean)

    whether the message is spam

See Also:



117
118
119
# File 'lib/multi_mail/mailgun/receiver.rb', line 117

def spam?(message)
  message['X-Mailgun-Sflag'] && message['X-Mailgun-Sflag'].value == 'Yes'
end

#transform(params) ⇒ Array<MultiMail::Message::Mailgun>

Transforms the content of Mailgun's webhook into a list of messages.



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/multi_mail/mailgun/receiver.rb', line 40

def transform(params)
  case @http_post_format
  when 'parsed', '', nil
    # Mail changes `self`.
    headers = self.class.multimap(JSON.load(params['message-headers']))
    this = self

    message = Message::Mailgun.new do
      headers headers

      # The following are redundant with `body-mime` in raw MIME format
      # and with `message-headers` in fully parsed format.
      #
      # from    params['from']
      # sender  params['sender']
      # to      params['recipient']
      # subject params['subject']
      #
      # Mailgun POSTs all MIME headers both individually and in
      # `message-headers`.

      text_part do
        body params['body-plain']
      end

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

      if params.key?('attachment-count')
        1.upto(params['attachment-count'].to_i) do |n|
          attachment = params["attachment-#{n}"]
          add_file(this.class.add_file_arguments(attachment))
        end
      end
    end

    # Extra Mailgun parameters.
    if params.key?('stripped-text') && !params['stripped-text'].empty?
      message.stripped_text = params['stripped-text']
    end
    if params.key?('stripped-signature') && !params['stripped-signature'].empty?
      message.stripped_signature = params['stripped-signature']
    end
    if params.key?('stripped-html') && !params['stripped-html'].empty?
      message.stripped_html = params['stripped-html']
    end
    if params.key?('content-id-map') && !params['content-id-map'].empty?
      message.content_id_map = params['content-id-map']
    end

    # @todo Store non-plain, non-HTML body parts.
    # params.keys.select do |key|
    #   key[/\Abody-(?!html|plain)/]
    # end

    [message]
  when 'raw'
    message = self.class.condense(Message::Mailgun.new(Mail.new(params['body-mime'])))
    [message]
  else
    raise ArgumentError, "Can't handle Mailgun #{@http_post_format} HTTP POST format"
  end
end

#valid?(params) ⇒ Boolean

Returns whether a request originates from Mailgun.

Parameters:

  • params (Hash)

    the content of Mailgun's webhook

Returns:

  • (Boolean)

    whether the request originates from Mailgun

Raises:

  • (IndexError)

    if the request is missing parameters

See Also:



26
27
28
29
30
31
32
# File 'lib/multi_mail/mailgun/receiver.rb', line 26

def valid?(params)
  if @mailgun_api_key
    params.fetch('signature') == signature(params)
  else
    super
  end
end