Debugging mail delivery in IronWorker - ruby

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)

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?

How to define Ruby Test::Unit testcase with `must`

I had a test case of the following form:
require 'test/unit'
class SomeTests < Test::Unit::TestCase
def test_should_do_action
assert true
end
end
and have re-written it using must as suggested in the book A Test::Unit Trick to Know About:
require 'test/unit'
class SomeTests < Test::Unit::TestCase
must "do action" do
assert true
end
end
And when I run it, I get an undefined method 'must' error shown as follows:
SomeTests.rb:3:in `<class:SomeTests>': undefined method `must' for SomeTests:Class (NoMethodError) from
SomeTests.rb:2:in `<top (required)>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:9:in `each' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:9:in `block in <main>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:4:in `select' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:4:in `<main>' rake aborted! Command failed with status (1): [ruby -w -I"lib" -I"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0" "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb" "test/**/*Tests.rb" ]
Tasks: TOP => default => test (See full trace by running task with --trace)
I thought that must might be a part of minitest, so I required 'minitest/unit' instead, but I still get an error. I also assume that must keyword isn't part of rspec, which I'm not using yet.
How do I get this to work properly?
It looks like that method is not provided out of the box, but was developed by a third party. You need to add code described here.

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

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.

Sinatra, Mongoid, Heroku, MongoHQ: connecting to Mongodb

Trying to get Mongoid up and running with Sinatra on Heroku (MongoHQ). Previous experience with Rails but first time with the stack and Sinatra.
Started with one of the simple examples on the web (app.rb):
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'mongoid'
configure do
Mongoid.load!('mongoid.yml')
Mongoid.configure do |config|
if ENV['MONGOHQ_URL']
conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
uri = URI.parse(ENV['MONGOHQ_URL'])
# problem happens here
config.master = conn.db(uri.path.gsub(/^\//, ''))
else
config.master = Mongo::Connection.from_uri("mongodb://localhost:27017").db('test')
end
end
end
# Models
class Counter
include Mongoid::Document
field :count, :type => Integer
def self.increment
c = first || new({:count => 0})
c.inc(:count, 1)
c.save
c.count
end
end
# Controllers
get '/' do
"Hello visitor n" + Counter.increment.to_s
end
For reference, mongoid.yml looks like:
development:
sessions:
default:
database: localhost
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
As per app.rb (# problem happens here), my logs say:
/app/app.rb:15:in `block (2 levels) in <top (required)>': undefined method `master=' for Mongoid::Config:Module (NoMethodError)
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.3/lib/mongoid.rb:112:in `configure'
from /app/app.rb:11:in `block in <top (required)>'
from /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1273:in `configure'
from /app/app.rb:8:in `<top (required)>'
I have also tried variants, including:
config.master = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXX')
Mongoid.database = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXXXX')
But get the same error:
undefined method `master` for Mongoid::Config:Module (NoMethodError)
or:
undefined method `database=` for Mongoid::Config:Module (NoMethodError)
What am I missing?
Shouldn't be
configure do
Mongoid.load!('mongoid.yml')
end
enough?
That's what the mongid docs are saying. The MONGOHQ_URL environment variable already contains every information to initialize the connection to the db.
So was using Mongoid 3.x ... which:
Doesn't use 10gen driver: uses Moped
Doesn't use config.master
The canonical sample code above which is all over the web works out of the box with Mongoid 2.x so have dropped back to that for the time being.
Thanks!

Resources