Search code examples
ruby-on-railsrubyactionmailerdelayed-job

rails delayed_job with dynamic SMTP (settings)



Rails 4, Ruby 2, delayed_job 4.0.2
Can I set dynamic SMTP settings? For example, I need set user_name and password, where I should add that attributes?
My code:

  class Email < Struct.new(:user_id, :attr)
    def save
      user = User.find(user_id)
      ActionMailer::Base.smtp_settings
        .merge!({ user_name: attr['serverEmail'], password: attr['serverPass'] })
      ReportMailer.delay.send_report(user, attr)
    end
  end

As you can see, I added .merge!(...needed attributes...), but that is not working!
As for me delay_job working only if I add all attributes to ActionMailer::Base.smtp_settings = {...}
, but I need dynamic settings Need help! Thanks!
P.S.
Copy at github


Solution

  • I've got ansver from albus522 (delay_job developer): "You will have to change the settings in the send_report mailer method. Otherwise DJ has no idea it was changed".
    So, I've move

    ActionMailer::Base.smtp_settings
        .merge!({ user_name: attr['serverEmail'], password: attr['serverPass'] })
    

    from

    class Email
    

    to

    class ReportMailer < ActionMailer::Base
    #send_report method
    

    and all working!
    Thanks!