Rails ActionMailer - format sender and recipient name/email address Rails ActionMailer - format sender and recipient name/email address ruby-on-rails ruby-on-rails

Rails ActionMailer - format sender and recipient name/email address


If you are taking user input for name and email, then unless you very carefully validate or escape the name and email, you can end up with an invalid From header by simply concatenating strings. Here is a safe way:

require 'mail'address = Mail::Address.new email # ex: "john@example.com"address.display_name = name.dup   # ex: "John Doe"# Set the From or Reply-To header to the following:address.format # returns "John Doe <john@example.com>"


@recipients   = "\"#{user.name}\" <#{user.email}>"@from         = "\"MyCompany\" <info@mycompany.com>"


In rails3 I place the following in each environment. i.e. production.rb

ActionMailer::Base.default :from => "Company Name <no-reply@production-server.ca>"

Placing quotations around the company name did not work for me in Rails3.