It seems that the Rails core team is so busy building awesomeness into Rails that they don’t have much time to improve the documentation. This is why docrails was started. The new Rails Guides are immensely helpful, and quite the improvement over the old wiki. However, you’ll still find a few surprises if you dig around in other people’s code like this handy feature I’ve found in ActionMailer.
ActionMailer let’s you specify the from address on emails sent out, but did you know you could also set a nice name in addition to that? You might have a basic mailer method like this.
def my_email(to_email, to_name) from 'myname@example.com' recipients to_email subject "Hello There #{to_name}" body(:to_email => to_email, :to_name => to_name) end
We pass in an email address to send to and the person’s name. The from name is simply hardcoded as a string. If that email showed up in your inbox, it would probably say it’s from myname. Depending on the email client, it may just show the address it came from. That’s great, but it doesn’t look very nice.
Enter the undocumented syntax I’ve stumbled on.
def my_email(to_email, to_name) from 'My Pretty Name <myname @example.com>' recipients to_email subject "Hello There #{to_name}" body(:to_email => to_email, :to_name => to_name) end</myname>
All you need to do is add the name you want to appear in the from field before your email address, then add a space, and finally wrap the email with angle brackets. That’s it! Now when your email arrives in someone’s inbox, it will show that it’s from My Pretty Name.
The syntax leaves a bit to be desired in my opinion. It doesn’t feel very Rails-y or even like the rest of ActionMailer. When specifying the recipients in ActionMailer for example, you can pass in either a single email address as a string or an array of string email addresses. The syntax should be similar with the from name and address. Here’s my proposed syntax.
def my_email(to_email, to_name) from ['My Pretty Name', 'myname@example.com'] recipients to_email subject "Hello There #{to_name}" body(:to_email => to_email, :to_name => to_name) end
Seems like a logical addition, eh? Pass in the from name as the first item in an array and the email as the second. Using a hash instead would also be acceptable. Maybe I’ll dig into Rails and submit my first ever patch.
Nice find!
I’m not sure but I think the feature you found might be provided by the TMail library that ActionMailer uses. The angle bracket syntax for display name is described in RFC 2822 (http://tools.ietf.org/html/rfc2822#section-3.4)
I think the use of display names whenever possible certainly helps the user experience. Thanks for the write-up.
Have you tried this with exim and succeeded? I keep getting errors complaining about the from-syntax, like this:
SMTP syntax error in “MAIL FROM:<Mark Test >” H=localhost (mydomain.de) [127.0.0.1]:55302 I=[127.0.0.1]:25 “@” or “.” expected after “Mark”
It appears to me, that ActionMailer is using to many ” signs here?