Class: MultiMail::Receiver::Mandrill

Inherits:
Object
  • Object
show all
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

Methods included from Base

included, #process

Constructor Details

#initialize(options = {}) ⇒ Mandrill

Initializes a Mandrill incoming email receiver.

Parameters:

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

    required and optional arguments

  • option (Hash)

    a customizable set of options



18
19
20
21
22
23
# File 'lib/multi_mail/mandrill/receiver.rb', line 18

def initialize(options = {})
  super
  @spamassassin_threshold = options[:spamassassin_threshold] || 5
  @mandrill_webhook_key = options[:mandrill_webhook_key]
  @mandrill_webhook_url = options[:mandrill_webhook_url]
end

Instance Method Details

#spam?(message) ⇒ Boolean

Returns whether a message is spam.

Parameters:

Returns:

  • (Boolean)

    whether the message is spam



116
117
118
# File 'lib/multi_mail/mandrill/receiver.rb', line 116

def spam?(message)
  message.spam_report_score > @spamassassin_threshold
end

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

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

Parameters:

  • params (Hash)

    the content of Mandrill's webhook

Returns:

See Also:



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 = 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 |_,attachment|
          add_file(:filename => attachment['name'], :content => attachment['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`.
    message.ts                = event['ts']
    message.email             = msg['email']
    message.dkim_signed       = msg['dkim']['signed']
    message.dkim_valid        = msg['dkim']['valid']
    message.spam_report_score = msg['spam_report']['score']
    message.spf_result        = msg['spf']['result']

    message
  end
end

#valid?(params) ⇒ Boolean

Returns whether a request originates from Mandrill.

Parameters:

  • params (Hash)

    the content of Mandrill's webhook

Returns:

  • (Boolean)

    whether the request originates from Mailgun

Raises:

  • (IndexError)

    if the request is missing parameters

See Also:



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