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 credentials so it actually does come from them.
But they will be sent from the Rails app, so that means for each user I need to send their emails using their own SMTP server.
How can I do that?
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}