Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/mailtrap/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Mail # rubocop:disable Metrics/ModuleLength
category
customvariables
contenttype
replyto
].freeze
private_constant :SPECIAL_HEADERS

Expand Down Expand Up @@ -195,6 +196,7 @@ def from_message(message)
to: prepare_addresses(message['to']),
cc: prepare_addresses(message['cc']),
bcc: prepare_addresses(message['bcc']),
reply_to: prepare_addresses(message['reply-to']).first,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reply-to in the raw email.

Before:

 >=?UTF-8?q?"=EB=A7=88=EC=9D=B8=EB=A6=AC=EC=8A=A4=ED=8A=B8"_<support@mailtr?==?UTF-8?q?ap.io>?=

After:

=?utf-8?q?=EB=A7=88=EC=9D=B8=EB=A6=AC=EC=8A=A4=ED=8A=B8?=<support@mailtrap.io> 

subject: message.subject,
text: prepare_text_part(message),
html: prepare_html_part(message),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 73 additions & 1 deletion spec/mailtrap/mail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
let(:expected_headers) do
{
'X-Special-Domain-Specific-Header' => 'SecretValue',
'Reply-To' => 'Reply To <reply-to@railsware.com>',
'One-more-custom-header' => 'CustomValue'
}
end
Expand All @@ -53,6 +52,35 @@

it { is_expected.to eq(expected_headers) }
end

context 'when reply-to is added in varying formats' do
[
['"메일트랩" <support@mailtrap.io>', { email: 'support@mailtrap.io', name: '메일트랩' }],
['"Mailtrap Team" <support@mailtrap.io>', { email: 'support@mailtrap.io', name: 'Mailtrap Team' }],
['support@mailtrap.io', { email: 'support@mailtrap.io' }]
].each do |reply_to, expected_reply_to|
it "maps '#{reply_to}' to structured field and excludes header copy" do
message.reply_to = reply_to

expect(mail.reply_to).to eq(expected_reply_to)
expect(headers).not_to have_key('Reply-To')
end
end
end

context 'when custom header and reply-to variants are present' do
before do
message.header['Reply-To'] = 'Reply To <reply-to@railsware.com>'
message.header['REPLY-TO'] = 'Upper Case <upper-reply-to@railsware.com>'
message.header['reply-to'] = 'Lower Case <lower-reply-to@railsware.com>'
message.header['X-Special-Domain-Specific-Header'] = 'SecretValue'
end

it 'keeps only custom headers and strips all reply-to header variants' do
expect(mail.reply_to).to eq({ email: 'lower-reply-to@railsware.com', name: 'Lower Case' })
expect(headers).to eq('X-Special-Domain-Specific-Header' => 'SecretValue')
end
end
end

describe '#attachment' do
Expand Down Expand Up @@ -155,6 +183,19 @@
its(:template_variables) { is_expected.to eq('first_name' => 'John') }
end

context "when 'reply-to' is invalid" do
Copy link
Contributor

@i7an i7an Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the tests should be refactored. I can do that once merged.

  • this should be merged with the next block.

before do
message.header['Reply-To'] = 'invalid email@example.com'
end

it 'raises an error' do
expect { mail }.to raise_error(
Mailtrap::Error,
"failed to parse 'Reply-To': 'invalid email@example.com'"
)
end
end

%i[from to cc bcc].each do |header|
context "when '#{header}' is invalid" do
let(:message_params) { super().merge(header => 'invalid email@example.com') }
Expand All @@ -167,5 +208,36 @@
end
end
end

context "when 'reply-to' is invalid" do
let(:invalid_reply_to) { 'invalid email@example.com' }

before do
message.header['Reply-To'] = invalid_reply_to
end

it 'raises an error' do
expect { mail }.to raise_error(
Mailtrap::Error,
"failed to parse 'Reply-To': 'invalid email@example.com'"
)
end

context 'when address contains a folded line break' do
let(:invalid_reply_to) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please explain this case?

encoded_name = ['메일트랩 팀'.encode('UTF-8')].pack('m0')
folded_domain = %w[exa mple.com].join("\r\n ")

"=?UTF-8?B?#{encoded_name}?= <no-reply@#{folded_domain}>"
end

it 'raises an error' do
expect { mail }.to raise_error(
Mailtrap::Error,
/failed to parse 'Reply-To': '.*no-reply@exa/m
)
end
end
end
end
end