Ruby-on-rails Rails ActionMailer - 格式化发件人和收件人姓名/电子邮件地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/957422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:19:09  来源:igfitidea点击:

Rails ActionMailer - format sender and recipient name/email address

ruby-on-railsactionmailer

提问by Grnbeagle

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

有没有办法在使用 ActionMailer 时为发件人和收件人信息指定电子邮件和名称?

Typically you'd do:

通常你会这样做:

@recipients   = "#{user.email}"
@from         = "[email protected]"
@subject      = "Hi"
@content_type = "text/html"

But, I want to specify name as well-- MyCompany <[email protected]>, John Doe <john.doe@mycompany>.

但是,我也想指定 name-- MyCompany <[email protected]>, John Doe <john.doe@mycompany>.

Is there a way to do that?

有没有办法做到这一点?

回答by James McKinney

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:

如果您正在接受用户输入的姓名和电子邮件,那么除非您非常仔细地验证或转义姓名和电子邮件,否则您可能会通过简单地连接字符串而得到无效的 From 标头。这是一个安全的方法:

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

回答by Eduardo Scoz

@recipients   = "\"#{user.name}\" <#{user.email}>"
@from         = "\"MyCompany\" <[email protected]>"

回答by astjohn

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

在 rails3 中,我在每个环境中放置以下内容。即生产.rb

ActionMailer::Base.default :from => "Company Name <[email protected]>"

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

在 Rails3 中,公司名称周围的引号对我不起作用。

回答by anka

within Rails 2.3.3 a bug within the ActionMailer was introduced. You can see the ticket over here Ticket #2340. It's resolved in 2-3-stable and master so it will be fixed in 3.x and 2.3.6.

在 Rails 2.3.3 中引入了 ActionMailer 中的错误。您可以在此处查看票证 #2340。它已在 2-3-stable 和 master 中解决,因此将在 3.x 和 2.3.6 中修复。

For fixing the problem within 2.3.* you can use the code provided within the ticket comments:

要解决 2.3.* 中的问题,您可以使用票证注释中提供的代码:

module ActionMailer
  class Base
    def perform_delivery_smtp(mail)
      destinations = mail.destinations
      mail.ready_to_send
      sender = (mail['return-path'] && mail['return-path'].spec) || Array(mail.from).first

      smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
      smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
      smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
                 smtp_settings[:authentication]) do |smtp|
        smtp.sendmail(mail.encoded, sender, destinations)
      end
    end
  end
end

回答by Marc

The version I like to use of this is

我喜欢使用的版本是

%`"#{account.full_name}" <#{account.email}>`

` << are backticks.

` << 是反引号。

Update

更新

You could also change that to

你也可以把它改成

%|"#{account.full_name}" <#{account.email}>|
%\"#{account.full_name}" <#{account.email}>\
%^"#{account.full_name}" <#{account.email}>^
%["#{account.full_name}" <#{account.email}>]

Read more about string literals.

阅读有关字符串文字的更多信息。

回答by Kevin

Another irritating aspect, at least with the new AR format, is to remember that 'default' is called on the class level. Referencing routines that are instance-only causes it to silently fail and give when you try to use it:

另一个令人恼火的方面,至少对于新的 AR 格式,是记住在类级别调用“默认”。引用仅实例的例程会导致它静默失败并在您尝试使用它时给出:

 NoMethodError: undefined method `new_post' for Notifier:Class

Here's what I ended up using:

这是我最终使用的:

def self.named_email(name,email) "\"#{name}\" <#{email}>" end
default :from => named_email(user.name, user.email)