Class: MultiMail::Receiver::Mailgun
- Inherits:
-
Object
- Object
- MultiMail::Receiver::Mailgun
- Includes:
- Base
- Defined in:
- lib/multi_mail/mailgun/receiver.rb
Overview
Mailgun's incoming email receiver.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Mailgun
constructor
Initializes a Mailgun incoming email receiver.
-
#spam?(message) ⇒ Boolean
Returns whether a message is spam.
-
#transform(params) ⇒ Array<MultiMail::Message::Mailgun>
Transforms the content of Mailgun's webhook into a list of messages.
-
#valid?(params) ⇒ Boolean
Returns whether a request originates from Mailgun.
Methods included from Base
Constructor Details
#initialize(options = {}) ⇒ Mailgun
Initializes a Mailgun incoming email receiver.
14 15 16 17 18 |
# File 'lib/multi_mail/mailgun/receiver.rb', line 14 def initialize( = {}) super @mailgun_api_key = [:mailgun_api_key] @http_post_format = [: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.
117 118 119 |
# File 'lib/multi_mail/mailgun/receiver.rb', line 117 def spam?() ['X-Mailgun-Sflag'] && ['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::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| = params["attachment-#{n}"] add_file(this.class.add_file_arguments()) end end end # Extra Mailgun parameters. if params.key?('stripped-text') && !params['stripped-text'].empty? .stripped_text = params['stripped-text'] end if params.key?('stripped-signature') && !params['stripped-signature'].empty? .stripped_signature = params['stripped-signature'] end if params.key?('stripped-html') && !params['stripped-html'].empty? .stripped_html = params['stripped-html'] end if params.key?('content-id-map') && !params['content-id-map'].empty? .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 [] when 'raw' = self.class.condense(Message::Mailgun.new(Mail.new(params['body-mime']))) [] 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.
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 |