Skip to content

Commit

Permalink
raise error when email address is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
i7an committed Dec 12, 2024
1 parent 12d5c97 commit ab3af80
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/mailtrap/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

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,
to: prepare_addresses(address_list(message['to'])&.addresses),
cc: prepare_addresses(address_list(message['cc'])&.addresses),
bcc: prepare_addresses(address_list(message['bcc'])&.addresses),
from: prepare_addresses(address_list(message, 'from')&.addresses).first,
to: prepare_addresses(address_list(message, 'to')&.addresses),
cc: prepare_addresses(address_list(message, 'cc')&.addresses),
bcc: prepare_addresses(address_list(message, 'bcc')&.addresses),
subject: message.subject,
text: prepare_text_part(message),
html: prepare_html_part(message),
Expand Down Expand Up @@ -50,10 +53,21 @@ def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLengt

HEADERS_TO_REMOVE = (SPECIAL_HEADERS + ACTIONMAILER_ADDED_HEADERS).freeze

def address_list(header)
header.respond_to?(:element) ? header.element : header&.address_list
# @param message [Mail::Message]
# @param header_name [String]
# @return [Mail::AddressList, nil]
def address_list(message, header_name)
field = message[header_name]
return nil unless field

unless field.errors.empty?
raise Mailtrap::Error, ["failed to parse '#{header_name}': '#{field.unparsed_value}'"]
end

field.respond_to?(:element) ? field.element : field.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 +80,7 @@ def prepare_headers(message)
.compact
end

# @param address [Mail::Address]
def prepare_address(address)
{
email: address.address,
Expand Down
10 changes: 10 additions & 0 deletions spec/mailtrap/mail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,15 @@
# (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}': 'invalid [email protected]'")
end
end
end
end
end

0 comments on commit ab3af80

Please sign in to comment.