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

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.

Related

Unable to send Mail using smtp relay, Ruby mail gem

I'm trying to send an email using the 'mail' gem (packaging using OCRA), but I'm getting an error message:
=== Loading script to check dependencies
C:/Ruby23/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/check_delivery_params.rb:
13:in `check_from': SMTP From address may not be blank: nil (ArgumentError)
from C:/Ruby23/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/check_delive
ry_params.rb:6:in `check'
from C:/Ruby23/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/network/deli
very_methods/smtp.rb:97:in `deliver!'
from C:/Ruby23/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/message.rb:2
149:in `do_delivery'
from C:/Ruby23/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/message.rb:2
39:in `deliver'
from C:/Ruby23/lib/ruby/gems/2.3.0/gems/mail-2.6.6/lib/mail/mail.rb:141:
in `deliver'
from c:/BacklogAlert.rb:140:in `send_email'
from c:/BacklogAlert.rb:163:in `<top (required)>'
from C:/Ruby23/lib/ruby/gems/2.3.0/gems/ocra-1.3.10/bin/ocra:1211:in `lo
ad'
from C:/Ruby23/lib/ruby/gems/2.3.0/gems/ocra-1.3.10/bin/ocra:1211:in `<t
op (required)>'
from C:/Ruby23/bin/ocra:22:in `load'
from C:/Ruby23/bin/ocra:22:in `<main>'
The problem is that we're using an internal smtp mail relay which only allows devices to send to internal recipients, so the "from" address isn't needed.
Here's my code:
Mail.defaults do
delivery_method :smtp, mail_options
end
Mail.deliver do
to email_recipient
from email_sender
subject 'Test email ' + today_date
body 'Attached is a file'
add_file test_file
end
How do i get the email to send without specifying a "from" address?

ruby mailgun send mail fails with 400 bad request

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.

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.

Debugging mail delivery in IronWorker

I'm trying to use the mail gem to send an email from an IronWorker task. Running the script works on my local system (OS X), but fails when run on IronWorker with the same parameters. I'm new to IronWorker and the mail gem, so I'm at a loss of where to start debugging. Here's the text of the error message.
/task/email.rb:5:in `block in <top (required)>': undefined method `[]' for nil:NilClass (NoMethodError)
from /task/__gems__/gems/mail-2.4.4/lib/mail/mail.rb:106:in `instance_eval'
from /task/__gems__/gems/mail-2.4.4/lib/mail/mail.rb:106:in `defaults'
from /task/email.rb:3:in `<top (required)>'
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from __runner__.rb:213:in `<main>'
And here's my code:
require 'mail'
Mail.defaults do
delivery_method :smtp, {
address: params['address'],
port: 587,
domain: params['domain'],
user_name: params['username'],
password: params['password'],
authentication: 'plain',
enable_starttls_auto: true
}
end
Mail.deliver do
to "#{params['to_name']} <#{params['to_address']}>"
from "#{params['from_name']} <#{params['from_address']}>"
subject "Mail test"
text_part do
body params['text']
end
html_part do
content_type 'text/html; charset=UTF-8'
body params['html']
end
end
Note that I was getting this error before locally, but it was caused by using username in the defaults instead of user_name. Any ideas?
I'm not sure but looks like 'params' is nil, could you try to inspect it before using in. It could happened if you're using in your worker separate namespaces (like worker body inside class)

sending file with pony by sinatra app - missing file

I want to send a email from my sinatra application.
Here is the code:
require 'pony'
class Cms < Application
get "/mail" do
Pony.mail :to => 'to#gmail.com',
:from => "from#gmail.com",
:subject => "Thanks for signing my guestbook!",
:via => :sendmail,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:user_name => 'user#gmail.com',
:pass => 'pass',
:enable_starttls_auto => false
},
:body => erb(:"cms/mail")
redirect '/'
end
end`
Thin is starting application with no errors, but When i request myapp.local/mail i've got an error:
LoadError - no such file to load -- mail/network/delivery_methods/smtp:
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/configuration.rb:31:in lookup_delivery_method'
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/configuration.rb:25:in delivery_method'
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/mail.rb:111:in delivery_method'
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/message.rb:116:in initialize'
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/mail.rb:50:in new'
/var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/mail.rb:50:in new'
/var/lib/gems/1.8/gems/pony-1.4/lib/pony.rb:174:in build_mail'
/var/lib/gems/1.8/gems/pony-1.4/lib/pony.rb:138:in mail'
./app/controllers/cms.rb:8:in GET /mail'
File /var/lib/gems/1.8/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb exists.
I was getting this same error when I was using the inline configuration of the Mail gem:
mail.delivery_method :sendmail
mail.deliver!
Removing that first line, and moving the configuration to immediately following the loading of the mail gem fixed it.
Wherever in your app you require 'mail' just configure it immediately:
require 'mail'
Mail.defaults do
delivery_method :sendmail
end
Update: This worked for awhile... But then for some reason I began seeing this error:
rbenv/versions/1.8.7-p374/lib/ruby/gems/1.8/gems/mail-2.5.4/lib/mail/fields/common/common_address.rb:9:in `parse': no such file to load -- mail/elements/address_list (LoadError)
Update2: The failures happen randomly it seems. Something about the way the autoload works in Ruby 1.8.7-p374 is causing it to not be able to find files that do in fact exist. Also, I am using slimgems not rubygems.
These are the hacks I've had to implement so far to use Mail with multi-part email and sendmail delivery method:
require 'mail'
require 'mail/network/delivery_methods/sendmail'
require 'mail/elements/address_list'
require 'mail/fields/common/common_address'
require 'mail/elements/content_type_element'
require 'mail/elements/address'
require 'mail/elements/content_transfer_encoding_element'
Mail.defaults do
delivery_method :sendmail
end

Resources