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

后端 未结 6 1505
不知归路
不知归路 2020-12-04 18:40

I saw this post but mine is slightly different:

Rails ActionMailer with multiple SMTP servers

I am allowing the users to send mail using their own SMTP crede

6条回答
  •  独厮守ぢ
    2020-12-04 19:34

    For Rails 3.2.x

    You can include AbstractController::Callbacks in your mailer class and the do a "after_filter :set_delivery_options" inside the mailer.

    The set_delivery_options method would have access to instance variables setup by you in your mailer action and you can access the mail object as "message".

    class MyNailer < ActionMailer::Base
      include AbstractController::Callbacks
      after_filter :set_delivery_options
    
      def nail_it(user)
        @user = user
        mail :subject => "you nailed it"
      end
    
      private
    
      def set_delivery_options
        message.delivery_method.settings.merge!(@user.company.smtp_creds)
      end
    end
    

提交回复
热议问题