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

Error when send email with Round bracket In "From" #1573

Open
huythaimanh opened this issue Apr 4, 2023 · 1 comment
Open

Error when send email with Round bracket In "From" #1573

huythaimanh opened this issue Apr 4, 2023 · 1 comment

Comments

@huythaimanh
Copy link

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:

/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 huythaimanh changed the title Error when send email with Round bracket Error when send email with Round bracket In "From" Apr 4, 2023
@krisleech
Copy link

krisleech commented Apr 6, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants