Rails ActionMailer-格式化发件人和收件人名称/电子邮件地址 [英] Rails ActionMailer - format sender and recipient name/email address

查看:95
本文介绍了Rails ActionMailer-格式化发件人和收件人名称/电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ActionMailer时是否可以为发件人和收件人信息指定电子邮件和名称?

Is there a way to specify email AND name for sender and recipient info when using ActionMailer?

通常情况下,您会这样做:

Typically you'd do:

@recipients   = "#{user.email}"
@from         = "info@mycompany.com"
@subject      = "Hi"
@content_type = "text/html"

但是,我也想指定名称- MyCompany< info@mycompany.com> John Doe< john.doe@mycompany>

But, I want to specify name as well-- MyCompany <info@mycompany.com>, John Doe <john.doe@mycompany>.

有没有办法做到这一点?

Is there a way to do that?

推荐答案

如果您接受用户输入的姓名和电子邮件,那么除非您非常仔细地验证或转义姓名和电子邮件,否则您可以通过简单地串联字符串而以无效的From头结尾。这是一种安全的方法:

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>"

这篇关于Rails ActionMailer-格式化发件人和收件人名称/电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆