Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailgun Sandbox domain not working

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.

I am using the Mailgun addon with Heroku in a RoR application. I am trying to use the sandbox domain with my personal gmail account added as an authorized user. I am using devise confirmable, so when a new user signs up, a default email should be sent to the email they provided. As seen below, the user is successfully being created and the email to be sent is found. Mailgun seems to recieve the necessary information, but does not believe I am authorized?

Sandbox is active

    SQL (1.1ms)  INSERT INTO "users" ("first_name", "last_name", "email", "encrypted_password", "created_at", "updated_at", "confirmation_token", "confirmation_sent_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["first_name", "C"], ["last_name", "C"], ["email", "[email protected]"], ["encrypted_password", "$2a$10$k9CdjjFd7GzLcKJ.E4VNie27aJrQm3LnigLHlFSFKcd2qR2x11cQW"], ["created_at", "2016-08-01 22:05:30.331566"], ["updated_at", "2016-08-01 22:05:30.331566"], ["confirmation_token", "38f6d620e0e8277957ee85bbede77610cdbea448a79b9d07fc5998fdc3c780d3"], ["confirmation_sent_at", "2016-08-01 22:05:30.565564"]]
2016-08-01T22:05:30.601828+00:00 app[web.1]:   Rendered vendor/bundle/ruby/2.2.0/gems/devise-3.4.1/app/views/devise/mailer/confirmation_instructions.html.erb (9.0ms)
2016-08-01T22:05:30.856455+00:00 app[web.1]: 2016-08-01T22:05:30.856470+00:00 app[web.1]: MyMailer#confirmation_instructions: processed outbound mail in 284.9ms
2016-08-01T22:05:30.978021+00:00 app[web.1]: 
2016-08-01T22:05:30.978023+00:00 app[web.1]: Sent mail to [email protected] (121.4ms)
2016-08-01T22:05:30.978026+00:00 app[web.1]: Date: Mon, 01 Aug 2016 22:05:30 +0000
2016-08-01T22:05:30.978027+00:00 app[web.1]: From: [email protected]
2016-08-01T22:05:30.978028+00:00 app[web.1]: To: [email protected]
2016-08-01T22:05:30.978028+00:00 app[web.1]: Message-ID: <579fc7aad20f8_33f9a82512758405f4@e15a6ab3-869e-4811-9cde-20f13e3e0db4.mail>
2016-08-01T22:05:30.978029+00:00 app[web.1]: Subject: Confirmation instructions
2016-08-01T22:05:30.978029+00:00 app[web.1]: Mime-Version: 1.0
2016-08-01T22:05:30.978030+00:00 app[web.1]: Content-Type: text/html;
2016-08-01T22:05:30.978030+00:00 app[web.1]:  charset=UTF-8
2016-08-01T22:05:30.978030+00:00 app[web.1]: Content-Transfer-Encoding: 7bit
2016-08-01T22:05:30.978031+00:00 app[web.1]: 
2016-08-01T22:05:30.978031+00:00 app[web.1]: <p>Welcome [email protected]!</p>
2016-08-01T22:05:30.978032+00:00 app[web.1]: 
2016-08-01T22:05:30.978034+00:00 app[web.1]: <p>You can confirm your account email through the link below:</p>
2016-08-01T22:05:30.978034+00:00 app[web.1]: 
2016-08-01T22:05:30.978035+00:00 app[web.1]: <p><a href="http://peeraccomplish.heroku.com/users/confirmation?confirmation_token=zry54mp6sBgdFdfcjPYP">Confirm my account</a></p>
2016-08-01T22:05:30.978036+00:00 app[web.1]: 
2016-08-01T22:05:30.979055+00:00 app[web.1]:    (0.9ms)  ROLLBACK
2016-08-01T22:05:30.981993+00:00 app[web.1]: Completed 500 Internal Server Error in 753ms
2016-08-01T22:05:30.983041+00:00 app[web.1]: 
2016-08-01T22:05:30.983042+00:00 app[web.1]: ):
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.

I used the directions on using Mailgun for Heroku - (copied and pasted into my config/production.rb):

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         => ENV['MAILGUN_DOMAIN'],
    :authentication => :plain,
  }
  ActionMailer::Base.delivery_method = :smtp

I have checked the environment variables and they all appear accurate. I also checked port 587 is working using telnet on heroku.

Any ideas where to go from here?

mailers/my_mailer.rb for reference:

class MyMailer < Devise::Mailer   
  helper :application # gives access to all helpers defined within `application_helper`.
  include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
  default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views

  default from: '[email protected]'
end
like image 463
Erik V Avatar asked Aug 02 '16 17:08

Erik V


People also ask

How do I enable domain in Mailgun?

Configure your Domain for Email In Mailgun, navigate to Sending > Domains click Add New Domain. Add your domain and follow the setup. You will be given two TXT (SPF and DKIM), two MX, and a CNAME record. Update these records in your DNS provider.

Can I use SMTP to interact with sandbox?

The sandbox domain clearly has some compelling advantages: It's available straightaway and very easy to use. It's free of charge. Can be used via both API and SMTP.

How do I link my domain to my Mailgun?

Adding A Domain First, log in to the Mailgun Control Panel (if you have not already done so). Then, within the left-hand navigation pane, click the Sending option to expand its list of suboptions. Next, click the Domains suboption. Click the Add New Domain button.

How many Domains can you have with Mailgun?

You can create up to 1,000 domains on a paid plan in the Control Panel or through the :ref:`Domains API <api-domains>`(Free accounts do not include the ability to create a custom domain).


2 Answers

I had this issue too. The problem is, when using the mailgun sandbox domain, you have to pre-register your recipient e-mail in order to send emails to it. In order to do this, go to your app mailgun dashboard, click in "authorized recipients", like the image below.

Where to go to add an authorized recipient

like image 97
Rodrigo Chaves Avatar answered Oct 04 '22 20:10

Rodrigo Chaves


I believe this was an issue on Mailgun's end. I confirmed that I had an authorized user associated with the Sandbox domain. Having no luck with the Sandbox domain, I registered a domain, and updated the config vars in the Heroku app settings, and with no changes to the code above, got emails sending.

like image 37
Erik V Avatar answered Oct 04 '22 19:10

Erik V