Railsのactionmailerの - 形式の送信者と受信者名/ Eメールアドレス

StackOverflow https://stackoverflow.com/questions/957422

  •  12-09-2019
  •  | 
  •  

質問

actionmailerのを使用している場合、送信者と受信者の情報のための電子メールと名前を指定する方法はありますか?

一般的にあなたがしたい:

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

しかし、私はwell-- MyCompany <info@mycompany.com>John Doe <john.doe@mycompany>として名を指定します。

それを行う方法はありますか?

役に立ちましたか?

解決

あなたが名前と電子メールのためにユーザーの入力を取っている場合は、非常に慎重に名前とメールアドレスを検証したり脱出しない限り、

、そして、あなたは単純に文字列を連結することにより、ヘッダから無効で終わることができます。ここで安全な方法があります:

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 をこっちにチケットを見ることができます。それは3.xおよび2.3.6で修正されますので、これは2-3安定とマスターで解決されます。

*あなたは、チケットのコメントの中に提供されたコードを使用することができます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形式で、「デフォルト」がクラスレベルで呼び出されたことを覚えておくことです。あなたはそれを使用しようとすると、インスタンス専用ですルーチンを参照すると、それは静かに失敗し、与える原因ます:

 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)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top