2

I've been trying to set up the mailgun API with my rails app (in development) and I'm using the mailgun_rails gem, but when trying to send a message the Action Controller gives me an error:

JSON::ParserError in UsersController#create

765: unexpected token at 'Mailgun Magnificent API'

The extracted source is as follows:

@user = User.create(user_params)
if @user.save
  UserMailer.account_activation(@user).deliver_now
  flash[:info] = "Please check your email to activate your account."
  redirect_to root_url

my development.rb has the following lines concerning the action mailer:

config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :mailgun

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true

config.action_mailer.mailgun_settings = {
  api_key: ENV['MAILGUN_API_KEY'],
  domain: ENV['MAILGUN_DOMAIN']
}

config.action_mailer.perform_caching = false

The environments in my .rbenv-vars file look like this:

MAILGUN_API_KEY=key-<<KEY>>
MAILGUN_DOMAIN=mg.<<MYDOMAIN>>.nl

All Domain verifications & DNS for mailgun have been completed (and recognized my mailgun itself).

Could anyone please help me figure out what's going wrong?

EDIT

I forgot to include my terminal trace:

Started POST "/signup" for 127.0.0.1 at 2017-09-11 22:03:58 +0200
  ActiveRecord::SchemaMigration Load (0.3ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"<<TOKEN>>", "user"=>{"name"=>"Mailtest", "email"=>"<<EMAIL>>", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.2ms)  BEGIN
  User Exists (0.3ms)  SELECT  1 AS one FROM `users` WHERE `users`.`email` = '<<EMAIL>>' LIMIT 1
  SQL (0.3ms)  INSERT INTO `users` (`email`, `name`, `created_at`, `updated_at`, `password_digest`, `activation_digest`) VALUES ('<<EMAIL>>', 'Mailtest', '2017-09-11 20:03:59', '2017-09-11 20:03:59', '<<TOKEN>>', '<<TOKEN>>')
   (1.9ms)  COMMIT
   (0.2ms)  BEGIN
  User Exists (0.3ms)  SELECT  1 AS one FROM `users` WHERE `users`.`email` = '<<EMAIL>>' AND (`users`.`id` != 17) LIMIT 1
   (0.2ms)  COMMIT
  Rendering user_mailer/account_activation.html.erb within layouts/mailer
  Rendered user_mailer/account_activation.html.erb within layouts/mailer (5.6ms)
  Rendering user_mailer/account_activation.text.erb within layouts/mailer
  Rendered user_mailer/account_activation.text.erb within layouts/mailer (0.6ms)
UserMailer#account_activation: processed outbound mail in 222.3ms
Sent mail to <<EMAIL>> (1149.7ms)
Date: Mon, 11 Sep 2017 22:03:59 +0200
From: noreply@<<DOMAIN>>.nl
To: <<EMAIL>>
Message-ID: <<ID>>
Subject: Account activation
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_59b6ec2f9d9da_4dd63ff8ffd4f034650e4";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_59b6ec2f9d9da_4dd63ff8ffd4f034650e4
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Hi Mailtest,

Welcome to the <<DOMAIN>>! Click on the link below to activate your account:

<<ACTIVATION LINK>>


----==_mimepart_59b6ec2f9d9da_4dd63ff8ffd4f034650e4
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <h1><<DOMAIN>></h1>

<p>Hi Mailtest,</p>

<p>
Welcome to the <<DOMAIN>>! Click on the link below to activate your account:
</p>

<a href="<<ACTIVATION LINK>>">Activate</a>

  </body>
</html>

----==_mimepart_59b6ec2f9d9da_4dd63ff8ffd4f034650e4--

Completed 500 Internal Server Error in 1589ms (ActiveRecord: 5.4ms)


  
JSON::ParserError (765: unexpected token at 'Mailgun Magnificent API')
Finn
  • 21
  • 6
  • There's a similar issue at https://github.com/jorgemanrubia/mailgun_rails/issues/77. You need to see the exact reply from mailgun to understand what's wrong. If you're using a recent version of rails the webconsole should popup and you could trace back the response from mailgun – Cristian Bica Sep 11 '17 at 20:44
  • 1
    "Mailgun Magnificent API" is Mailgun's way of saying "your sender domain and auth are all good, but I can't figure out what API you're asking for". One way this might happen is if `MAILGUN_DOMAIN=mg.<>.nl` resolves to something with slashes in it. [More on "Mailgun Magnificent API"](https://stackoverflow.com/a/54774987/647002). – medmunds Feb 19 '19 at 21:15

0 Answers0