Verified email not sending through Heroku/Mailgun
Asked Answered
M

5

11

I have Rails app on Heroku. It has a custom domain, and I've tried to set up email sending through Mailgun. I've installed Mailgun as an add-on through Heroku, and I've gone through the steps Mailgun gives to "verify" my custom domain. If I run Mailgun's "Check DNS Records Now" everything comes back green and the status is "Active." I can even send messages from my custom domain with the curl call they provide. However, when I try to send an email from my Rails app using ActionMailer I get: Net::SMTPFatalError (554 Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authorized recipients in domain settings.

Why does it think I'm using a "Sandbox subdomain"? Here's what I have in environments/production.rb:

  # Mailgun
  ActionMailer::Base.smtp_settings = {                                    
    port: ENV['MAILGUN_SMTP_PORT'],                                       
    address: ENV['MAILGUN_SMTP_SERVER'],                                  
    user_name: ENV['MAILGUN_SMTP_LOGIN'],
    password: ENV['MAILGUN_SMTP_PASSWORD'],                               
    domain: 'my-custom-domain.com',                                               
    authentication: :plain,                                               
  } 
  ActionMailer::Base.delivery_method = :smtp                              
  # Devise recoverable      
  config.action_mailer.default_url_options = { host: 'my-custom-domain.com' } 

For development I'm using Gmail so I know it's reading the right config file. And all the env vars are set correctly. The from is set correctly as well, I see it my logs ([email protected]) What did I miss? Is there something that could still be propagating even through the status is active?

Thanks!

Monocle answered 30/5, 2016 at 22:57 Comment(2)
Did you add the recipients of your e-mail to "Authorized Recipients" list ?Oblast
I had the exact same issue - I did what you mention below, changed the heroku env vars to point to the new credentials, but still the mail doesn't get sent. Strangely, the "resend confirmation instructions" chain works fine. Any idea how to fix this?Fourfold
M
12

So the issue turned out to be that when I verified my custom domain it created a second domain under my Heroku/Mailgun account. I still had the credentials from the xxx.mailgun.org (sandbox) domain in my Heroku env vars. Once I replaced them with the credentials from my custom domain everything worked. (Since Heroku set the first set of env vars I foolishly assumed the new set would get put in automatically.) Sigh...

Thanks for your help lyen.

Monocle answered 31/5, 2016 at 16:5 Comment(3)
Mailgun / Heroku really ought to provide better instructions!Overcritical
Can this be added using Node.js instead of ruby?Zacheryzack
unfortunately even after (re)setting environment variables for verified custom domain it still does not work form me on heroku... it works great in development mode (when i run from localhost with only MAILGUN_API_KEY and MAILGUN_DOMAIN it sends emails). I have checked Authorized Recipients is empty as well, all 'Domain Verification and DNS` checkboxes are green. Any ideas what other setup could go wrong?Household
G
8

To add on to lostphilosopher answer, I recently solved this MailGun custom domain issue myself.

Here's what worked for me:

Situation: After successfully adding a custom domain to Mailgun, my mail sending/receiving stopped working

Errors: After adding custom domain and verifying DNS with Mailgun, I received error messages such as:

Net::SMTPFatalError: 554 Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authorized recipients in Account Settings.

Solution: Realize that Heroku does NOT automatically update configuration variables for you. You must manually update them with values for your new domain.

  1. In MailGun - Find your new MailGun domain settings under under https://mailgun.com/app/domains
  2. In Heroku - goto: Heroku > App > Settings > Reveal Config Vars
  3. In Heroku - Update the following config vars
    • MAILGUN_DOMAIN
    • MAILGUN_SMTP_LOGIN
    • MAILGUN_SMTP_PASSWORD

Update Heroku Config Vars With New MailGun Domain Settings

And yes I agree - MailGun docs are not the most thorough. Especially when it comes to testing your setup.

Grind answered 8/1, 2017 at 21:26 Comment(1)
what goes into mailgun_domainHypotension
O
4

Did you add the recipients of your e-mail to "Authorized Recipients" list under https://mailgun.com/app/domains/sandbox_your_domain.mailgun.org?

enter image description here

Be sure to add a real e-mail address which can receive e-mails.

This is because mailgun would send a confirmation e-mail asking if the owner of the e-mail address really wants to receive e-mails from mailgun.

Oblast answered 31/5, 2016 at 8:39 Comment(1)
I want to use Mailgun to send production messages. I don't want to be in sandbox mode. How do I get out of it? I was able to send a message via curl without "authorizing" those recipients. How do I do that?Monocle
H
1
  1. Make sure you have verified domain set (mailgun -> domains -> verified domains & dns - all green).
  2. Make sure you updated env variables in Heroku to work with verified domain (heroku -> settings -> reveal config vars) should have fields: MAILGUN_API_KEY, MAILGUN_DOMAIN, MAILGUN_SMTP_PASSWORD, MAILGUN_SMTP_LOGIN, MAILGUN_SMTP_SERVER, MAILGUN_SMTP_PORT with proper values.
  3. Make sure you have cleared the list of authorized recipients (mailgun -> domains -> authorized reciepients) - so everyone can receive your emails.
  4. Make sure you have cleared the list of IP whitelist (mailgun -> account overview -> account settings -> security -> IP whitelist) or included ip of your heroku app. In my case the only ip there was my localhost ip (for dev) and it was blocking heroku deployed app for sending emails.
Household answered 21/3, 2018 at 18:5 Comment(0)
B
0

Update sorry about this, but the instructions below show how to send mail using sendgrid but still in sandbox mode. I'll update these steps if I figure out how to get out of sandbox mode.


I had to wait 10 minutes and it all randomly started working (and the sandbox error went away) - I did not have to change config variables in heroku nor did I have to log in to the sendgrid website.

Here's everything to do from the very start to finish:

4 steps to setup mailgun heroku addon

Step 1

Run heroku addons:create mailgun:starter (as per heroku mailgun docs)

Step 2

Add these configs to production.rb (replacing https://www.example.com with your real website url and yourapp with your heroku app url - find it with heroku info):

# Note: no trailing slash on host url
config.action_mailer.default_url_options = { :host => 'https://www.example.com' } 

# From the heroku docs:
# https://devcenter.heroku.com/articles/mailgun#sending-emails-via-smtp

ActionMailer::Base.smtp_settings = {
  :port           => ENV['MAILGUN_SMTP_PORT'],
  :address        => ENV['MAILGUN_SMTP_SERVER'],
  :user_name      => ENV['MAILGUN_SMTP_LOGIN'],
  :password       => ENV['MAILGUN_SMTP_PASSWORD'],
  :domain         => 'yourapp.herokuapp.com',
  :authentication => :plain,
}
ActionMailer::Base.delivery_method = :smtp

Step 3

In the browser, go to your heroku dashboard, select your app -> Resources -> Mailgun (I'm not sure if this step is necessary, but I did it so I include it here - perhaps someone can confirm if it's necessary)

Step 4

Wait about 10-20 minutes.1

1 Mine errored with for 10 minutes or so, then started working without me doing anything other than waiting:

Net::SMTPUnknownError (could not get 3xx (421: 421 Domain
sandboxsldkjfsldkjfsldkjflkjsdfa8.mailgun.org is not allowed to send:
Sandbox subdomains are for test purposes only. Please add your own domain
or add the address to authorized recipients in Account Settings.


Bonus

Here's a very easy way to test emails from the rails console. Go to the rails console with

heroku run rails console

then drop this into the console (replacing the from and to fields):

class TestMailer < ApplicationMailer
  default from: '[email protected]'

  def test_email
    mail(to: '[email protected]', 
         subject: 'Test Email from Rails Console', 
         body: 'This is a test email.')
  end
end

TestMailer.test_email.deliver_now
Burgeon answered 10/8, 2023 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.