# ActionMailer task in cron

**URL:** <https://www.ruby-forum.com/t/actionmailer-task-in-cron/172764>\
**Category:** Rails\
**Created:** [July 27, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/actionmailer-task-in-cron/172764 "2009-07-27T19:17:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [July 27, 2009, 7:17pm UTC](https://www.ruby-forum.com/t/actionmailer-task-in-cron/172764/1 "2009-07-27T19: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) 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?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [July 27, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/actionmailer-task-in-cron/172764/2 "2009-07-27T20:07:20Z")

</div>

James B. 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:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [July 27, 2009, 8:24pm UTC](https://www.ruby-forum.com/t/actionmailer-task-in-cron/172764/3 "2009-07-27T20:24:52Z")

</div>

James B. 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.
