Skip to content
Saumya Kanoria edited this page Jul 29, 2016 · 7 revisions

Rails already provides outgoing email functionality via ActionMailer. All we do is add a bit of configurability on top, by enabling app admins to set SMTP settings and some other sender information through the admin UI.

But this is something that could easily be configured through environment variables or Rails secrets, so why this sugar? Because 1) admins might actually benefit from being able to set/change these settings, and 2) for multi-tenanted apps, this would actually become essential as each tenant would ideally like to use their own SMTP server.

In fact, these settings do fall back on Rails secrets, which in turn (by default) defer to environment variables, so you have several layers and options for configuration.

Screenshot

Outgoing email settings screenshot

Setup and Usage

Creating Mailers

To enable any mailer class to use the settings so configured, merely include the ConfigurableDeliveryMailer module as follows:

class SomeMailer < ActionMailer::Base
  include ConfigurableDeliverMailer
  
  # The rest of your code as usual
end

We have already modified the Devise mailer along these lines.

Adding/Removing Available Settings

You may like to add/remove/modify the possible outgoing email settings that can be set via the admin console.

These settings are built on the 'app settings' functionality provided by this starter kit, which provides a convenient way to define any application level settings required by the app, as well as create forms within the admin console to let app admins set/change these settings. This means that:

Modify the first two files as desired. You should not really need to modify the AppSettingsCtrl controller. It is only for completeness that we mention it above. If you wish to set/change the fallback defaults, do so in either secrets.yml or, as the secrets defer (by default) to environment variables anyway, in your environment variables.

Clone this wiki locally