Class: MultiMail::Receiver::Mandrill
- Inherits:
-
Object
- Object
- MultiMail::Receiver::Mandrill
- Includes:
- Base
- Defined in:
- lib/multi_mail/mandrill/receiver.rb
Overview
Mandrill's incoming email receiver.
Mandrill uses an HTTP header to ensure a request originates from Mandrill.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Mandrill
constructor
Initializes a Mandrill incoming email receiver.
-
#spam?(message) ⇒ Boolean
Returns whether a message is spam.
-
#transform(params) ⇒ Array<MultiMail::Message::Mandrill>
Transforms the content of Mandrill's webhook into a list of messages.
-
#valid?(params) ⇒ Boolean
Returns whether a request originates from Mandrill.
Methods included from Base
Constructor Details
#initialize(options = {}) ⇒ Mandrill
Initializes a Mandrill incoming email receiver.
18 19 20 21 22 23 |
# File 'lib/multi_mail/mandrill/receiver.rb', line 18 def initialize( = {}) super @spamassassin_threshold = [:spamassassin_threshold] || 5 @mandrill_webhook_key = [:mandrill_webhook_key] @mandrill_webhook_url = [:mandrill_webhook_url] end |
Instance Method Details
#spam?(message) ⇒ Boolean
Returns whether a message is spam.
116 117 118 |
# File 'lib/multi_mail/mandrill/receiver.rb', line 116 def spam?() .spam_report_score > @spamassassin_threshold end |
#transform(params) ⇒ Array<MultiMail::Message::Mandrill>
Transforms the content of Mandrill's webhook into a list of messages.
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 107 108 109 110 |
# File 'lib/multi_mail/mandrill/receiver.rb', line 44 def transform(params) # JSON is necessarily UTF-8. JSON.load(params['mandrill_events']).select do |event| event.fetch('event') == 'inbound' end.map do |event| msg = event['msg'] # Mail changes `self`. headers = self.class.multimap(msg['headers']) this = self = Message::Mandrill.new do headers headers # The following are redundant with `message-headers`: # # address = Mail::Address.new(msg['from_email']) # address.display_name = msg['from_name'] # # from address.format # to msg['to'].flatten.compact # subject msg['subject'] text_part do body msg['text'] end # If an email contains multiple HTML parts, Mandrill will only # include the first HTML part in its `html` parameter. We therefore # parse its `raw_msg` parameter to set the HTML part correctly. html = this.class.condense(Message::Mandrill.new(Mail.new(msg['raw_msg']))).parts.find do |part| part.content_type == 'text/html; charset=UTF-8' end if html html_part do content_type 'text/html; charset=UTF-8' body html.body.decoded end elsif msg.key?('html') html_part do content_type 'text/html; charset=UTF-8' body msg['html'] end end if msg.key?('attachments') msg['attachments'].each do |_,| add_file(:filename => ['name'], :content => ['content']) end end end # Extra Mandrill parameters. Discard `sender` and `tags`, which are # null according to the docs, `matched_rules` within `spam_report`, # and `detail` within `spf`, which is just a human-readable version of # `result`. .ts = event['ts'] .email = msg['email'] .dkim_signed = msg['dkim']['signed'] .dkim_valid = msg['dkim']['valid'] .spam_report_score = msg['spam_report']['score'] .spf_result = msg['spf']['result'] end end |
#valid?(params) ⇒ Boolean
Returns whether a request originates from Mandrill.
31 32 33 34 35 36 37 |
# File 'lib/multi_mail/mandrill/receiver.rb', line 31 def valid?(params) if @mandrill_webhook_url && @mandrill_webhook_key params.fetch('env').fetch('HTTP_X_MANDRILL_SIGNATURE') == signature(params) else super end end |