Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
Copy Markdown
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.

61 changes: 60 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,51 @@

it { is_expected.to eq(expected_headers) }
end

context 'when reply-to is added in varying formats' do
before do
message.reply_to = 'Reply To <reply-to@railsware.com>'
end

it 'excludes reply-to from custom headers' do
expect(headers).not_to have_key('Reply-To')
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(headers).to eq('X-Special-Domain-Specific-Header' => 'SecretValue')
end
end
end

describe '#reply_to' do
before do
message.reply_to = 'Reply To <reply-to@railsware.com>'
end

it 'maps reply-to to the structured field' do
expect(mail.reply_to).to eq(email: 'reply-to@railsware.com', name: 'Reply To')
end

context 'when reply-to header 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>'
end

it 'maps the reply-to value to the structured field' do
expect(mail.reply_to).to eq({ email: 'lower-reply-to@railsware.com', name: 'Lower Case' })
end
end
end

describe '#attachment' do
Expand Down Expand Up @@ -167,5 +211,20 @@
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
end
end
end
Loading