# ActionMailer task in cron

**URL:** <https://discuss.rubyonrails.org/t/actionmailer-task-in-cron/44586>\
**Category:** rubyonrails-talk\
**Created:** [July 27, 2009, 5:17pm UTC](https://discuss.rubyonrails.org/t/actionmailer-task-in-cron/44586 "2009-07-27T17:17:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 27, 2009, 5:17pm UTC](https://discuss.rubyonrails.org/t/actionmailer-task-in-cron/44586/1 "2009-07-27T17:17:11Z")

</div>

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) &nbsp;&nbsp;from /usr/lib/ruby/1.8/net/smtp.rb:653:in `getok' &nbsp;&nbsp;from /usr/lib/ruby/1.8/net/smtp.rb:623:in `helo' &nbsp;&nbsp;from /usr/lib/ruby/1.8/net/smtp.rb:401:in `do\_start' &nbsp;&nbsp;from /usr/lib/ruby/1.8/net/smtp.rb:378:in `start' &nbsp;&nbsp;from /usr/lib64/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:681:in `perform\_delivery\_smtp' &nbsp;&nbsp;from /usr/lib64/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action\_mailer/base.rb:523:in ` __send__' &nbsp;&nbsp;from /usr/lib64/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:523:in `deliver!' &nbsp;&nbsp;from /usr/lib64/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action\_mailer/base.rb:395:in `method_missing' &nbsp;&nbsp;from /home/byrnejb/Software/Development/Projects/proforma.git/app/controllers/mail_notice_fx_cacb.rb:6:in `email\_fx' &nbsp;&nbsp;from ./bin/../lib/forex/hll\_forex\_ca\_feed.rb:114:in `execute' &nbsp;&nbsp;from bin/forex:5

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

test: &nbsp;&nbsp;delivery\_method: :smtp &nbsp;&nbsp;smtp\_settings: &nbsp;&nbsp;&nbsp;&nbsp;address: smtp.harte-lyne.ca &nbsp;&nbsp;&nbsp;&nbsp;port: 25 &nbsp;&nbsp;&nbsp;&nbsp;domain: harte-lyne.ca

production: &nbsp;&nbsp;delivery\_method: :smtp &nbsp;&nbsp;smtp\_settings: &nbsp;&nbsp;&nbsp;&nbsp;address: smtp.harte-lyne.ca &nbsp;&nbsp;&nbsp;&nbsp;port: 25 &nbsp;&nbsp;&nbsp;&nbsp;domain: harte-lyne.ca

development: &nbsp;&nbsp;delivery\_method: :smtp &nbsp;&nbsp;smtp\_settings: &nbsp;&nbsp;&nbsp;&nbsp;address: smtp.harte-lyne.ca &nbsp;&nbsp;&nbsp;&nbsp;port: 25 &nbsp;&nbsp;&nbsp;&nbsp;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' &nbsp;&nbsp;email\_settings = YAML::load(File.open("#{RAILS\_ROOT}/config/email.yml")) &nbsp;&nbsp;ActionMailer::Base.smtp\_settings = &nbsp;&nbsp;&nbsp;&nbsp;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) &nbsp;&nbsp;from /usr/lib/ruby/1.8/net/smtp.rb:653:in `getok' &nbsp;&nbsp;from /usr/lib/ruby/1.8/net/smtp.rb:623:in `helo' &nbsp;&nbsp;from /usr/lib/ruby/1.8/net/smtp.rb:401:in `do\_start' &nbsp;&nbsp;from /usr/lib/ruby/1.8/net/smtp.rb:378:in `start' &nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:681:in `perform\_delivery\_smtp' &nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action\_mailer/base.rb:523:in ` __send__' &nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action_mailer/base.rb:523:in `deliver!' &nbsp;&nbsp;from /usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.3/lib/action\_mailer/base.rb:395:in `method_missing' &nbsp;&nbsp;from /var/data/theheart/releases/20090727153549/app/controllers/mail_notice_fx_cacb.rb:6:in `email\_fx' &nbsp;&nbsp;from ./bin/../lib/forex/hll\_forex\_ca\_feed.rb:114:in `execute' &nbsp;&nbsp;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: ... &nbsp;&nbsp;unless defined? RAILS\_DEFAULT\_LOGGER &nbsp;&nbsp;&nbsp;&nbsp;require File.dirname(\_\_FILE\_\_) + '/../../config/environment' &nbsp;&nbsp;end &nbsp;&nbsp;require 'hll\_commercial\_forex\_rates\_notice' ...

Can anybody tell me what I am doing wrong?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 27, 2009, 6:07pm UTC](https://discuss.rubyonrails.org/t/actionmailer-task-in-cron/44586/2 "2009-07-27T18:07:20Z")

</div>

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'.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 27, 2009, 6:24pm UTC](https://discuss.rubyonrails.org/t/actionmailer-task-in-cron/44586/3 "2009-07-27T18:24:52Z")

</div>

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.
