Class: MultiMail::Receiver::SendGrid

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

Overview

SendGrid's incoming email receiver.

Instance Method Summary collapse

Methods included from Base

included, #process, #valid?

Constructor Details

#initialize(options = {}) ⇒ SendGrid

Initializes a SendGrid incoming email receiver.

Parameters:

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

    required and optional arguments

  • option (Hash)

    a customizable set of options



11
12
13
14
# File 'lib/multi_mail/sendgrid/receiver.rb', line 11

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

Instance Method Details

#encode(key) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/multi_mail/sendgrid/receiver.rb', line 79

def encode(key)
  if @charsets.key?(key)
    if @params[key].respond_to?(:force_encoding)
      @params[key].force_encoding(@charsets[key]).encode('UTF-8')
    else
      Iconv.conv('UTF-8', @charsets[key], @params[key])
    end
  else
    @params[key]
  end
end

#spam?(message) ⇒ Boolean

Returns whether a message is spam.

Parameters:

Returns:

  • (Boolean)

    whether the message is spam



75
76
77
# File 'lib/multi_mail/sendgrid/receiver.rb', line 75

def spam?(message)
  message.spam_score.to_f > @spamassassin_threshold
end

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

Transforms the content of SendGrid's webook into a list of messages.

Parameters:

  • params (Hash)

    the content of Mandrill's webhook

Returns:

See Also:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/multi_mail/sendgrid/receiver.rb', line 21

def transform(params)
  # Make variables available to the `encode` method.
  @params = params
  @charsets = JSON.load(params['charsets'])

  # Mail changes `self`.
  this = self

  message = Message::SendGrid.new do
    # SendGrid includes a `charsets` parameter, which describes the
    # encodings of the `from`, `to`, `cc` and `subject` parameters, which
    # we don't need because we parse the headers directly.
    # @see http://sendgrid.com/docs/API_Reference/Webhooks/parse.html#-Character-Sets-and-Header-Decoding
    header params['headers']

    # The following are redundant with `headers`:
    #
    # from    params['from']
    # to      params['to']
    # cc      params['cc']
    # subject params['subject']

    text_part do
      body this.encode('text')
    end

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

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

  # Extra SendGrid parameters.
  message.dkim = params['dkim']
  message.spf = params['SPF']
  message.spam_report = params['spam_report']
  message.spam_score = params['spam_score']

  # Discard `envelope`, which contains `to` and `from`, and the
  # undocumented `attachment-info`.
  [message]
end