bounce-email 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2009-02-15
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,11 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/bounce-email.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ test/test_bounce-email.rb
11
+ test/test_helper.rb
@@ -0,0 +1,35 @@
1
+
2
+ For more information on bounce-email, see http://bounce-email.rubyforge.org
3
+
4
+ You can easily use this plugin with e.g. Rails application.
5
+ Configure rails, so it send email using postfix and it adds VERP.
6
+ #- in environment.rb file add lines -#
7
+ config.action_mailer.delivery_method = :sendmail
8
+ config.action_mailer.sendmail_settings = {
9
+ :location => '/usr/sbin/sendmail',
10
+ :arguments => '-XV -f bounces-main@amerimail.lv -i -t'
11
+ }
12
+ #- end -#
13
+ Change amerimail.lv to email server that will handle bounce emails.
14
+
15
+ Follow this tutorial to handle bounce-emails: http://keakaj.com/wisdom/2007/08/08/verp-on-rails/
16
+
17
+ You can make Ruby file like this:
18
+ require "rubygems"
19
+ require "tmail"
20
+ require "bounce-email"
21
+
22
+ mail = TMail::Mail.parse(STDIN.read)
23
+ bounce = BounceEmail::Mail.new(mail)
24
+
25
+ # Do something with bounce info
26
+ # bounce.isbounce -> true/false
27
+ # bounce.code -> e.g. "5.1.1"
28
+ # bounce.reason -> e.g. "Something about the address specified in the message caused this DSN."
29
+ # bounce.type -> e.g. "Permanent Failure"
30
+
31
+ # Permanent Failure means that it is hard bounce
32
+ # Persistent Transient Failure means that it is soft bounce
33
+
34
+ # If reason is "Vacation auto-reply" then email could be vacation email (but it is rearly cought).
35
+
@@ -0,0 +1,62 @@
1
+ = bounce-email
2
+
3
+ http://bounce-email.rubyforge.org/
4
+
5
+ == DESCRIPTION:
6
+
7
+ This Ruby library is for determining the bounce type of an email message. It finds out if bounce is hard or soft or it is out of office mail.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Can't determine email address that bounced.
12
+
13
+ == SYNOPSIS:
14
+
15
+ Follow this tutorial to handle bounce-emails: http://keakaj.com/wisdom/2007/08/08/verp-on-rails/
16
+
17
+ You can make Ruby file like this:
18
+ require "rubygems"
19
+ require "tmail"
20
+ require "bounce-email"
21
+
22
+ mail = TMail::Mail.parse(STDIN.read)
23
+ bounce = BounceEmail::Mail.new(mail)
24
+
25
+ # Do something with bounce info
26
+ # bounce.isbounce -> true/false
27
+ # bounce.code -> e.g. "5.1.1"
28
+ # bounce.reason -> e.g. "Something about the address specified in the message caused this DSN."
29
+ # bounce.type -> e.g. "Permanent Failure"
30
+
31
+ == REQUIREMENTS:
32
+
33
+ Ruby Gem TMail is required
34
+
35
+ == INSTALL:
36
+
37
+ Nothing to install.
38
+
39
+ == LICENSE:
40
+
41
+ (The MIT License)
42
+
43
+ Copyright (c) 2009 Agris Ameriks
44
+
45
+ Permission is hereby granted, free of charge, to any person obtaining
46
+ a copy of this software and associated documentation files (the
47
+ 'Software'), to deal in the Software without restriction, including
48
+ without limitation the rights to use, copy, modify, merge, publish,
49
+ distribute, sublicense, and/or sell copies of the Software, and to
50
+ permit persons to whom the Software is furnished to do so, subject to
51
+ the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be
54
+ included in all copies or substantial portions of the Software.
55
+
56
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
57
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
58
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
59
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
60
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
61
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
62
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/bounce-email'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('bounce-email', BounceEmail::VERSION) do |p|
7
+ p.developer('Agris Ameriks', 'ameriks@gmail.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ p.extra_deps = [
12
+ ['tmail','>= 1.2.3']
13
+ ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
@@ -0,0 +1,162 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module BounceEmail
5
+ VERSION = '0.0.1'
6
+ # I used quite much from http://www.phpclasses.org/browse/package/2691.html
7
+ require 'tmail'
8
+ class Mail
9
+ attr_accessor :isbounce, :code, :reason, :type
10
+
11
+ def initialize(mail) # You have to pass TMail object
12
+ @isbounce = (check_if_bounce(mail) ? true : false)
13
+
14
+ if @isbounce
15
+ if mail.subject.match(/auto.*reply|vacation|vocation|(out|away).*office|on holiday/i)
16
+ @reason = "Vacation auto-reply"
17
+ elsif !mail.parts.empty?
18
+ @code = mail.parts[1].body.match(/Status: ([0-9.]{0,})/)
19
+ @code = @code[1] if !@code.nil?
20
+ end
21
+
22
+ # Try to GET status code from txt
23
+ @code = getStatusFromTxt(mail.body) if (@code.nil? || @code.blank?) && @reason.blank?
24
+ @reason = "unknown" if @code.blank?
25
+
26
+ if @reason.blank?
27
+ @type = getTypeFromStatusCode(@code[0].chr.to_i)
28
+ @reason = getReasonFromStatusCode(@code.gsub(/\./,'')[1..2])
29
+ end
30
+ end
31
+ rescue
32
+ @reason = "unknown"
33
+ end
34
+
35
+ private
36
+ def getStatusFromTxt(email)
37
+ #=begin
38
+ # This function is taken from PHP Bounce Handler class (http://www.phpclasses.org/browse/package/2691.html)
39
+ # Author: Chris Fortune
40
+ # Big thanks goes to him
41
+ # I transled them to Ruby and added some my parts
42
+ #=end
43
+ if email.match(/no such address|Recipient address rejected|User unknown in virtual alias table|The recipient was unavailable to take delivery of the message|Sorry, no mailbox here by that name/i)
44
+ return "5.1.1"
45
+ elsif email.match(/unrouteable mail domain|Esta casilla ha expirado por falta de uso/i)
46
+ return "5.1.2"
47
+ elsif email.match(/mailbox is full|Mailbox quota usage exceeded|User mailbox exceeds allowed size|Message rejected\. Not enough storage space/i) # AA added 4th or
48
+ if email.match(/This is a permanent error/i) # AA added this
49
+ return "5.2.2"
50
+ else
51
+ return "4.2.2"
52
+ end
53
+ elsif email.match(/not yet been delivered/i)
54
+ return "4.2.0"
55
+ elsif email.match(/mailbox unavailable/i)
56
+ return "5.2.0"
57
+ elsif email.match(/Unrouteable address/i)
58
+ return "5.4.4"
59
+ elsif email.match(/retry timeout exceeded/i)
60
+ return "4.4.7"
61
+ elsif email.match(/The account or domain may not exist, they may be blacklisted, or missing the proper dns entries./i)
62
+ return "5.2.0"
63
+ elsif email.match(/554 TRANSACTION FAILED/i)
64
+ return "5.5.4"
65
+ elsif email.match(/Status: 4\.4\.1|delivery temporarily suspended/i)
66
+ return "4.4.1"
67
+ elsif email.match(/550 OU\-002|Mail rejected by Windows Live Hotmail for policy reasons/i)
68
+ return "5.5.0"
69
+ elsif email.match(/PERM_FAILURE: DNS Error: Domain name not found/i)
70
+ return "5.1.2"
71
+ elsif email.match(/Delivery attempts will continue to be made for/i)
72
+ return "4.2.0"
73
+ elsif email.match(/554 delivery error:/i)
74
+ return "5.5.4"
75
+ elsif email.match(/550-5\.1\.1|This Gmail user does not exist\./i)
76
+ return "5.1.1"
77
+ elsif email.match(/5.7.1 Your message.*?was blocked by ROTA DNSBL/i) # AA added
78
+ return "5.7.1"
79
+ elsif email.match(/Technical details of permanent failure/i) && (email.match(/The recipient server did not accept our requests to connect/i) || email.match(/Connection was dropped by remote host/i) || email.match(/Could not initiate SMTP conversation/i)) # AA added
80
+ return "5.3.2"
81
+ elsif email.match(/Technical details of temporary failure/i) && (email.match(/The recipient server did not accept our requests to connect/i) || email.match(/Connection was dropped by remote host/i) || email.match(/Could not initiate SMTP conversation/i)) # AA added
82
+ return "4.3.2"
83
+ elsif email.match(/Delivery to the following recipient failed permanently/i) # AA added
84
+ return "5.0.0"
85
+ end
86
+ end
87
+
88
+ def getReasonFromStatusCode(code)
89
+ array = {}
90
+ array['00'] = "Other undefined status is the only undefined error code. It should be used for all errors for which only the class of the error is known."
91
+ array['10'] = "Something about the address specified in the message caused this DSN."
92
+ array['11'] = "The mailbox specified in the address does not exist. For Internet mail names, this means the address portion to the left of the '@' sign is invalid. This code is only useful for permanent failures."
93
+ array['12'] = "The destination system specified in the address does not exist or is incapable of accepting mail. For Internet mail names, this means the address portion to the right of the @ is invalid for mail. This codes is only useful for permanent failures."
94
+ array['13'] = "The destination address was syntactically invalid. This can apply to any field in the address. This code is only useful for permanent failures."
95
+ array['14'] = "The mailbox address as specified matches one or more recipients on the destination system. This may result if a heuristic address mapping algorithm is used to map the specified address to a local mailbox name."
96
+ array['15'] = "This mailbox address as specified was valid. This status code should be used for positive delivery reports."
97
+ array['16'] = "The mailbox address provided was at one time valid, but mail is no longer being accepted for that address. This code is only useful for permanent failures."
98
+ array['17'] = "The sender's address was syntactically invalid. This can apply to any field in the address."
99
+ array['18'] = "The sender's system specified in the address does not exist or is incapable of accepting return mail. For domain names, this means the address portion to the right of the @ is invalid for mail. "
100
+ array['20'] = "The mailbox exists, but something about the destination mailbox has caused the sending of this DSN."
101
+ array['21'] = "The mailbox exists, but is not accepting messages. This may be a permanent error if the mailbox will never be re-enabled or a transient error if the mailbox is only temporarily disabled."
102
+ array['22'] = "The mailbox is full because the user has exceeded a per-mailbox administrative quota or physical capacity. The general semantics implies that the recipient can delete messages to make more space available. This code should be used as a persistent transient failure."
103
+ array['23'] = "A per-mailbox administrative message length limit has been exceeded. This status code should be used when the per-mailbox message length limit is less than the general system limit. This code should be used as a permanent failure."
104
+ array['24'] = "The mailbox is a mailing list address and the mailing list was unable to be expanded. This code may represent a permanent failure or a persistent transient failure. "
105
+ array['30'] = "The destination system exists and normally accepts mail, but something about the system has caused the generation of this DSN."
106
+ array['31'] = "Mail system storage has been exceeded. The general semantics imply that the individual recipient may not be able to delete material to make room for additional messages. This is useful only as a persistent transient error."
107
+ array['32'] = "The host on which the mailbox is resident is not accepting messages. Examples of such conditions include an immanent shutdown, excessive load, or system maintenance. This is useful for both permanent and permanent transient errors. "
108
+ array['33'] = "Selected features specified for the message are not supported by the destination system. This can occur in gateways when features from one domain cannot be mapped onto the supported feature in another."
109
+ array['34'] = "The message is larger than per-message size limit. This limit may either be for physical or administrative reasons. This is useful only as a permanent error."
110
+ array['35'] = "The system is not configured in a manner which will permit it to accept this message."
111
+ array['40'] = "Something went wrong with the networking, but it is not clear what the problem is, or the problem cannot be well expressed with any of the other provided detail codes."
112
+ array['41'] = "The outbound connection attempt was not answered, either because the remote system was busy, or otherwise unable to take a call. This is useful only as a persistent transient error."
113
+ array['42'] = "The outbound connection was established, but was otherwise unable to complete the message transaction, either because of time-out, or inadequate connection quality. This is useful only as a persistent transient error."
114
+ array['43'] = "The network system was unable to forward the message, because a directory server was unavailable. This is useful only as a persistent transient error. The inability to connect to an Internet DNS server is one example of the directory server failure error. "
115
+ array['44'] = "The mail system was unable to determine the next hop for the message because the necessary routing information was unavailable from the directory server. This is useful for both permanent and persistent transient errors. A DNS lookup returning only an SOA (Start of Administration) record for a domain name is one example of the unable to route error."
116
+ array['45'] = "The mail system was unable to deliver the message because the mail system was congested. This is useful only as a persistent transient error."
117
+ array['46'] = "A routing loop caused the message to be forwarded too many times, either because of incorrect routing tables or a user forwarding loop. This is useful only as a persistent transient error."
118
+ array['47'] = "The message was considered too old by the rejecting system, either because it remained on that host too long or because the time-to-live value specified by the sender of the message was exceeded. If possible, the code for the actual problem found when delivery was attempted should be returned rather than this code. This is useful only as a persistent transient error."
119
+ array['50'] = "Something was wrong with the protocol necessary to deliver the message to the next hop and the problem cannot be well expressed with any of the other provided detail codes."
120
+ array['51'] = "A mail transaction protocol command was issued which was either out of sequence or unsupported. This is useful only as a permanent error."
121
+ array['52'] = "A mail transaction protocol command was issued which could not be interpreted, either because the syntax was wrong or the command is unrecognized. This is useful only as a permanent error."
122
+ array['53'] = "More recipients were specified for the message than could have been delivered by the protocol. This error should normally result in the segmentation of the message into two, the remainder of the recipients to be delivered on a subsequent delivery attempt. It is included in this list in the event that such segmentation is not possible."
123
+ array['54'] = "A valid mail transaction protocol command was issued with invalid arguments, either because the arguments were out of range or represented unrecognized features. This is useful only as a permanent error. "
124
+ array['55'] = "A protocol version mis-match existed which could not be automatically resolved by the communicating parties."
125
+ array['60'] = "Something about the content of a message caused it to be considered undeliverable and the problem cannot be well expressed with any of the other provided detail codes. "
126
+ array['61'] = "The media of the message is not supported by either the delivery protocol or the next system in the forwarding path. This is useful only as a permanent error."
127
+ array['62'] = "The content of the message must be converted before it can be delivered and such conversion is not permitted. Such prohibitions may be the expression of the sender in the message itself or the policy of the sending host."
128
+ array['63'] = "The message content must be converted to be forwarded but such conversion is not possible or is not practical by a host in the forwarding path. This condition may result when an ESMTP gateway supports 8bit transport but is not able to downgrade the message to 7 bit as required for the next hop."
129
+ array['64'] = "This is a warning sent to the sender when message delivery was successfully but when the delivery required a conversion in which some data was lost. This may also be a permanant error if the sender has indicated that conversion with loss is prohibited for the message."
130
+ array['65'] = "A conversion was required but was unsuccessful. This may be useful as a permanent or persistent temporary notification."
131
+ array['70'] = "Something related to security caused the message to be returned, and the problem cannot be well expressed with any of the other provided detail codes. This status code may also be used when the condition cannot be further described because of security policies in force."
132
+ array['71'] = "The sender is not authorized to send to the destination. This can be the result of per-host or per-recipient filtering. This memo does not discuss the merits of any such filtering, but provides a mechanism to report such. This is useful only as a permanent error."
133
+ array['72'] = "The sender is not authorized to send a message to the intended mailing list. This is useful only as a permanent error."
134
+ array['73'] = "A conversion from one secure messaging protocol to another was required for delivery and such conversion was not possible. This is useful only as a permanent error. "
135
+ array['74'] = "A message contained security features such as secure authentication which could not be supported on the delivery protocol. This is useful only as a permanent error."
136
+ array['75'] = "A transport system otherwise authorized to validate or decrypt a message in transport was unable to do so because necessary information such as key was not available or such information was invalid."
137
+ array['76'] = "A transport system otherwise authorized to validate or decrypt a message was unable to do so because the necessary algorithm was not supported. "
138
+ array['77'] = "A transport system otherwise authorized to validate a message was unable to do so because the message was corrupted or altered. This may be useful as a permanent, transient persistent, or successful delivery code."
139
+ res = array[code]
140
+ array = nil
141
+ return res
142
+ end
143
+
144
+ def getTypeFromStatusCode(code)
145
+ case code
146
+ when 5
147
+ return "Permanent Failure"
148
+ when 4
149
+ return "Persistent Transient Failure"
150
+ when 2
151
+ return "Success"
152
+ end
153
+ end
154
+
155
+ def check_if_bounce(mail)
156
+ return true if mail.subject.match(/((returned|undelivered) mail)|(mail delivery)( failed)?|(delivery )(status notification|failure)|(failure notice)|(undeliver(able|ed)( mail)?)|(return(ing message|ed) to sender)|auto.*reply|vacation|vocation|(out|away).*office|on holiday/i)
157
+ return true if mail['precedence'].to_s.match(/(auto_reply|autoreply)/i)
158
+ return true if mail.from.to_s.match(/^(MAILER-DAEMON|POSTMASTER)\@/i)
159
+ return false
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestBounce-email < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/bounce-email'
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bounce-email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Agris Ameriks
8
+ autorequire: bounce-email
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-16 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: This Ruby library is for determining the bounce type of an email message. It finds out if bounce is hard or soft or it is out of office mail.
17
+ email: ameriks@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - History.txt
24
+ - Manifest.txt
25
+ - PostInstall.txt
26
+ - Rakefile
27
+ - README.rdoc
28
+ files:
29
+ - lib/bounce-email.rb
30
+ - test/test_bounce-email.rb
31
+ - test/test_helper.rb
32
+ - History.txt
33
+ - Manifest.txt
34
+ - PostInstall.txt
35
+ - Rakefile
36
+ - README.rdoc
37
+ has_rdoc: true
38
+ homepage: bounce-email.rubyforge.org
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --title
42
+ - bounce-email -- Easy Bounce Handling
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project: bounce-email
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Ruby Bounce Email Handler
64
+ test_files: []
65
+