ActionMailer task in cron

I have a Ruby script that sends an email. It works perfectly well when
run from the command line of a user session, wheter in development or
test mode. When run by cron as the same user however, it fails with
this:

/usr/lib/ruby/1.8/net/smtp.rb:680:in `check_response': 501 5.0.0 HELO
requires domain address (Net::SMTPSyntaxError)
  from /usr/lib/ruby/1.8/net/smtp.rb:653:in `getok'
  from /usr/lib/ruby/1.8/net/smtp.rb:623:in `helo'
  from /usr/lib/ruby/1.8/net/smtp.rb:401:in `do_start'
  from /usr/lib/ruby/1.8/net/smtp.rb:378:in `start'
  from
/usr/lib64/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:681:in
`perform_delivery_smtp'
  from
/usr/lib64/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:523:in
`__send__'
  from
/usr/lib64/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:523:in
`deliver!'
  from
/usr/lib64/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:395:in
`method_missing'
  from
/home/byrnejb/Software/Development/Projects/proforma.git/app/controllers/mail_notice_fx_cacb.rb:6:in
`email_fx'
  from ./bin/../lib/forex/hll_forex_ca_feed.rb:114:in `execute'
  from bin/forex:5

I use a config/email.yml file that is defined thus:

test:
  delivery_method: :smtp
  smtp_settings:
    address: smtp.harte-lyne.ca
    port: 25
    domain: harte-lyne.ca

production:
  delivery_method: :smtp
  smtp_settings:
    address: smtp.harte-lyne.ca
    port: 25
    domain: harte-lyne.ca

development:
  delivery_method: :smtp
  smtp_settings:
    address: smtp.harte-lyne.ca
    port: 25
    domain: harte-lyne.ca

and is loaded via config/initializers/load_email_config.rb which looks
like this:

# Load mail configuration if not in test environment
if RAILS_ENV != 'test'
  email_settings =
YAML::load(File.open("#{RAILS_ROOT}/config/email.yml"))
  ActionMailer::Base.smtp_settings =
    email_settings[RAILS_ENV] unless email_settings[RAILS_ENV].nil?
end

I have placed puts in the initializer file and I can see it being
called, but the puts display after the eerror so it seems to me that
perhaps the email is being sent before the settings are.

For example, if I place puts before the RAILS_ENV and before each
statement within the conditional then I see this:

usr/lib/ruby/1.8/net/smtp.rb:680:in `check_response': 501 5.0.0 HELO
requires domain address (Net::SMTPSyntaxError)
  from /usr/lib/ruby/1.8/net/smtp.rb:653:in `getok'
  from /usr/lib/ruby/1.8/net/smtp.rb:623:in `helo'
  from /usr/lib/ruby/1.8/net/smtp.rb:401:in `do_start'
  from /usr/lib/ruby/1.8/net/smtp.rb:378:in `start'
  from
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:681:in
`perform_delivery_smtp'
  from
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:523:in
`__send__'
  from
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:523:in
`deliver!'
  from
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:395:in
`method_missing'
  from
/var/data/theheart/releases/20090727153549/app/controllers/mail_notice_fx_cacb.rb:6:in
`email_fx'
  from ./bin/../lib/forex/hll_forex_ca_feed.rb:114:in `execute'
  from bin/forex:5
Loading email config
Reading email config
Setting smtp defaults

The script has this in it:

#!/usr/bin/env ruby
require File.dirname(__FILE__) +
'/../lib/forex/hll_commercial_forex_notify'
HLLCommercialForexNotify::Main.new(ARGV).execute

And, the script class does this:
...
  unless defined? RAILS_DEFAULT_LOGGER
    require File.dirname(__FILE__) + '/../../config/environment'
  end
  require 'hll_commercial_forex_rates_notice'
...

Can anybody tell me what I am doing wrong?

James Byrne wrote:

I have a Ruby script that sends an email. It works perfectly well when
run from the command line of a user session, wheter in development or
test mode. When run by cron as the same user however, it fails with
this:

I have discovered that I get the same behaviour as observed in cron in a
session if I set RAILS_ENV='production'.

James Byrne wrote:

I have discovered that I get the same behaviour as observed in cron in a
session if I set RAILS_ENV='production'.

I have uncovered two more things.

1. If the delivery method is set to 'smtp' then delivery fails with teh
error given above.

2. The delivery method must be set in production.rb to have effect. I
am not sure why because the setup that I had worked in test and
development.