safety_mailer 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,30 +1,90 @@
1
1
  module SafetyMailer
2
2
  class Carrier
3
- attr_accessor :matchers, :settings
3
+ attr_accessor :matchers, :settings, :mail
4
+
4
5
  def initialize(params = {})
5
6
  self.matchers = params[:allowed_matchers] || []
6
7
  self.settings = params[:delivery_method_settings] || {}
7
8
  delivery_method = params[:delivery_method] || :smtp
8
9
  @delivery_method = Mail::Configuration.instance.lookup_delivery_method(delivery_method).new(settings)
10
+ @sendgrid_options = {}
9
11
  end
10
- def log(msg)
11
- Rails.logger.warn(msg) if defined?(Rails)
12
- end
12
+
13
13
  def deliver!(mail)
14
- mail.to = mail.to.reject do |recipient|
15
- if matchers.any?{ |m| recipient =~ m }
16
- false
17
- else
18
- log "*** safety_mailer suppressing mail to #{recipient}"
19
- true
20
- end
14
+ self.mail = mail
15
+ allowed = filter(recipients)
16
+
17
+ if allowed.empty?
18
+ log "*** safety_mailer - no allowed recipients ... suppressing delivery altogether"
19
+ return
20
+ end
21
+
22
+ mail['X-SMTPAPI'].value = prepare_sendgrid_delivery(allowed) if sendgrid?
23
+ mail.to = allowed
24
+
25
+ @delivery_method.deliver!(mail)
26
+ end
27
+
28
+ private
29
+
30
+ def recipients
31
+ sendgrid?
32
+ sendgrid_to = @sendgrid_options['to']
33
+ sendgrid_to.nil? || sendgrid_to.empty? ? mail.to : sendgrid_to
34
+ end
35
+
36
+ def sendgrid?
37
+ @sendgrid ||= !!if mail['X-SMTPAPI']
38
+ @sendgrid_options = JSON.parse(mail['X-SMTPAPI'].value)
21
39
  end
22
- if mail.to.nil? || mail.to.empty?
23
- log "*** safety_mailer - no recipients left ... suppressing delivery altogether"
24
- else
25
- log "*** safety_mailer allowing delivery to #{mail.to}"
26
- @delivery_method.deliver!(mail)
40
+ rescue JSON::ParserError
41
+ log "*** safety_mailer was unable to parse the X-SMTPAPI header"
42
+ end
43
+
44
+ def filter(addresses)
45
+ allowed, rejected = addresses.partition { |r| whitelisted?(r) }
46
+
47
+ rejected.each { |addr| log "*** safety_mailer delivery suppressed for #{addr}" }
48
+ allowed.each { |addr| log "*** safety_mailer delivery allowed for #{addr}" }
49
+
50
+ allowed
51
+ end
52
+
53
+ def whitelisted?(recipient)
54
+ matchers.any? { |m| recipient =~ m }
55
+ end
56
+
57
+ # Handles clean-up for additional SendGrid features that may be required
58
+ # by changes to the recipient list. Expects the passed-in Array of
59
+ # addresses to have been whitelist-filtered already.
60
+ def prepare_sendgrid_delivery(addresses)
61
+ amendments = { 'to' => addresses }
62
+
63
+ # The SendGrid Substitution Tags feature, if used, requires that an
64
+ # ordered Array of substitution values aligns with the Array of
65
+ # recipients in the "to" field of the API header. If substitution key is
66
+ # present, this filters the Arrays for each template to re-align with our
67
+ # whitelisted addresses.
68
+ #
69
+ # @see http://docs.sendgrid.com/documentation/api/smtp-api/developers-guide/substitution-tags/
70
+ if substitutions = @sendgrid_options['sub']
71
+ substitutions.each do |template, values|
72
+ values = recipients.zip(values).map do |addr, value|
73
+ value if addresses.include?(addr)
74
+ end
75
+
76
+ substitutions[template] = values.compact
77
+ end
78
+
79
+ amendments['sub'] = substitutions
27
80
  end
81
+
82
+ JSON.generate(@sendgrid_options.merge(amendments))
83
+ end
84
+
85
+ def log(msg)
86
+ Rails.logger.warn(msg) if defined?(Rails)
28
87
  end
88
+
29
89
  end
30
90
  end
@@ -1,3 +1,3 @@
1
1
  module SafetyMailer
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safety_mailer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bill Kirtley