Open jkhulme opened 5 years ago
The rules here are quite complicated. You can read them in RFC5322 if you're interested.
But to summarize, in quoted names like you're using, you can use ASCII %d33-126, except for %d34 and %d92 (double quote and backslash). This is why your examples are having problems.
It is possible to escape those characters with a backslash. Maybe something like this would work for you?
email = "joe.bloggs@example.com"
name = "Joe B\"log-gs"
escaped_name = name.gsub(/\\/) { '\\\\' }.gsub(/"/) { '\"' }
to = "\"#{escaped_name}\" <#{email}>"
# Test this can be parsed:
Mail::AddressList.new(to)
We're using v2.7.1 of the mail gem with ruby 2.4.4
Some of our 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:
Failure 1:
Falure 2:
When we try and send the failures we get the error:
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
Any advice on how we could prevent the errors, and allow the emails to be sent correctly would be appreciated