Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Mail parse error #35

Merged
merged 2 commits into from
Dec 13, 2024
Merged
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## [2.1.2] - 2024-12-13

- Improved handling of invalid `from`, `to`, `cc`, `bcc` headers when sending
with Action Mailer

## [2.1.1] - 2024-12-11

- Improved handling of empty `from`
- Improved handling of empty `from` header when sending with Action Mailer

## [2.1.0] - 2024-07-08

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
mailtrap (2.1.1)
mailtrap (2.1.2)

GEM
remote: https://rubygems.org/
Expand Down
15 changes: 14 additions & 1 deletion lib/mailtrap/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

require_relative 'mail/base'
require_relative 'mail/from_template'
require_relative 'errors'

module Mailtrap
module Mail
class << self
# @param message [Mail::Message]
# @return [Mailtrap::Mail::Base]
def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
Mailtrap::Mail::Base.new(
from: prepare_addresses(address_list(message['from'])&.addresses).first,
Expand Down Expand Up @@ -50,10 +53,19 @@ def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLengt

HEADERS_TO_REMOVE = (SPECIAL_HEADERS + ACTIONMAILER_ADDED_HEADERS).freeze

# @param header [Mail::Field, nil]
# @return [Mail::AddressList, nil]
def address_list(header)
header.respond_to?(:element) ? header.element : header&.address_list
return nil unless header

unless header.errors.empty?
Copy link
Contributor Author

@i7an i7an Dec 12, 2024

Choose a reason for hiding this comment

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

interestingly one word email address e.g. invalid are parsed with no issues. Server then returns "invalid email address" error.

raise Mailtrap::Error, ["failed to parse '#{header.name}': '#{header.unparsed_value}'"]
end

header.respond_to?(:element) ? header.element : header.address_list
end

# @param addresses [Array<Mail::Address>, nil]
def prepare_addresses(addresses)
Array(addresses).map { |address| prepare_address(address) }
end
Expand All @@ -66,6 +78,7 @@ def prepare_headers(message)
.compact
end

# @param address [Mail::Address]
def prepare_address(address)
{
email: address.address,
Expand Down
2 changes: 1 addition & 1 deletion lib/mailtrap/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Mailtrap
VERSION = '2.1.1'
VERSION = '2.1.2'
end
13 changes: 13 additions & 0 deletions spec/mailtrap/mail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,18 @@
# (There will be a "'from' is required" error from the server)
its(:from) { is_expected.to be_nil }
end

%i[from to cc bcc].each do |header|
context "when '#{header}' is invalid" do
let(:message_params) { super().merge(header => 'invalid [email protected]') }

it 'raises an error' do
expect { mail }.to raise_error(
Mailtrap::Error,
"failed to parse '#{header.capitalize}': 'invalid [email protected]'"
)
end
end
end
end
end
Loading