Skip to content

Commit f065248

Browse files
authored
Include reply-to, cc + bcc in logging output (#20)
1 parent d98c395 commit f065248

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lib/courrier/email/providers/logger.rb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ def format_email_using(options)
2121
<<~EMAIL
2222
#{separator}
2323
Timestamp: #{Time.now.strftime("%Y-%m-%d %H:%M:%S %z")}
24-
From: #{@options.from}
25-
To: #{@options.to}
26-
Subject: #{@options.subject}
24+
#{meta_fields(from: options)}
2725
2826
Text:
2927
#{@options.text || "(empty)"}
@@ -35,6 +33,27 @@ def format_email_using(options)
3533
end
3634

3735
def separator = "-" * 80
36+
37+
def meta_fields(from:)
38+
fields = [
39+
[:from, "From"],
40+
[:to, "To"],
41+
[:reply_to, "Reply-To"],
42+
[:cc, "Cc"],
43+
[:bcc, "Bcc"],
44+
[:subject, "Subject"]
45+
]
46+
47+
fields.map do |field, label|
48+
value = from.send(field)
49+
50+
next if value.nil? || value.to_s.strip.empty?
51+
52+
"#{label}:".ljust(11) + value
53+
rescue NoMatchingPatternError
54+
nil
55+
end.compact.join("\n")
56+
end
3857
end
3958
end
4059
end

test/courrier/email/providers/logger_test.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ class LoggerTest < Minitest::Test
77
def test_formats_email_for_logging
88
email = TestEmail.new(
99
10-
10+
11+
reply_to: "[email protected]"
1112
)
1213
logger = Logger.new(options: email.options)
1314

@@ -20,6 +21,23 @@ def test_formats_email_for_logging
2021
assert_includes output, "HTML:\n<p>Test HTML Body</p>"
2122
end
2223

24+
def test_formats_optional_meta_fields_for_logging
25+
email = TestEmail.new(
26+
27+
28+
reply_to: "[email protected]",
29+
30+
31+
)
32+
logger = Logger.new(options: email.options)
33+
34+
output = capture_logger_output { logger.deliver }
35+
36+
assert_includes output, "Reply-To: [email protected]"
37+
assert_includes output, "Cc: [email protected]"
38+
assert_includes output, "Bcc: [email protected]"
39+
end
40+
2341
private
2442

2543
def capture_logger_output

0 commit comments

Comments
 (0)