email-bounce-sorter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f114ed67b247a9025ad9c37dc93372ee74ce7531a40e3f25f28a2c5006f507d2
4
+ data.tar.gz: bb4a311b6cadd7e6a288215055fd3543ae43c826856adb9e7a47bca283b8cd6f
5
+ SHA512:
6
+ metadata.gz: 1d7792134c8053c6f0fe151f31022e4c1096759b3811b7731d489fdb9f41f07445118034c485defded00a0240bfb96f6cf008c1ad16a145f6c904a0283b3f358
7
+ data.tar.gz: 4e8993fda56cbaa30ad3cdc73eb5c46dda963fc84b0beed05fffe37a523c851b5345553c4a9f801493cf25735d194ea03d586dc9d0769c15bd8449b70cc49a1c
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 OneGTMLab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # email-bounce-sorter
2
+
3
+ A small Ruby command-line tool for the idea behind the GTM glossary entry
4
+ [Email Bounce](https://www.onegtmlab.dev/glossary/email-bounce): sort a list of bounces into the ones to remove and the ones that may succeed later,
5
+ and work out the bounce rate.
6
+
7
+ What the glossary entry teaches, and what this tool does with it:
8
+
9
+ - A **hard bounce** is a permanent failure, such as an address or domain that does not exist; the address should be removed. The tool lists it under `remove`.
10
+ - A **soft bounce** is temporary, such as a full mailbox or a server that is briefly unavailable; the message may succeed later. The tool lists it under `retry`.
11
+ - Bounces affect more than the one message, and keeping them low is one of the basic conditions of good deliverability, so the tool also prints the bounce rate
12
+ (bounces divided by emails sent). The entry's own example, 500 emails sent and 40 hard bounces, comes out at 8.0% in the tests.
13
+ - Teams reduce bounces by verifying addresses before sending and removing hard bounces at once.
14
+
15
+ The entry says a bounce comes back "usually with a code explaining why". The tool reads that code by its first digit, the standard reply-code split:
16
+ 5 (5xx or 5.x.x) is permanent, 4 is temporary. A row with no usable code goes to `review` for a person to look at; it is never guessed.
17
+ The glossary page gives no number for how many soft bounces are too many, so there is no default: pass `--soft-limit N` if you want an address with N or more
18
+ soft bounces moved to `remove`. Ruby 3 or later, standard library only.
19
+
20
+ ```bash
21
+ ruby bin/email-bounce-sorter examples/bounces.csv --sent 500
22
+ ruby bin/email-bounce-sorter examples/bounces.csv --sent 500 --soft-limit 2 --out out
23
+ ```
24
+
25
+ Input is lines of `email,code` (no quoted commas). The rate counts bounce rows, not unique addresses.
26
+
27
+ ## Test
28
+
29
+ ```bash
30
+ ruby test/bounce_sorter_test.rb
31
+ ```
32
+
33
+ The addresses in `examples/` are made up, on reserved example domains. MIT licensed.
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/bounce_sorter"
5
+
6
+ def usage
7
+ puts "usage: email-bounce-sorter FILE.csv --sent N [--soft-limit N] [--out DIR]\n" \
8
+ " FILE.csv has lines of: email,code (for example: a@example.com,550 5.1.1)"
9
+ end
10
+
11
+ args = ARGV.dup
12
+ file = args.shift
13
+ if file.nil? || file.start_with?("--")
14
+ usage
15
+ exit(file.nil? ? 0 : 2)
16
+ end
17
+
18
+ opts = {}
19
+ until args.empty?
20
+ key = args.shift
21
+ case key
22
+ when "--sent" then opts[:sent] = Integer(args.shift)
23
+ when "--soft-limit" then opts[:soft_limit] = Integer(args.shift)
24
+ when "--out" then opts[:out] = args.shift
25
+ else
26
+ warn "unknown option #{key}"
27
+ exit 2
28
+ end
29
+ end
30
+
31
+ begin
32
+ events = BounceSorter.parse(File.read(file))
33
+ result = BounceSorter.sort(events, soft_limit: opts[:soft_limit])
34
+ puts "#{events.size} bounce rows: #{result.hard} hard, #{result.soft} soft, #{result.unclassified} without a usable code"
35
+ puts "remove #{result.remove.size} | retry later #{result.retry.size} | review by hand #{result.review.size}"
36
+ if opts[:sent]
37
+ puts "bounce rate: #{BounceSorter.rate(events.size, opts[:sent])}% (#{events.size} of #{opts[:sent]} sent)"
38
+ puts "hard bounce rate: #{BounceSorter.rate(result.hard, opts[:sent])}%"
39
+ end
40
+ if opts[:out]
41
+ Dir.mkdir(opts[:out]) unless Dir.exist?(opts[:out])
42
+ { "remove" => result.remove, "retry" => result.retry, "review" => result.review }.each do |name, list|
43
+ File.write(File.join(opts[:out], "#{name}.txt"), list.map { |e| "#{e}\n" }.join)
44
+ end
45
+ puts "wrote remove.txt, retry.txt, review.txt to #{opts[:out]}"
46
+ end
47
+ puts "Definitions: https://www.onegtmlab.dev/glossary/email-bounce"
48
+ rescue ArgumentError, Errno::ENOENT => e
49
+ warn e.message
50
+ exit 2
51
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Sorts bounce events into hard and soft bounces, the way the GTM glossary entry "Email Bounce" defines them:
4
+ # hard bounce = permanent failure (an address or domain that does not exist): remove the address.
5
+ # soft bounce = temporary failure (a full mailbox, a server briefly unavailable): the message may succeed later.
6
+ # Source page: https://www.onegtmlab.dev/glossary/email-bounce
7
+ #
8
+ # The page says a bounce is returned "usually with a code explaining why". This tool reads that code by its class:
9
+ # a status starting 5 (5xx or 5.x.x) is permanent, one starting 4 (4xx or 4.x.x) is temporary. That is the
10
+ # standard reply-code split; the glossary page itself only gives the permanent versus temporary definition.
11
+ # A row with no usable code is left "unclassified" for a person to look at, never guessed.
12
+ module BounceSorter
13
+ Event = Struct.new(:email, :code, keyword_init: true)
14
+
15
+ module_function
16
+
17
+ # Returns :hard, :soft or :unclassified.
18
+ def classify(code)
19
+ first = code.to_s.strip[0]
20
+ case first
21
+ when "5" then :hard
22
+ when "4" then :soft
23
+ else :unclassified
24
+ end
25
+ end
26
+
27
+ # Result of sorting a list of events.
28
+ # remove: addresses to remove (any hard bounce, plus soft ones that reached soft_limit, if you set one)
29
+ # retry: addresses with only soft bounces below the limit
30
+ # review: addresses whose only rows have no usable code
31
+ Result = Struct.new(:remove, :retry, :review, :hard, :soft, :unclassified, keyword_init: true)
32
+
33
+ def sort(events, soft_limit: nil)
34
+ raise ArgumentError, "soft_limit must be 1 or more" if soft_limit && soft_limit < 1
35
+
36
+ by_email = events.group_by { |e| e.email.to_s.strip.downcase }
37
+ by_email.delete("")
38
+ remove = []
39
+ retry_list = []
40
+ review = []
41
+ hard = soft = unclassified = 0
42
+ by_email.each do |email, rows|
43
+ kinds = rows.map { |r| classify(r.code) }
44
+ hard += kinds.count(:hard)
45
+ soft += kinds.count(:soft)
46
+ unclassified += kinds.count(:unclassified)
47
+ if kinds.include?(:hard)
48
+ remove << email
49
+ elsif kinds.include?(:soft)
50
+ soft_count = kinds.count(:soft)
51
+ (soft_limit && soft_count >= soft_limit ? remove : retry_list) << email
52
+ else
53
+ review << email
54
+ end
55
+ end
56
+ Result.new(remove: remove.sort, retry: retry_list.sort, review: review.sort,
57
+ hard: hard, soft: soft, unclassified: unclassified)
58
+ end
59
+
60
+ # Bounce rate = bounces / emails sent, as a percentage rounded to one decimal.
61
+ def rate(bounces, sent)
62
+ raise ArgumentError, "sent must be 1 or more" unless sent.is_a?(Integer) && sent >= 1
63
+ raise ArgumentError, "bounces cannot be negative" if bounces.negative?
64
+
65
+ (bounces * 100.0 / sent).round(1)
66
+ end
67
+
68
+ # Reads "email,code" lines. A header line starting with "email" is skipped. No quoted commas.
69
+ def parse(text)
70
+ text.each_line.filter_map do |line|
71
+ email, code = line.chomp.split(",", 2).map { |s| s&.strip }
72
+ next if email.nil? || email.empty? || email.downcase == "email"
73
+
74
+ Event.new(email: email, code: code)
75
+ end
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: email-bounce-sorter
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jainendra Ojha
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: "Ruby CLI that sorts email bounces into hard (remove), soft (retry later) and unclassified by reply-code class, and works out the bounce rate."
13
+ email:
14
+ - account@onegtmlab.dev
15
+ executables:
16
+ - email-bounce-sorter
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - bin/email-bounce-sorter
23
+ - lib/bounce_sorter.rb
24
+ homepage: "https://www.onegtmlab.dev/glossary/email-bounce"
25
+ licenses:
26
+ - MIT
27
+ metadata:
28
+ source_code_uri: "https://github.com/jaizendra-netizen/email-bounce-sorter"
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ -
35
+ - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: "2.7"
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ -
41
+ - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ requirements: []
45
+ rubygems_version: 4.1.0
46
+ specification_version: 4
47
+ summary: "Ruby CLI that sorts email bounces into hard (remove), soft (retry later) and unclassified by reply-code class, and works out the bounce rate"
48
+ test_files: []