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

后端 未结 6 1504
不知归路
不知归路 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:18

    Doing what is described in the other answer is not safe; you are setting class variables here, not instanced variables. If your Rails container is forking, you can do this, but now your application is depending on an implementation detail of the container. If you're not forking a new Ruby process, then you can have a race condition here.

    You should have a model that is extending ActionMailer::Base, and when you call a method, it will return a Mail::Message object. That is your instance object and is where you should change your settings. The settings are also just a hash, so you can inline it.

    msg = MyMailer.some_message
    msg.delivery_method.settings.merge!(@user.mail_settings)
    msg.deliver
    

    Where in the above mail_settings returns some hash with appropriate keys IE

    {:user_name=>username, :password=>password}
    

提交回复
热议问题