Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionMailer not sending emails properly if the :to field has a comma

One of my mailers looks like this:

mail(:from => "Support Team <[email protected]>",
     :to => "#{@user.alias} <#{@user.email}>",
     :subject => 'Verification Email')

However, if the alias of the user is "Foobar, Bar", then the email actually gets sent to: Foobar, Bar <[email protected]>. i.e. to foobar and to composer.

I think the problem is with the comma in "Foobar, Bar". Does this need to be escaped or something?

Should my mailer look like this instead:

mail(:from => "Support Team <[email protected]>",
     :to => @user.email,
     :subject => 'Verification Email')
like image 659
Christian Fazzini Avatar asked Feb 18 '12 13:02

Christian Fazzini


People also ask

How do I send an email to ROR?

Go to the config folder of your emails project and open environment. rb file and add the following line at the bottom of this file. It tells ActionMailer that you want to use the SMTP server. You can also set it to be :sendmail if you are using a Unix-based operating system such as Mac OS X or Linux.

How does Rails action mailer work?

Action Mailer allows you to send emails from your application using a mailer model and views. So, in Rails, emails are used by creating mailers that inherit from ActionMailer::Base and live in app/mailers. Those mailers have associated views that appear alongside controller views in app/views.


1 Answers

Certainly spaces (and possibly commas) in the name part of an email header must be included within quotes.

Quoting a name is sometimes optional, but never forbidden, so for simplicity, try:

mail(:from => "\"Support Team\" <[email protected]>",
     :to => "\"#{@user.alias}\" <#{@user.email}>",
     :subject => 'Verification Email')

EDIT For completeness, I have added escaped quotes to from, because they should also be necessary.

like image 182
SimonMayer Avatar answered Nov 04 '22 11:11

SimonMayer