2

I have a Ruby web app that sends email via Mailgun.

My Mailgun account & gem are properly set up and I can send emails manually (via curl, for instance).

The API key and the API base URL (https sandbox domain) are stored in environment variables.

When I attempt to send emails from the app like this:

  def initialize(mailer: nil)
    @mailer = mailer || Mailgun::Client.new(ENV['MAILGUN_API_KEY'])
  end

then:

  def call(user)
    mailer.send_message(ENV['MAILGUN_SANDBOX'], {from: '...',
        to: user.email,
        subject: '...',
        text: "..."})
  end

When I run the app with Sinatra via localhost:xxxx, I get a Mailgun::CommunicationError at /.../... 301 Moved Permanently: ... nginx pointing to this line:

mailer.send_message(ENV['MAILGUN_SANDBOX'], ...

Any idea why that happens? I've researched the issue for hours but couldn't find a clue on what to do next.

Thanks!

sliute
  • 101
  • 5

1 Answers1

0

I ran into this same issue. If you have already fixed this then hopefully this can help someone else.

I switched over to message builder for ease of use and being able to render my html but I'm pretty sure it will still send with the format you have setup with :text

When I switched over to the proper domain in the .env file I believe it solved my issue. You'll need 2 different domains to use Mailgun. The first is the full domain for your sandbox. ENV['MAILGUN_DOMAIN'] it is the sandbox domain with the full https://api.mailgun.net/v3/sandboxXXXXxxxXXXXXX.mailgun.org to send most of the mail formats.

You'll also need the last half of the full domain to send messages. That's just the sandboxXXXXxxxXXXXXX.mailgun.org which is passed into the MessageBuilder or other message .send_message method. When I had them mixed up or both the same I kept on getting this error. When I switched over to separate the two in my development.rb and some_mailer.rb is when I could send the mail without a problem.

Below is my file setup, for reference. I'm pretty new to all of this but this is how I'm setup and it's working for me so hopefully it helps.

# .env 
MAILGUN_DOMAIN='https://api.mailgun.net/v3/sandboxXXXXxxxXXXXXX.mailgun.org'
MAILGUN_SEND_DOMAIN='sandboxXXXXxxxXXXXXX.mailgun.org'

# development.rb
ActionMailer::Base.smtp_settings = {
  :authentication => :plain,
  :address => "smtp.mailgun.org",
  :port => 587,
  :domain => "ENV['MAILGUN_DOMAIN']",
  :user_name => "ENV['MAILGUN_USERNAME']",
  :password => "ENV['MAILGUN_PASSWORD']"
}


# some_mailer.rb
def some_mail_notification(user)
  @user = user

  mg_client = Mailgun::Client.new ENV['MAILGUN_KEY']
  mb_obj = Mailgun::MessageBuilder.new
  mb_obj.from "email@testing.com", {'first' => 'Customer', 'last' => 'Support'}
  mb_obj.add_recipient :to, @user.email, { 'first' => @user.first_name, 'last' => @user.last_name }
  mb_obj.subject "Your Recent Purchase on Some Site"
  mb_obj.body_html ("#{render 'some_mail_notification.html.erb'}")
  mg_client.send_message("sandboxXXXXxxxXXXXXX.mailgun.org", mb_obj)
end

I left the send_message above to the sandbox domain but you can set that as an environment variable in the .env file.

Lost Soul
  • 15
  • 4