Skip to content

Instantly share code, notes, and snippets.

@ritikesh
Last active August 25, 2020 09:01
Show Gist options
  • Save ritikesh/d768b13aba9ea85db60c60d371b7457c to your computer and use it in GitHub Desktop.
Save ritikesh/d768b13aba9ea85db60c60d371b7457c to your computer and use it in GitHub Desktop.
blog gists
class MailboxInterceptor
def self.delivering_email(mail)
set_smtp_settings(mail)
fix_encodings
ensure
unset_email_config
end
private
def self.set_smtp_settings(mail)
smtp_settings = tenant_mail_config&.smtp_settings : default_smtp_settings
ActionMailer.smtp_settings = smtp_settings
mail.delivery_method(:smtp, smtp_settings)
end
def unset_email_config
Thread.current[:tenant_mail_config] = nil
end
def tenant_mail_config=(mail_config)
Thread.current[:tenant_mail_config] = mail_config
end
def tenant_mail_config
Thread.current[:tenant_mail_config]
end
end
ActionMailer::Base.register_interceptor(MailboxInterceptor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment