We have a rails app that can send emails to people. We're using the mail gem (v2.7.1)
Some of the users are typoing their names - not their email addresses - in ways that are causing syntax errors when we try and send the email.
Here are some examples, one that works fine, and two different failing examples - names have been changed, but the typos have been kept the same
Working:
to = "\"Joe Bloggs\"<joe.bloggs@example.com>"
m = Mail.new
m.to = to
#<Mail::Message:92412660, Multipart: false, Headers: <To: "Joe Bloggs"<joe.bloggs@example.com>>>
Failure 1:
to = "\"Joe B\"log-gs\"<joe.bloggs@example.com>"
m = Mail.new
m.to = to
#<Mail::Message:92412660, Multipart: false, Headers: <To: "Joe B"log-gs"<joe.bloggs@example.com>>>
Falure 2:
to = "\"JOE BLOGGS\\\"<joe.bloggs@example.com>"
m = Mail.new
m.to = to
#<Mail::Message:92412660, Multipart: false, Headers: <To: "JOE BLOGGS\"<joe.bloggs@example.com>>>
When we try and send the failures we get the error:
Net::SMTPSyntaxError: 501 Syntax error in parameters
/app/vendor/ruby-2.4.4/lib/ruby/2.4.0/net/smtp.rb:969 in check_response
/app/vendor/ruby-2.4.4/lib/ruby/2.4.0/net/smtp.rb:937 in getok
/app/vendor/ruby-2.4.4/lib/ruby/2.4.0/net/smtp.rb:837 in mailfrom
/app/vendor/ruby-2.4.4/lib/ruby/2.4.0/net/smtp.rb:658 in send_message
/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp_connection.rb:54 in deliver!
/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:101 in block in deliver!
/app/vendor/ruby-2.4.4/lib/ruby/2.4.0/net/smtp.rb:519 in start
/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:109 in start_smtp_session
/gems/mail-2.7.1/lib/mail/network/delivery_methods/smtp.rb:100 in deliver!
/gems/mail-2.7.1/lib/mail/message.rb:276 in deliver!
I've tried to find out what the allowed characters in the to are, but I haven't been able to find it.
I'm wondering if there is something we could do escape the characters, I tried CGI.escape
but it didn't seem to have the effect I wanted
"%22JOE+BLOGGS%5C%22+%3Cjoe.bloggs%40example.com%3E"
#<Mail::Message:92412660, Multipart: false, Headers: <To: %22JOE+BLOGGS%5C%22+%3Cjoe.bloggs%40example.com%3E>>