Rails ActionMailer with multiple SMTP servers

前端 未结 11 1076
一整个雨季
一整个雨季 2020-12-13 06:19

I have a need to use two different smtp servers in a Rails application. It appears that the way ActionMailer is constructed, it is not possible to have different smtp_settin

11条回答
  •  [愿得一人]
    2020-12-13 07:02

    Solution for Rails 3.2:

    class SomeMailer < ActionMailer::Base
    
      include AbstractController::Callbacks
      after_filter :set_delivery_options
    
      private
      def set_delivery_options
        settings = {
          :address => 'smtp.server',
          :port => 587,
          :domain => 'your_domain',
          :user_name => 'smtp_username',
          :password => 'smtp_password',
          :authentication => 'PLAIN' # or something
        }
    
        message.delivery_method.settings.merge!(settings)
      end
    end
    

    Solution inspired by How to send emails with multiple, dynamic smtp using Actionmailer/Ruby on Rails

提交回复
热议问题