8

I'm trying out mailgun API with ruby. First thing I did was register an account. I have the api_key and the sandbox domain active. I then add my own email to authorized recipients from the sandbox domain. I did exactly like in the docs:

def send_simple_message
  RestClient.post "https://api:key-mykey"\
  "@api.mailgun.net/v3/sandboxe5148e9bfa2d4e99a1b02d237a8546fe.mailgun.org/messages",
  :from => "Excited User <postmaster@sandboxe5148e9bfa2d4e99a1b02d237a8546fe.mailgun.org>",
  :to => "my@email.com, postmaster@sandboxe5148e9bfa2d4e99a1b02d237a8546fe.mailgun.org",
  :subject => "Hello",
  :text => "Testing some Mailgun awesomness!",
  :multipart => true
end

send_simple_message

But it always returns 400 bad request, here's the trace from the terminal:

/home/ys/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rest-client-2.0.0/lib/restclient/abstract_response.rb:223:in `exception_with_response': 400 Bad Request (RestClient::BadRequest)
    from /home/ys/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rest-client-2.0.0/lib/restclient/abstract_response.rb:103:in `return!'
    from /home/ys/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rest-client-2.0.0/lib/restclient/request.rb:860:in `process_result'
    from /home/ys/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rest-client-2.0.0/lib/restclient/request.rb:776:in `block in transmit'
    from /home/ys/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/http.rb:853:in `start'
    from /home/ys/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rest-client-2.0.0/lib/restclient/request.rb:766:in `transmit'
    from /home/ys/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rest-client-2.0.0/lib/restclient/request.rb:215:in `execute'
    from /home/ys/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rest-client-2.0.0/lib/restclient/request.rb:52:in `execute'
    from /home/ys/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rest-client-2.0.0/lib/restclient.rb:71:in `post'
    from mailgunner.rb:24:in `send_simple_message'
    from mailgunner.rb:33:in `<main>'

What did I do wrong here? I installed rest-client gem so I think there's some problems in my registration or something?

fatg
  • 519
  • 7
  • 23
  • I hope you got your issue resolved. In my case I had to add `recipient email` to Authorized recipient section as I was using sandbox account. – Abhinay Nov 17 '16 at 10:45
  • @Abhinay yup, I resolved the issue but not sure how... it's so long ago – fatg Nov 28 '16 at 04:42

2 Answers2

4

I had a similar problem and saw the documentation here: https://github.com/rest-client/rest-client (in the exceptions section) where they surrounded the RestClient.post with a rescue. And I made it print:

 def send_simple_message
   begin
     RestClient.post ...
   rescue RestClient::ExceptionWithResponse => e
     puts e.response
   end
 end

then I got an error string with this:

{"message": "'from' parameter is not a valid address. please check documentation"}

then saw that in my test I had an error in the from field:

:from => "Test <alert@mg.example.com", # missing '>' at the end 

Maybe you can use a similar approach, to solve your problem.

João Reis
  • 423
  • 4
  • 11
2

We've been experiencing this issue, which for us is caused by people entering their email incorrectly into a form. In every occurrence I've combed through, the recipient's address is written as something@gmail.con or ...@hotmail.comm where Mailgun can't validate the domain and sends it back as invalid.

Jesse Novotny
  • 704
  • 7
  • 16