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 = "foo@bar.com"
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.