LoginSignup
5
4

More than 5 years have passed since last update.

ActionMailer で envelope_from を指定する方法

Posted at

昔は sendmail を利用している場合、sendmail_settings で envelope_from を指定できた。

config/application.rb
config.action_mailer.sendmail_settings[:arguments] = '-f hogehoge@example.com'

いまは出来ないご様子。
https://github.com/mikel/mail/commit/4875bc2ba1670289a34a155569af2f51abddc316

envelope_from がどこで確定するのか調べると Mail::Message#smtp_envelope_from にたどり着く。

mail-x.x.x/lib/mail/message.rb
    # Returns the SMTP Envelope From value of the mail object, as a single
    # string of an address spec.
    #
    # Defaults to Return-Path, Sender, or the first From address.
    #
    # Example:
    #
    #  mail.smtp_envelope_from = 'Mikel <mikel@test.lindsaar.net>'
    def smtp_envelope_from( val = nil )
      if val
        self.smtp_envelope_from = val
      else
        @smtp_envelope_from || return_path || sender || from_addrs.first
      end
    end

ActionMailerの公式ドキュメントには return_pathsender を指定すると envelope_from を指定できると記載されているが、envelope_from だけを指定したい時は次のようにすればよい。

app/mailers/application_mailer.rb
  after_action :set_envelope_from

  private
  # envelope-from を設定する
  #
  def set_envelope_from
    mail.smtp_envelope_from = 'hogehoge@example.com'
  end
end
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4