Mailgun::CommunicationError via nginx '301 Moved Permanently' error Mailgun::CommunicationError via nginx '301 Moved Permanently' error nginx nginx

Mailgun::CommunicationError via nginx '301 Moved Permanently' error


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.rbActionMailer::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.rbdef 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.