ruby mailgun发送邮件失败,有400个错误请求

时间:2016-07-15 05:35:41

标签: ruby mailgun

我正在尝试使用ruby的mailgun API。我做的第一件事是注册一个帐户。我有api_key和沙盒域激活。然后,我将自己的电子邮件添加到沙盒域中的授权收件人。 我在文档中完全赞同:

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

但它总是返回400个错误请求,这里是来自终端的跟踪:

/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>'

我在这里做错了什么?我安装了rest-client gem,所以我认为我的注册中存在一些问题或什么?

2 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,并在此处查看了文档: https://github.com/rest-client/rest-client(在例外部分) 他们用救援包围了RestClient.post。我把它打印出来了:

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

然后我得到了一个错误字符串:

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

然后在我的测试中看到我在from字段中出错:

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

也许您可以使用类似的方法来解决您的问题。

答案 1 :(得分:0)

我们一直在遇到这个问题,这对我们来说是由于人们错误地将电子邮件输入表单引起的。在我经历过的每一次事件中,收件人的地址都写为something@gmail.con...@hotmail.comm,而Mailgun无法验证域名并将其作为无效发送回来。