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

#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:

  • message (Mail::Message)

    a message

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<Mail::Message>

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

Parameters:

  • params (Hash)

    the content of Mailgun's webhook

Returns:

  • (Array<Mail::Message>)

    messages

See Also:



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 = Mail.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.
    extra = [
      'stripped-text',
      'stripped-signature',
      'stripped-html',
      'content-id-map',
    ]

    # Non-plain, non-HTML body parts.
    extra += params.keys.select do |key|
      key[/\Abody-(?!html|plain)/]
    end

    extra.each do |key|
      if params.key?(key) && !params[key].empty?
        message[key] = params[key]
      end
    end

    [message]
  when 'raw'
    message = self.class.condense(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