We are using mailgun to send mails from our apps.
-
Check Last Pass if a client already has a mailgun account.
-
Create an account if not. Email template is: mailgun+{client}@infinum.hr
-
Create a domain for the app.
-
Contact your friendly neighborhood devops to add DNS records for domain verification.
-
Add
mailgun_rails
gem to gemfile -
Add smtp settings to your app. Example:
config.action_mailer.delivery_method = :mailgun config.action_mailer.mailgun_settings = { api_key: Rails.application.secrets.mailgun[:api_key], domain: Rails.application.secrets.mailgun[:domain] }
-
Profit
The HTTP API has some advantages over SMTP:
- It’s faster.
- Better for large scale sending.
- You don’t have to deal with MIME because Mailgun will assemble it on their side.
- Request libraries are available for your language of choice.
If you want to track your emails more than 7 days (which is the retainer period for free mailgun) you need to setup webhooks to our mail aggregator service.
When staging and production databases are in sync, users and their emails are also in sync. Sometimes, we trigger an action which could send emails to real users. To prevent this, we should intercept all emails which are sent to real users from staging environment.
We are using recipient_interceptor for emails restriction at staging.
-
Add
recipient_interceptor
gem to gemfile -
In config/environments/staging.rb add gem settings. Example:
Mail.register_interceptor( RecipientInterceptor .new(Rails.application.secrets.permitted_emails, subject_prefix: '[STAGING]') )
-
In secrets.yml add permitted_emails key under the staging section.
-
If you want to set more then one email, then emails should be separated with comma. (Example: 'test1@infinum.co,test2@infinum.co')
If everything is configured, all emails will be delivered at specified emails.
Preview email in the default browser instead of sending it. This is great for two reasons:
- You don't need to set up email delivery in development environment.
- There is no risk of accidentally sending a test email to real user when developing and testing.
We are using letter_opener for previewing emails in development.
-
Add
letter_opener
gem to gemfile(in development group) -
In config/environments/development.rb set delivery method. Example:
config.action_mailer.delivery_method = :letter_opener
Rails also has built in feature for previewing emails.Previewing emails With this approach, it is possible to see how email looks without sending real email.