Reputation: 10207
In my Ruby on Rails application I have a Mailer
class that sends email with a user defined from
header:
def hello_email(user)
@user = user
from_email = %("#{@user.name}" <#{@user.email}>)
to_email = "[email protected]"
mail(from: from_email, to: to_email, subject: 'Hello everybody')
end
I am using a slightly different version of this code and have them delivered with Sucker Punch.
The problem I encountered is that the mailer method silently fails when @user.name
is malformatted, e.g. contains a comma, producing a Net::SMTPSyntaxError 504 5.5.2
in the logs.
What would be the best way to prevent that? I'd like to write a validation method in the User
class that blacklists or whitelists certain characters but I don't know which ones. The only character causing problems so far has been a comma. And it was quite different to track down.
Thanks for any help.
Upvotes: 0
Views: 160
Reputation: 10207
OK, I managed to find the solution myself.
This is the old code inside my User
model:
def sender
display_name = profile.company_name? ? profile.company_name : name
"#{display_name} (via #{APP_NAME})<#{NO_REPLY_EMAIL}>"
end
This is the corrected version:
def sender
display_name = profile.company_name? ? profile.company_name : name
%("#{display_name} (via #{APP_NAME})" <#{NO_REPLY_EMAIL}>)
end
The key is to enclose the display name in double quotes.
Upvotes: 0
Reputation: 6121
My best guess is that you have to detect for ,
and ;
only, and what made me conclude it is that if you try to compose an email (e.g. gmail), you start typing name in To
section, and as soon as you type ,
or ;
it considers it as a delimiter. so the regex is pretty simple
/[,;]/
UPDATE
Found this supporting answer here
Upvotes: 1