mail icon indicating copy to clipboard operation
mail copied to clipboard

Error when send email with Round bracket In "From"

Open huythaimanh opened this issue 2 years ago • 1 comments

I got this error when trying to send an SMTP email with a close round bracket (

Net::SMTPFatalError

Test SendMail ) <[email protected]> Invalid email address.

If I removed the bracket (. It can send an email successfully. Test SendMail <[email protected]>

Here is the log:

/usr/local/lib/ruby/2.6.0/net/smtp.rb:969→ check_response
--
/usr/local/lib/ruby/2.6.0/net/smtp.rb:937→ getok
/usr/local/lib/ruby/2.6.0/net/smtp.rb:837→ mailfrom
/usr/local/lib/ruby/2.6.0/net/smtp.rb:658→ send_message
mail-2.7.1/lib/mail/network/delivery_methods/smtp_connection.rb:54→ deliver!
mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:101→ block in deliver!
/usr/local/lib/ruby/2.6.0/net/smtp.rb:519→ start
mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:109→ start_smtp_session
mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:100→ deliver!
mail-2.7.1/lib/mail/message.rb:2159→ do_delivery
mail-2.7.1/lib/mail/message.rb:260→ block in deliver

Could the code be changed to support if we send an email with a single round bracket?

huythaimanh avatar Apr 04 '23 09:04 huythaimanh

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 <[email protected]>"
mail = Mail.new { to(to) }
mail[:to].addrs # => [#<Mail::Address:82860 Address: |Kris <[email protected]>| >]

# with
to = "Kris Leech [TECH] <[email protected]>"
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] <[email protected]>", #<Mail::Field::IncompleteParseError: Mail::AddressList can not parse |Kris Leech [TECH] <[email protected]>|: 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]')} <[email protected]>"
mail = Mail.new { to(to) }
mail[:to].addrs # => no error

krisleech avatar Apr 06 '23 09:04 krisleech