ActionMailer를 사용할 때 발신자 및 수신자 정보에 대한 이메일 및 이름을 지정하는 방법이 있습니까?
일반적으로 다음을 수행합니다.
@recipients = "#{user.email}"
@from = "info@mycompany.com"
@subject = "Hi"
@content_type = "text/html"
그러나, 나는 저기 …로 이름을 지정하려면 MyCompany <info@mycompany.com>
, John Doe <john.doe@mycompany>
.
그렇게하는 방법이 있습니까?
답변
이름과 이메일에 대한 사용자 입력을받는 경우 이름과 이메일을 매우 신중하게 확인하거나 이스케이프하지 않는 한 문자열을 연결하여 잘못된 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>"
답변
@recipients = "\"#{user.name}\" <#{user.email}>"
@from = "\"MyCompany\" <info@mycompany.com>"
답변
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}>]
답변
적어도 새로운 AR 형식의 또 다른 짜증나는 측면은 ‘default’가 클래스 수준에서 호출된다는 것을 기억하는 것입니다. 인스턴스 전용 루틴을 참조하면 자동으로 실패하고 사용하려고 할 때 제공됩니다.
NoMethodError: undefined method `new_post' for Notifier:Class
내가 사용한 결과는 다음과 같습니다.
def self.named_email(name,email) "\"#{name}\" <#{email}>" end
default :from => named_email(user.name, user.email)