Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Mailgun Email Setup

Tim Morgan edited this page Mar 1, 2017 · 9 revisions

Following are instructions for setting up a Mailgun account for incoming and outgoing email with OneBody. Mailgun offers a free tier for up to 10,000 email messages per month.

For the instructions below, we're going to assume your OneBody domain is members.mychurch.com. Just replace that with your own domain everywhere below.

In the Mailgun web interface:

  1. Create a domain. Make sure it matches exactly the domain you use for your OneBody website, e.g. if your OneBody site is members.mychurch.com, then your Mailgun email domain should also be members.mychurch.com. If you want to live dangerously, you can set it differently, but you'll need to set your OneBody email_host to match.

  2. After creating a domain in Mailgun go through the steps that mailgun provides in order to Add Sending & Tracking DNS Records. You can add all three types (TXT, CNAME, MX). Here are some instructions for setting up DNS on DigitalOcean.

  3. Create a priority 0 "catchall" route in Mailgun (this is in the routes section of Mailgun) that forwards to http://members.mychurch.com/emails.mime. The final route should look like this:

     0 	catch_all() 	forward("http://members.mychurch.com/emails.mime")
    
  4. Under the "Domain information" section, take note of your SMTP Login and Password information.

On your OneBody server:

  1. Create a copy of the mailgun example file (note: if you installed OneBody from a debian package this file already exists at /etc/onebody/email.yml and you can skip this step):

     cp config/email.yml{.mailgun-example,}
    
  2. Edit your email.yml config file to look like this (replace YOUR_SMTP_USERNAME and YOUR_SMTP_PASSWORD below with the details you got in step 3 above; also remember to change the "members.mychurch.com" to your own domain name):

     production:
       smtp:
         address: "smtp.mailgun.org"
         domain: "members.mychurch.com"
         port: 587
         user_name: "YOUR_SMTP_USERNAME"
         password: "YOUR_SMTP_PASSWORD"
         authentication: plain
    
  3. Restart your server.

    • If you installed via the Debian package, run service onebody restart.

    • If you installed manually (or if that command doesn't work), run cd /var/www/onebody && touch tmp/restart.txt.

  4. Send email both ways (an incoming message from an outside address to a OneBody Group address; and an outgoing message from the website) to test that everything is working.

Troubleshooting

  • For some new accounts, DigitalOcean is blocking SMTP ports. File a support ticket to get them to lift that block.

  • Use the Mailgun "Logs" tab to see if there are any error messages.

  • Ensure your email.yml config file is formatted properly. Indentation should use two spaces (no tabs).

  • Make sure to restart your server for the changes to take effect.

  • I just can't get it to work!

    Try this in a terminal on your server:

     ⚡ gem install pony
     ⚡ irb -r pony
     irb(main):001:0> Pony.mail(to: 'YOUR_EMAIL', from: 'YOUR_EMAIL', subject: 'test', body: 'hello', via: :smtp, via_options: { address: 'smtp.mailgun.org', port: 587, user_name: 'MG_USER_NAME', password: 'MG_PASSWORD', domain: 'members.factoledo.org', authentication: :plain })
     => #<Mail::Message:20807040>
    

    If the email is sent, then something is probably wrong with your email.yml config. Check that.

    If the email fails, check your user name, password, and sending domain.