ruby mailgun send mail fails with 400 bad request - ruby

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?

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.

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.

Related

How to rescue RestClient API calls in development mode when no Internet available?

In my Rails app I am making various API calls like this one:
class SearchFacebook
FALLBACK_STARS = 5.0
def self.call
begin
url = "https://graph.facebook.com/v2.12/#{FACEBOOK_PAGE_ID}"
response = RestClient.get(url, :params => {:access_token => FACEBOOK_ACCESS_TOKEN, :fields => "overall_star_rating"})
json = JSON.parse(response)
stars = json["overall_star_rating"]
rescue RestClient::ExceptionWithResponse => error
stars = FALLBACK_STARS
end
{:stars => stars}
end
end
The problem is that I am running my app in my local environment a lot of the time. When there's no Internet connection available (which happens a lot to me, don't ask why), I get the following error:
SocketError.
Failed to open TCP connection to api.facebook.com:443 (getaddrinfo:
nodename nor servname provided, or not known)
How can I rescue from being offline?
I checked RestClient documentation already but to no avail. Thanks for any help.
Can you use a blanket rescue => e and use byebug to inspect the exception being raised. You could then rescue that error if in a development environment.
There are some other options mentioned here Check if Internet Connection Exists with Ruby?
OK, I solved this by simply rescuing from a SocketError:
rescue SocketError, RestClient::ExceptionWithResponse => error
I found this answer quite helpful.

Ruby ArgumentError when actually providing correct arguments

Ruby complains i am not providing enough arguments to my script, which is:
#!/usr/bin/ruby
require 'mail'
def send(file,recipients_csv)
recipients=recipients_csv.split(",")
recipients.each do |recipient|
Mail.defaults do
delivery_method :smtp,{ :address => 'localhost', :port => 25,:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE}
end
mail = Mail.new do
from 'noreply#mydomain.com'
to "#{recipient}"
subject "test"
body "test"
add_file :filename => "a_testfile.tsv", :content => File.read(file.path)
end
mail.deliver!
end
end
testfile=File.new("newfile.tsv","w")
send(testfile,"name#mydomain.com")
What i get back is:
Mailer.rb:4:in `send': wrong number of arguments (1 for 2) (ArgumentError)
from /usr/lib64/ruby/gems/1.9.1/gems/treetop-1.4.15/lib/treetop/runtime/compiled_parser.rb:18:in `parse'
from /usr/lib64/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/elements/address_list.rb:26:in `initialize'
from /usr/lib64/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/fields/common/common_address.rb:9:in `new'
I dont get this, the arguments i provide are obviously 2
This might be conflicting with the Ruby base send method. Try renaming send to send_mail (or something), to avoid overwriting the send method
This error isn't coming from when you are running the script yourself on line 22, you are clearly passing it two arguments. It is actually coming from one of the three files you see in your error stack.
from /usr/lib64/ruby/gems/1.9.1/gems/treetop-1.4.15/lib/treetop/runtime/compiled_parser.rb:18:in `parse'
from /usr/lib64/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/elements/address_list.rb:26:in `initialize'
from /usr/lib64/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/fields/common/common_address.rb:9:in `new'
If you go into those files send is being called with only one argument as opposed to two.

404 not found, but can access normally from web browser

I tried many URLs on this and they seem to be fine until I came across this particular one:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open("http://www.moxyst.com/fashion/men-clothing/underwear.html"))
puts doc
This is the result:
/Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:353:in `open_http': 404 Not Found (OpenURI::HTTPError)
from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:709:in `buffer_open'
from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:210:in `block in open_loop'
from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:208:in `catch'
from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:208:in `open_loop'
from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:149:in `open_uri'
from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:689:in `open'
from /Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:34:in `open'
from test.rb:5:in `<main>'
I can access this from a web browser, I just don't get it at all.
What is going on, and how can I deal with this kind of error? Can I ignore it and let the rest do their work?
You're getting 404 Not Found (OpenURI::HTTPError), so, if you want to allow your code to continue, rescue for that exception. Something like this should work:
require 'nokogiri'
require 'open-uri'
URLS = %w[
http://www.moxyst.com/fashion/men-clothing/underwear.html
]
URLs.each do |url|
begin
doc = Nokogiri::HTML(open(url))
rescue OpenURI::HTTPError => e
puts "Can't access #{ url }"
puts e.message
puts
next
end
puts doc.to_html
end
You can use more generic exceptions, but then you run into problems getting weird output or might handle an unrelated problem in a way that causes more problems, so you'll need to figure out the granularity you need.
You could even sniff either the HTTPd headers, the status of the response, or look at the exception message if you want even more control and want to do something different for a 401 or a 404.
I can access this from a web browser, I just don't get it at all.
Well, that could be something happening on the server side: Perhaps they don't like the UserAgent string you're sending? The OpenURI documentation shows how to change that header:
Additional header fields can be specified by an optional hash argument.
open("http://www.ruby-lang.org/en/",
"User-Agent" => "Ruby/#{RUBY_VERSION}",
"From" => "foo#bar.invalid",
"Referer" => "http://www.ruby-lang.org/") {|f|
# ...
}
You might need to pass 'User-Agent' as parameter to open method. Some sites require a valid User-Agent otherwise they simply don't respond or show a 404 not found error.
doc = Nokogiri::HTML(open("http://www.moxyst.com/fashion/men-clothing/underwear.html", "User-Agent" => "MyCrawlerName (http://mycrawler-url.com)"))
So what is going on and how can I deal with this kind of error.
No clue what's going on, but you can deal with it by catching the error.
begin
doc = Nokogiri::HTML(open("http://www.moxyst.com/fashion/men-clothing/underwear.html"))
puts doc
rescue => e
puts "I failed: #{e}"
end
Can I just ignore it and let the rest do their work?
Sure! Maybe? Not sure. We don't know your requirements.

Sinatra unit test - post with JSON body

I am trying to build a unit test for a REST API I built using Sinatra. For right now I just want to test that my echo function works right. Echo uses POST and will return the exact same payload from the post. I am still new with ruby, so forgive me if I don't use the proper lingo.
Here is the code I want to test:
post '/echo' do
request.body.read
end
This is the unit test I am trying to make:
ENV['RACK_ENV'] = 'test'
require './rest_server'
require 'test/unit'
require 'rack/test'
require 'json'
class RestServer < Test::Unit::TestCase
def app
Sinatra::Application
end
def test_check_methods
data = '{"dataIn": "hello"}'
response = post '/echo', JSON.parse(data)
assert.last_response.ok?
assert(response.body == data)
end
end
With the above code, here is the error:
NoMethodError: undefined method `dataIn' for Sinatra::Application:Class
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1285:in `block in compile!'
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1285:in `each_pair'
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1285:in `compile!'
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1267:in `route'
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1256:in `post'
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1688:in `block (2 levels) in delegate'
/Users/barrywilliams/RubymineProjects/project/rest_server_test.rb:20:in `test_check_methods'
If I try doing it without the JSON.parse, I get
NoMethodError: undefined method `key?' for "{\"dataIn\": \"hello\"}":String
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1265:in `route'
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1256:in `post'
/Users/barrywilliams/.rvm/gems/ruby-1.9.3-p448/gems/sinatra-1.3.4/lib/sinatra/base.rb:1688:in `block (2 levels) in delegate'
/Users/barrywilliams/RubymineProjects/project/rest_server_test.rb:20:in `test_check_methods'
If I try doing it where data = 'hello', then I get the same undefined method 'key?' error
I've tried this suggestion, with no success:
http://softwareblog.morlok.net/2010/12/18/testing-post-with-racktest/
I get an error saying that post only takes 2 arguments, not 3.
So, in summary, I need to be able to make a call, have the code I'm testing receive the call and return a response, then I need to be able to read that response and verify it was the original data. Right now it looks like it's getting stuck at just making the call.
I did a thing a little similar, it might help you :
The application post definition :
post '/' do
data = JSON.parse request.body.read.to_s
"Hello !\n#{data.to_s}"
end
The .to_s is necessary, else the conversions will not be exactly the same :-/
Then on the test file :
class RootPostTest < Test::Unit::TestCase
include Rack::Test::Methods
def app
Sinatra::Application
end
def test_return_the_parameters
data = {
'reqID' => 1,
'signedReqID' => "plop",
'cert' => "mycert"
}
post '/', data.to_json, "CONTENT_TYPE" => "application/json"
assert last_response.ok?
body_espected = "Hello !\n#{JSON.parse(data.to_json).to_s}"
assert_equal last_response.body, body_espected
end
end
Hope it helped you.
Rack Test will give you back the response body in last_response.body, no need to save it to a variable. You're also not echoing back what you've sent - data in the code you've given is JSON, but you converted it to a hash and posted that, so it's not going to match what comes back. Either send JSON, or convert it to JSON in the Sinatra route if you want to do that (see https://stackoverflow.com/a/12138793/335847 for more).
In the Sinatra app:
require 'json'
post '/echo' do
# Don't use request.body.read as you're not posting JSON
params.to_json
end
and in the test file:
def test_check_methods
data = '{"dataIn": "hello"}'
post '/echo', JSON.parse(data)
assert.last_response.ok?
assert(last_response.body == data)
end
If you do end up wanting to post JSON (which I think is usually not a good idea if it's easy to convert or already have the data as a hash) then use :provides => "json" as a condition to the route, and consider using Rack::Test::Accepts to make life easier writing the test for that (note: that's a shameless plug for a gem I wrote;)

Parsing Email with Ruby: unexpected token ATOM (expected SPACE) (Net::IMAP::ResponseParseError)

I am getting started parsing email with Ruby. I'm trying to read from my GMail account:
require 'rubygems'
require 'mail'
Mail.defaults do
retriever_method :imap, :address => "imap.gmail.com",
:port => 995,
:user_name => 'example#gmail.com',
:password => 'password',
:enable_ssl => true
end
emails = Mail.all
emails.each do |email|
puts email.inspect
end
...but I'm getting this error:
/Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:3277:in `parse_error': unexpected token ATOM (expected SPACE) (Net::IMAP::ResponseParseError)
from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:3129:in `match'
from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:2100:in `continue_req'
from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:2087:in `response'
from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:2015:in `parse'
from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:1166:in `get_response'
from /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/imap.rb:1051:in `initialize'
from /Users/andrew/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/network/retriever_methods/imap.rb:143:in `new'
from /Users/andrew/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/network/retriever_methods/imap.rb:143:in `start'
from /Users/andrew/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/network/retriever_methods/imap.rb:65:in `find'
from /Users/andrew/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/network/retriever_methods/base.rb:41:in `all'
from /Users/andrew/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/mail.rb:171:in `all'
Ruby's IMAP parser has had bugs. Your error output shows that the problem isn't likely to be in your code, it's likely to be the Ruby IMAP parser code that you can't easily change without patching Ruby.
If you're just interested in Gmail, and want to try a easier solution, try the Gmail gem:
https://github.com/nu7hatch/gmail
If you're interested in knowing the details of what's happening and possily how to patch Ruby:
http://claudiofloreani.blogspot.com/2012/01/monkeypatching-ruby-imap-class-to-build.html
This error message is typical if you try to do IMAP on a POP3 mail server.
Google Mail's IMAP port is 993. The port you tried is for POP3.

Resources