ses-proxy 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,19 @@ require 'eventmachine'
2
2
  require 'mail'
3
3
  require 'aws-sdk'
4
4
 
5
+ if SesProxy::Conf.get[:aws][:ses] and SesProxy::Conf.get[:aws][:ses][:username] and SesProxy::Conf.get[:aws][:ses][:password]
6
+ Mail.defaults do
7
+ delivery_method :smtp, {
8
+ :address => 'email-smtp.us-east-1.amazonaws.com',
9
+ :port => '587',
10
+ :user_name => SesProxy::Conf.get[:aws][:ses][:username],
11
+ :password => SesProxy::Conf.get[:aws][:ses][:password],
12
+ :authentication => :plain,
13
+ :enable_starttls_auto => true
14
+ }
15
+ end
16
+ end
17
+
5
18
  module SesProxy
6
19
  class SmtpServer < EM::P::SmtpServer
7
20
 
@@ -64,12 +77,31 @@ module SesProxy
64
77
  def receive_message
65
78
  return false unless verified
66
79
  mail = Mail.read_from_string(message)
67
- bounced = Bounce.where({:email=>{"$in"=>recipients|mail.cc_addrs|mail.bcc_addrs}}).map(&:email)
80
+ bounced = Bounce.where({:email=>{"$in"=>recipients}}).map(&:email)
68
81
  #TODO: Define policy for retry when bounce is not permanent
69
- actual_recipients = recipients - bounced
82
+
83
+ #Remove bounced addresses
84
+ actual_recipients = mail.to_addrs - bounced
70
85
  actual_cc_addrs = mail.cc_addrs - bounced
71
- actual_bcc_addrs = mail.bcc_addrs - bounced
72
- original_number = recipients.size+mail.cc_addrs.size+mail.bcc_addrs.size
86
+ actual_bcc_addrs = recipients - (mail.to_addrs + mail.cc_addrs) - bounced
87
+
88
+ #Remove blacklisted domains
89
+ if SesProxy::Conf.get[:blacklisted_domains] and SesProxy::Conf.get[:blacklisted_domains].any?
90
+ bld = SesProxy::Conf.get[:blacklisted_domains]
91
+ actual_recipients.collect!{|address| address unless bld.include?(address.split('@').last)}.compact!
92
+ actual_cc_addrs.collect!{|address| address unless bld.include?(address.split('@').last)}.compact!
93
+ actual_bcc_addrs.collect!{|address| address unless bld.include?(address.split('@').last)}.compact!
94
+ end
95
+
96
+ #Remove blacklisted regexp
97
+ if SesProxy::Conf.get[:blacklisted_regexp] and SesProxy::Conf.get[:blacklisted_regexp].any?
98
+ blr = SesProxy::Conf.get[:blacklisted_regexp]
99
+ actual_recipients.collect!{|address| address unless blr.map{|regexp| Regexp.new(regexp).match(address)}.compact.any? }.compact!
100
+ actual_cc_addrs.collect!{|address| address unless blr.map{|regexp| Regexp.new(regexp).match(address)}.compact.any? }.compact!
101
+ actual_bcc_addrs.collect!{|address| address unless blr.map{|regexp| Regexp.new(regexp).match(address)}.compact.any? }.compact!
102
+ end
103
+
104
+ original_number = recipients.size
73
105
  filtered_number = actual_recipients.size+actual_cc_addrs.size+actual_bcc_addrs.size
74
106
  record = RecipientsNumber.new({
75
107
  :original=>original_number,
@@ -82,40 +114,48 @@ module SesProxy
82
114
  mail.to = actual_recipients.uniq.join(",")
83
115
  mail.cc = actual_cc_addrs.uniq.join(",")
84
116
  mail.bcc = actual_bcc_addrs.uniq.join(",")
85
- record = Email.new({
86
- :sender => sender,
87
- :recipients => actual_recipients.uniq.join(","),
88
- :subject => mail.subject,
89
- :body => mail.body.decoded,
90
- :system => mail['X-Sender-System']||"Unknown",
91
- :created_at => Time.now,
92
- :updated_at => Time.now
93
- })
94
- record.save!
117
+ unless SesProxy::Conf.get[:collect_sent_mails].eql? false
118
+ record = Email.new({
119
+ :sender => sender,
120
+ :recipients => actual_recipients.uniq.join(","),
121
+ :subject => mail.subject,
122
+ :body => mail.body.decoded,
123
+ :system => mail['X-Sender-System']||"Unknown",
124
+ :created_at => Time.now,
125
+ :updated_at => Time.now
126
+ })
127
+ record.save!
128
+ end
95
129
  begin
96
- ses.send_raw_email(mail.to_s)
130
+ if SesProxy::Conf.get[:aws][:ses] and SesProxy::Conf.get[:aws][:ses][:username] and SesProxy::Conf.get[:aws][:ses][:password]
131
+ mail.deliver!
132
+ else
133
+ ses.send_raw_email(mail.to_s)
134
+ end
97
135
  rescue Exception => e
98
136
  print "Error! "
99
137
  puts e.message
100
138
  return false
101
139
  end
102
140
  else
103
- puts "No valid recipients!"
141
+ puts "No valid recipients! #{mail.to_addrs}"
104
142
  end
105
143
  if not original_number.eql? filtered_number
106
- mail.to = (recipients&bounced).uniq.join(",")
107
- mail.cc = (mail.cc_addrs&bounced).uniq.join(",")
108
- mail.bcc = (mail.bcc_addrs&bounced).uniq.join(",")
109
- record = BouncedEmail.new({
110
- :sender => sender,
111
- :recipients => (recipients&bounced).uniq.join(","),
112
- :subject => mail.subject,
113
- :body => mail.body.decoded,
114
- :system => mail['X-Sender-System']||"Unknown",
115
- :created_at => Time.now,
116
- :updated_at => Time.now
117
- })
118
- record.save!
144
+ unless SesProxy::Conf.get[:collect_bounced_mails].eql? false
145
+ mail.to = (recipients&bounced).uniq.join(",")
146
+ mail.cc = (mail.cc_addrs&bounced).uniq.join(",")
147
+ mail.bcc = (mail.bcc_addrs&bounced).uniq.join(",")
148
+ record = BouncedEmail.new({
149
+ :sender => sender,
150
+ :recipients => (recipients&bounced).uniq.join(","),
151
+ :subject => mail.subject,
152
+ :body => mail.body.decoded,
153
+ :system => mail['X-Sender-System']||"Unknown",
154
+ :created_at => Time.now,
155
+ :updated_at => Time.now
156
+ })
157
+ record.save!
158
+ end
119
159
  end
120
160
  end
121
161
 
@@ -141,4 +181,4 @@ module SesProxy
141
181
  !!@server
142
182
  end
143
183
  end
144
- end
184
+ end
@@ -1,8 +1,19 @@
1
+ :collect_sent_mails: true
2
+ :collect_bounced_mails: true
3
+ :blacklisted_domains:
4
+ - domain1.ltd
5
+ - domain2.ltd
6
+ :blacklisted_regexp:
7
+ - regexp1_pattern_without_delimiters
8
+ - regexp2_pattern_without_delimiters
1
9
  :aws:
2
10
  :access_key_id: your_access_key_id
3
11
  :secret_access_key: your_secret_access_key
4
12
  :account_id: 000000000000
5
13
  :allowed_topic_arns: ["arn:aws:sns:us-east-1:123456789012:MyTopic"]
14
+ :ses:
15
+ :username: your_ses_smtp_username
16
+ :password: your_ses_smtp_password
6
17
  :smtp_auth:
7
18
  :user: smtp_user
8
19
  :password: smtp_pass
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ses-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -289,8 +289,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
289
289
  version: '0'
290
290
  requirements: []
291
291
  rubyforge_project:
292
- rubygems_version: 1.8.24
292
+ rubygems_version: 1.8.25
293
293
  signing_key:
294
294
  specification_version: 3
295
295
  summary: SMTP Proxy for Amazon Simple Email Service with bounce and complaints support
296
296
  test_files: []
297
+ has_rdoc: false