Skip to content

Commit cecd916

Browse files
authored
Merge pull request #35 from railsware/handle-mail-parse-error
Handle Mail parse error
2 parents 12d5c97 + 5be0011 commit cecd916

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
## [2.1.2] - 2024-12-13
2+
3+
- Improved handling of invalid `from`, `to`, `cc`, `bcc` headers when sending
4+
with Action Mailer
5+
16
## [2.1.1] - 2024-12-11
27

3-
- Improved handling of empty `from`
8+
- Improved handling of empty `from` header when sending with Action Mailer
49

510
## [2.1.0] - 2024-07-08
611

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
mailtrap (2.1.1)
4+
mailtrap (2.1.2)
55

66
GEM
77
remote: https://rubygems.org/

lib/mailtrap/mail.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
require_relative 'mail/base'
66
require_relative 'mail/from_template'
7+
require_relative 'errors'
78

89
module Mailtrap
910
module Mail
1011
class << self
12+
# @param message [Mail::Message]
13+
# @return [Mailtrap::Mail::Base]
1114
def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
1215
Mailtrap::Mail::Base.new(
1316
from: prepare_addresses(address_list(message['from'])&.addresses).first,
@@ -50,10 +53,19 @@ def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLengt
5053

5154
HEADERS_TO_REMOVE = (SPECIAL_HEADERS + ACTIONMAILER_ADDED_HEADERS).freeze
5255

56+
# @param header [Mail::Field, nil]
57+
# @return [Mail::AddressList, nil]
5358
def address_list(header)
54-
header.respond_to?(:element) ? header.element : header&.address_list
59+
return nil unless header
60+
61+
unless header.errors.empty?
62+
raise Mailtrap::Error, ["failed to parse '#{header.name}': '#{header.unparsed_value}'"]
63+
end
64+
65+
header.respond_to?(:element) ? header.element : header.address_list
5566
end
5667

68+
# @param addresses [Array<Mail::Address>, nil]
5769
def prepare_addresses(addresses)
5870
Array(addresses).map { |address| prepare_address(address) }
5971
end
@@ -66,6 +78,7 @@ def prepare_headers(message)
6678
.compact
6779
end
6880

81+
# @param address [Mail::Address]
6982
def prepare_address(address)
7083
{
7184
email: address.address,

lib/mailtrap/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Mailtrap
4-
VERSION = '2.1.1'
4+
VERSION = '2.1.2'
55
end

spec/mailtrap/mail_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,18 @@
142142
# (There will be a "'from' is required" error from the server)
143143
its(:from) { is_expected.to be_nil }
144144
end
145+
146+
%i[from to cc bcc].each do |header|
147+
context "when '#{header}' is invalid" do
148+
let(:message_params) { super().merge(header => 'invalid [email protected]') }
149+
150+
it 'raises an error' do
151+
expect { mail }.to raise_error(
152+
Mailtrap::Error,
153+
"failed to parse '#{header.capitalize}': 'invalid [email protected]'"
154+
)
155+
end
156+
end
157+
end
145158
end
146159
end

0 commit comments

Comments
 (0)