View Categories

Rails ActionMailer with Multiple SMTP Servers

1 min read

Rails ActionMailer with Multiple SMTP Servers #

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_settings for a subclass. I could reload the smtp settings for each mailer class whenever a message is being sent, but that messes up the ExceptionNotifier plugin which is outside my control (unless I mess with it too). Does anyone have a solution/plugin for something like this?

Ideally I would like to have

class UserMailer < ActionMailer::Base; end
and then set in environment.rb

ActionMailer::Base.smtp_settings = standard_smtp_settings
UserMailer.smtp_settings = user_smtp_settings
Thus, most of my mailers including ExceptionNotifier would pickup the default settings, but the UserMailer would use a paid relay service.

Here is Some Solutions :

Solution 1: #

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

Solution 2: #

class MailerWithCustomSmtp < ActionMailer::Base
SMTP_SETTINGS = {
:address => “smtp.gmail.com”,
:port => 587,
:authentication => :plain,
:user_name => “custom_account@transfs.com”,
:password => ‘password’,
}

def awesome_email(bidder, options={})
with_custom_smtp_settings do
subject ‘Awesome Email D00D!’
recipients ‘someone@test.com’
from ‘custom_reply_to@transfs.com’
body ‘Hope this works…’
end
end

# Override the deliver! method so that we can reset our custom smtp server settings
def deliver!(mail = @mail)
out = super
reset_smtp_settings if @_temp_smtp_settings
out
end

private

def with_custom_smtp_settings(&block)
@_temp_smtp_settings = @@smtp_settings
@@smtp_settings = SMTP_SETTINGS
yield
end

def reset_smtp_settings
@@smtp_settings = @_temp_smtp_settings
@_temp_smtp_settings = nil
end
end

Solution 3: #

# app/models/user_mailer.rb
class UserMailer < ActionMailer::Base
def self.smtp_settings
USER_MAILER_SMTP_SETTINGS
end

def spam(user)
recipients user.mail
from ‘spam@example.com’
subject ‘Enlarge whatever!’
body :user => user
content_type ‘text/html’
end
end

# config/environment.rb
ActionMailer::Base.smtp_settings = standard_smtp_settings
USER_MAILER_SMTP_SETTINGS = user_smtp_settings

# From console or whatever…
UserMailer.deliver_spam(user)

Solution 4: #

class CustomMailer < ActionMailer::Base

before_deliver do |mail|
self.smtp_settings = custom_settings
end

after_deliver do |mail|
self.smtp_settings = default_settings
end

def some_message
subject “blah”
recipients “blah@blah.com”
from “ruby.ninja@ninjaness.com”
body “You can haz Ninja rb skillz!”
attachment some_doc
end

end

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *

Privacy Settings
We use cookies to enhance your experience while using our website. If you are using our Services via a browser you can restrict, block or remove cookies through your web browser settings. We also use content and scripts from third parties that may use tracking technologies. You can selectively provide your consent below to allow such third party embeds. For complete information about the cookies we use, data we collect and how we process them, please check our Privacy Policy
Youtube
Consent to display content from Youtube
Vimeo
Consent to display content from Vimeo
Google Maps
Consent to display content from Google
Spotify
Consent to display content from Spotify
Sound Cloud
Consent to display content from Sound