bounce_email 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/bounce_email.rb +77 -72
- data/test/bounce_email_test.rb +16 -3
- data/test/fixtures/no_subject.txt +21 -0
- metadata +6 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/lib/bounce_email.rb
CHANGED
@@ -30,21 +30,22 @@ module BounceEmail
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
|
34
33
|
def bounced?
|
35
34
|
@bounced ||= check_if_bounce(@mail) || (diagnostic_code != "unknown") || (error_status != "unknown")
|
36
35
|
end
|
37
|
-
alias_method :is_bounce?, :bounced? #to stay backwards compatible
|
38
36
|
|
39
37
|
def diagnostic_code
|
40
38
|
@diagnostic_code ||= get_reason_from_status_code(code)
|
41
39
|
end
|
42
|
-
alias_method :reason, :diagnostic_code #to stay backwards compatible
|
43
40
|
|
44
41
|
def error_status
|
45
42
|
@error_status ||= get_code(@mail)
|
46
43
|
end
|
47
|
-
|
44
|
+
|
45
|
+
# backward compatibility
|
46
|
+
alias_method :is_bounce?, :bounced?
|
47
|
+
alias_method :reason, :diagnostic_code
|
48
|
+
alias_method :code, :error_status
|
48
49
|
|
49
50
|
=begin #Streamline with Mail Gem methods - IMPLEMENT ME!
|
50
51
|
def final_recipien?
|
@@ -65,16 +66,17 @@ module BounceEmail
|
|
65
66
|
@original_mail ||= get_original_mail(@mail)
|
66
67
|
end
|
67
68
|
|
68
|
-
def method_missing(m, *args)
|
69
|
-
@mail.send(m, *args)
|
69
|
+
def method_missing(m, *args, &block)
|
70
|
+
@mail.send(m, *args, &block)
|
70
71
|
end
|
71
72
|
|
72
73
|
private
|
74
|
+
|
73
75
|
def get_code(mail)
|
74
|
-
unicode_subject = mail.subject
|
76
|
+
unicode_subject = mail.subject.to_s
|
75
77
|
unicode_subject = unicode_subject.encode('utf-8') if unicode_subject.respond_to?(:encode)
|
76
78
|
|
77
|
-
return '97' if
|
79
|
+
return '97' if unicode_subject.match(/delayed/i)
|
78
80
|
return '98' if unicode_subject.match(/(unzulässiger|unerlaubter) anhang/i)
|
79
81
|
return '99' if unicode_subject.match(/auto.*reply|vacation|vocation|(out|away).*office|on holiday|abwesenheits|autorespond|Automatische|eingangsbestätigung/i)
|
80
82
|
|
@@ -129,78 +131,82 @@ module BounceEmail
|
|
129
131
|
|
130
132
|
def get_reason_from_status_code(code)
|
131
133
|
return 'unknown' if code.nil? or code == 'unknown'
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
134
|
+
reasons = {
|
135
|
+
'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.",
|
136
|
+
'10' => "Something about the address specified in the message caused this DSN.",
|
137
|
+
'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.",
|
138
|
+
'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.",
|
139
|
+
'13' => "The destination address was syntactically invalid. This can apply to any field in the address. This code is only useful for permanent failures.",
|
140
|
+
'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.",
|
141
|
+
'15' => "This mailbox address as specified was valid. This status code should be used for positive delivery reports.",
|
142
|
+
'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.",
|
143
|
+
'17' => "The sender's address was syntactically invalid. This can apply to any field in the address.",
|
144
|
+
'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. ",
|
145
|
+
'20' => "The mailbox exists, but something about the destination mailbox has caused the sending of this DSN.",
|
146
|
+
'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.",
|
147
|
+
'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.",
|
148
|
+
'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.",
|
149
|
+
'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. ",
|
150
|
+
'30' => "The destination system exists and normally accepts mail, but something about the system has caused the generation of this DSN.",
|
151
|
+
'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.",
|
152
|
+
'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. ",
|
153
|
+
'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.",
|
154
|
+
'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.",
|
155
|
+
'35' => "The system is not configured in a manner which will permit it to accept this message.",
|
156
|
+
'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.",
|
157
|
+
'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.",
|
158
|
+
'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.",
|
159
|
+
'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. ",
|
160
|
+
'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.",
|
161
|
+
'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.",
|
162
|
+
'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.",
|
163
|
+
'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.",
|
164
|
+
'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.",
|
165
|
+
'51' => "A mail transaction protocol command was issued which was either out of sequence or unsupported. This is useful only as a permanent error.",
|
166
|
+
'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.",
|
167
|
+
'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.",
|
168
|
+
'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. ",
|
169
|
+
'55' => "A protocol version mis-match existed which could not be automatically resolved by the communicating parties.",
|
170
|
+
'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. ",
|
171
|
+
'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.",
|
172
|
+
'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.",
|
173
|
+
'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.",
|
174
|
+
'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.",
|
175
|
+
'65' => "A conversion was required but was unsuccessful. This may be useful as a permanent or persistent temporary notification.",
|
176
|
+
'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.",
|
177
|
+
'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.",
|
178
|
+
'72' => "The sender is not authorized to send a message to the intended mailing list. This is useful only as a permanent error.",
|
179
|
+
'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. ",
|
180
|
+
'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.",
|
181
|
+
'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.",
|
182
|
+
'76' => "A transport system otherwise authorized to validate or decrypt a message was unable to do so because the necessary algorithm was not supported. ",
|
183
|
+
'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.",
|
184
|
+
#custom codes,
|
185
|
+
'97' => "Delayed",
|
186
|
+
'98' => "Not allowed Attachment",
|
187
|
+
'99' => "Vacation auto-reply",
|
188
|
+
}
|
189
|
+
|
186
190
|
code = code.gsub(/\./,'')[1..2]
|
187
|
-
|
191
|
+
reasons[code] || 'unknown'
|
188
192
|
end
|
189
193
|
|
190
194
|
def get_type_from_status_code(code)
|
191
195
|
return TYPE_HARD_FAIL if code.nil? or code == 'unknown'
|
196
|
+
|
192
197
|
pre_code = code[0].chr.to_i
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
198
|
+
types = {
|
199
|
+
5 => TYPE_HARD_FAIL,
|
200
|
+
4 => TYPE_SOFT_FAIL,
|
201
|
+
2 => TYPE_SUCCESS,
|
202
|
+
}
|
203
|
+
|
204
|
+
types[pre_code]
|
199
205
|
end
|
200
206
|
|
201
207
|
def check_if_bounce(mail)
|
202
|
-
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/i)
|
203
|
-
return true if mail.subject.match(/auto.*reply|vacation|vocation|(out|away).*office|on holiday|abwesenheits|autorespond|Automatische|eingangsbestätigung/i)
|
208
|
+
return true if mail.subject.to_s.match(/(returned|undelivered) mail|mail delivery( failed)?|(delivery )(status notification|failure)|failure notice|undeliver(able|ed)( mail)?|return(ing message|ed) to sender/i)
|
209
|
+
return true if mail.subject.to_s.match(/auto.*reply|vacation|vocation|(out|away).*office|on holiday|abwesenheits|autorespond|Automatische|eingangsbestätigung/i)
|
204
210
|
return true if mail['precedence'].to_s.match(/auto.*(reply|responder|antwort)/i)
|
205
211
|
return true if mail.from.to_s.match(/^(MAILER-DAEMON|POSTMASTER)\@/i)
|
206
212
|
false
|
@@ -216,6 +222,5 @@ module BounceEmail
|
|
216
222
|
rescue => e
|
217
223
|
nil
|
218
224
|
end
|
219
|
-
|
220
225
|
end
|
221
226
|
end
|
data/test/bounce_email_test.rb
CHANGED
@@ -88,13 +88,17 @@ class BounceEmailTest < Test::Unit::TestCase
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
+
def test_does_not_fail_if_subject_is_nil
|
92
|
+
bounce = BounceEmail::Mail.new load_email('no_subject')
|
93
|
+
assert !bounce.bounced?
|
94
|
+
end
|
91
95
|
|
92
96
|
def test_mail_methods_fallback
|
93
97
|
bounce = test_bounce('tt_bounce_10')
|
94
98
|
assert bounce.body
|
95
99
|
assert bounce.date
|
96
100
|
end
|
97
|
-
|
101
|
+
|
98
102
|
#Test mutlipart message from exchange
|
99
103
|
def test_multipart
|
100
104
|
bounce = test_bounce('tt_bounce_24')
|
@@ -102,12 +106,21 @@ class BounceEmailTest < Test::Unit::TestCase
|
|
102
106
|
assert_equal BounceEmail::TYPE_HARD_FAIL, bounce.type
|
103
107
|
assert_not_nil bounce.original_mail
|
104
108
|
end
|
105
|
-
|
109
|
+
|
106
110
|
#Test regexp in when parsing the original email
|
107
111
|
def test_multipart
|
108
112
|
bounce = test_bounce('tt_bounce_25')
|
109
113
|
assert bounce.bounced?
|
110
114
|
assert_not_nil bounce.original_mail
|
111
115
|
end
|
112
|
-
|
116
|
+
|
117
|
+
private
|
118
|
+
|
119
|
+
def load_email(name, prefix = 'fixtures')
|
120
|
+
default_extention = '.txt'
|
121
|
+
name << default_extention unless name.end_with?(default_extention)
|
122
|
+
|
123
|
+
path = File.join(File.dirname(__FILE__), prefix, name)
|
124
|
+
Mail.read(path)
|
125
|
+
end
|
113
126
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
From bounces-main+bratchka=amerimail.lv@amerimail.lv Tue Feb 10 06:54:24 2009
|
2
|
+
Return-Path: <bounces-main+bratchka=amerimail.lv@amerimail.lv>
|
3
|
+
X-Original-To: bratchka@amerimail.lv
|
4
|
+
Delivered-To: bounce_and_mail_handler@ameriks
|
5
|
+
Received: from Albanis-3.local (unknown [87.99.72.11])
|
6
|
+
by ameriks (Postfix) with ESMTP id 3499D3A08C
|
7
|
+
for <bratchka@amerimail.lv>; Tue, 10 Feb 2009 06:54:24 +0200 (EET)
|
8
|
+
Received: by Albanis-3.local (Postfix, from userid 501)
|
9
|
+
id D9FB832925D; Tue, 10 Feb 2009 06:54:22 +0200 (EET)
|
10
|
+
Date: Tue, 10 Feb 2009 06:54:14 +0200
|
11
|
+
From: ameriks@amerimail.lv
|
12
|
+
Reply-To: ginta.amerika@ameri.lv
|
13
|
+
To: bratchka@amerimail.lv
|
14
|
+
Mime-Version: 1.0
|
15
|
+
Content-Type: text/html; charset=utf-8
|
16
|
+
Message-Id: <20090210045423.D9FB832925D@Albanis-3.local>
|
17
|
+
|
18
|
+
Labdien, mes loti atvainojamies, ja sanemat so epastu. Mes paslaik veicam jaunas sistemas izstradi, kur ir arii ieklauta epastu sutisana. Ludzu ignorejiet so e-pastu!
|
19
|
+
Paldies par sapratni.
|
20
|
+
|
21
|
+
Ja jus velaties ar mums sazinaties si epasta sakaraa, sutiet epastu uz agris.ameriks@ameri.lv.
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bounce_email
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tobias Bielohlawek
|
@@ -18,7 +18,7 @@ autorequire:
|
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date: 2011-10-
|
21
|
+
date: 2011-10-28 00:00:00 +03:00
|
22
22
|
default_executable:
|
23
23
|
dependencies:
|
24
24
|
- !ruby/object:Gem::Dependency
|
@@ -96,6 +96,7 @@ files:
|
|
96
96
|
- test/bounces/tt_bounce_24.txt
|
97
97
|
- test/bounces/tt_bounce_25.txt
|
98
98
|
- test/bounces/unknown_code_bounce_01.txt
|
99
|
+
- test/fixtures/no_subject.txt
|
99
100
|
- test/non_bounces/tt_1234210666.txt
|
100
101
|
- test/non_bounces/tt_1234211024.txt
|
101
102
|
- test/non_bounces/tt_1234241664.txt
|
@@ -173,6 +174,7 @@ test_files:
|
|
173
174
|
- test/bounces/tt_bounce_24.txt
|
174
175
|
- test/bounces/tt_bounce_25.txt
|
175
176
|
- test/bounces/unknown_code_bounce_01.txt
|
177
|
+
- test/fixtures/no_subject.txt
|
176
178
|
- test/non_bounces/tt_1234210666.txt
|
177
179
|
- test/non_bounces/tt_1234211024.txt
|
178
180
|
- test/non_bounces/tt_1234241664.txt
|