Rails ActionMailer with multiple SMTP servers

前端 未结 11 1085
一整个雨季
一整个雨季 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: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.

提交回复
热议问题