Unable to send Mail using smtp relay, Ruby mail gem - ruby

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?

Related

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 on Windows send email using SMTP library

I have the following code that works perfectly on a Mac but fails on windows.
require 'net/smtp'
opts = {}
opts[:server] ||= 'mycompany.com'
opts[:from] ||= 'user0#mycompany.com'
opts[:from_alias] ||= 'Example'
opts[:subject] ||= "You need to see this"
opts[:body] ||= "Important stuff!"
msg = <<END_OF_MESSAGE
From: #{opts[:from_alias]} <#{opts[:from]}>
To: <user0#mycompany.com>
Subject: #{opts[:subject]}
#{opts[:body]}
END_OF_MESSAGE
Net::SMTP.start(opts[:server]) do |smtp|
smtp.send_message msg, opts[:from], "user0#mycompany.com"
end
With the following error message...
Z:\util>ruby test.rb
C:/Ruby200/lib/ruby/2.0.0/net/smtp.rb:540:in `initialize': Permission denied - c
onnect(2) (Errno::EACCES)
from C:/Ruby200/lib/ruby/2.0.0/net/smtp.rb:540:in `open'
from C:/Ruby200/lib/ruby/2.0.0/net/smtp.rb:540:in `tcp_socket'
from C:/Ruby200/lib/ruby/2.0.0/net/smtp.rb:550:in `block in do_start'
from C:/Ruby200/lib/ruby/2.0.0/timeout.rb:66:in `timeout'
from C:/Ruby200/lib/ruby/2.0.0/net/smtp.rb:549:in `do_start'
from C:/Ruby200/lib/ruby/2.0.0/net/smtp.rb:519:in `start'
from C:/Ruby200/lib/ruby/2.0.0/net/smtp.rb:456:in `start'
from test.rb:19:in `<main>'
Is there something missing that i need on the windows environment to send the email?
Thanks

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)

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.

How to search message in mailbox with net/imap in Ruby?

I have some script on Ruby 1.9.3:
require "net/imap"
imap = Net::IMAP.new(mail_imap_server)
imap.login(mail_login, mail_password)
imap.select("INBOX")
puts imap.search(["FROM", "homer#simpson.com"])
imap.logout
imap.disconnect
If the desired message is present, then all is well.
If the desired message is missing, an error:
/opt/local/lib/ruby1.9/1.9.1/net/imap.rb:1332:in `block in search_internal': undefined method `[]' for nil:NilClass (NoMethodError)
from /opt/local/lib/ruby1.9/1.9.1/monitor.rb:211:in `mon_synchronize'
from /opt/local/lib/ruby1.9/1.9.1/net/imap.rb:1326:in `search_internal'
from /opt/local/lib/ruby1.9/1.9.1/net/imap.rb:752:in `search'
from ./mail.rb:12:in `mail'
from ./mail.rb:26:in `<main>'
How can I solve this problem?
first check if there are messages in the result, use a rescue just in case
require "net/imap"
imap = Net::IMAP.new(mail_imap_server)
imap.login(mail_login, mail_password)
imap.select("INBOX")
begin
messages = imap.search(["FROM", "homer#simpson.com"])
puts messages if messages.length > 0
rescue
puts "Error while retrieving the message"
end
imap.logout
imap.disconnect

Resources