Rails ActionMailer with multiple SMTP servers

前端 未结 11 1050
一整个雨季
一整个雨季 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 06:57

    I'm afraid it's not doable natively.
    But you can trick it a bit by modifying the @@smtp_settings variable in the model.

    There's an article on Oreilly which explains it pretty well even though they code is not clean at all. http://broadcast.oreilly.com/2009/03/using-multiple-smtp-accounts-w.html

    0 讨论(0)
  • 2020-12-13 06:57

    When I wanted a quick test in the console (Rails 5) I did the following:

    settings = { username: "" } # etc
    mailer = MyMailer.some_method
    mailer.delivery_method.settings.merge!(settings)
    mailer.deliver
    
    0 讨论(0)
  • 2020-12-13 06:59

    Tried to use jkrall's option with Rails 3.2.1 but for some reason it wouldn't override default configuration, but doing:

    MyMailer.my_email.delivery_method.settings.merge!(SMTP_SETTINGS).deliver
    

    Similar to http://www.scottw.com/multiple-smtp-servers-with-action-mailer, made it work.

    0 讨论(0)
  • 2020-12-13 07:01

    Rails-2.3.*

    # app/models/user_mailer.rb
    class UserMailer < ActionMailer::Base
      def self.smtp_settings
        USER_MAILER_SMTP_SETTINGS
      end
    
      def spam(user)
        recipients user.mail
        from 'spam@example.com'
        subject 'Enlarge whatever!'
        body :user => user
        content_type 'text/html'
      end
    end
    
    # config/environment.rb
    ActionMailer::Base.smtp_settings = standard_smtp_settings
    USER_MAILER_SMTP_SETTINGS = user_smtp_settings
    
    # From console or whatever...
    UserMailer.deliver_spam(user)
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-13 07:11
    class UserMailer < ActionMailer::Base
      def welcome_email(user, company)
        @user = user
        @url  = user_url(@user)
        delivery_options = { user_name: company.smtp_user,
                             password: company.smtp_password,
                             address: company.smtp_host }
        mail(to: @user.email,
             subject: "Please see the Terms and Conditions attached",
             delivery_method_options: delivery_options)
      end
    end
    

    Rails 4 allows for dynamic delivery options. The above code is straight from the action mailer basics guide, which you can find here: http://guides.rubyonrails.org/v4.0/action_mailer_basics.html#sending-emails-with-dynamic-delivery-options

    With this, it is possible to have different smtp settings for every email you send, or, like in your use case for different sub classes like UserMailer, OtherMailer etc.

    0 讨论(0)
提交回复
热议问题