Grn*_*gle 109 ruby-on-rails actionmailer
使用ActionMailer时,有没有办法为发件人和收件人信息指定电子邮件和名称?
通常你会这样做:
@recipients   = "#{user.email}"
@from         = "info@mycompany.com"
@subject      = "Hi"
@content_type = "text/html"
但是,我也想指定名字 - MyCompany <info@mycompany.com>, John Doe <john.doe@mycompany>.
有没有办法做到这一点?
Jam*_*ney 232
如果您正在接收用户输入的姓名和电子邮件,那么除非您非常仔细地验证或转义名称和电子邮件,否则只需连接字符串就可以得到无效的From标头.这是一种安全的方式:
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>"
Edu*_*coz 97
@recipients   = "\"#{user.name}\" <#{user.email}>"
@from         = "\"MyCompany\" <info@mycompany.com>"
ast*_*ohn 42
在rails3中,我将以下内容放在每个环境中.即production.rb
ActionMailer::Base.default :from => "Company Name <no-reply@production-server.ca>"
在Rails3中,在公司名称周围放置引号不适用于我.
在Rails 2.3.3中,引入了ActionMailer中的一个错误.你可以在这里看到门票#2340.它在2-3-stable和master中得到解决,所以它将在3.x和2.3.6中修复.
要在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
我喜欢使用的版本是
%`"#{account.full_name}" <#{account.email}>`
`<<是反击.
您也可以将其更改为
%|"#{account.full_name}" <#{account.email}>|
%\"#{account.full_name}" <#{account.email}>\
%^"#{account.full_name}" <#{account.email}>^
%["#{account.full_name}" <#{account.email}>]
| 归档时间: | 
 | 
| 查看次数: | 43957 次 | 
| 最近记录: |