Open huythaimanh opened 1 year ago
I've had a similar problem since we get people's names from a third party which includes a department suffixed to the last name which includes square brackets.
# without
to = "Kris Leech <kris@example.com>"
mail = Mail.new { to(to) }
mail[:to].addrs # => [#<Mail::Address:82860 Address: |Kris <kris.leech@example.com>| >]
# with
to = "Kris Leech [TECH] <kris.leech@example.com>"
mail = Mail.new { to(to) }
mail[:to].addrs # => NoMethodError: undefined method `addrs' for #<Mail::UnstructuredField
Can see it is a parse error:
mail[:to].errors # => [["To", "Kris Leech [TECH] <kris.leech@example.com>", #<Mail::Field::IncompleteParseError: Mail::AddressList can not parse |Kris Leech [TECH] <kris.leech@example.com>|: Only able to parse up to "Kris Leech ">]]
If you are constructing the "to" yourself you can quote the name part:
QuoteName = Class.new do
extend Mail::Utilities
def self.call(name)
quote_phrase(name)
end
end
to = "#{QuoteName.('Kris Leech [TECH]')} <kris@example.com>"
mail = Mail.new { to(to) }
mail[:to].addrs # => no error
I got this error when trying to send an SMTP email with a close round bracket (
Net::SMTPFatalError
Test SendMail ) <no-reply@test.com> Invalid email address.
If I removed the bracket (. It can send an email successfully.
Test SendMail <no-reply@test.com>
Here is the log:
Could the code be changed to support if we send an email with a single round bracket?