ActionMailer cannot use SMTP for email delivery

RoR aficionados,

I’ve configured ActionMailer for SMTP delivery of emails, and have
configured “config/environment.rb” accordingly, including the address of
our (external, but fully operational - checked and double-checked :slight_smile:
SMTP server.

Sending out emails from my Windows development laptop works without a
hitch, but on a Linux staging server using the exact same source code
with the exact same config files it doesn’t work; the symptoms are as
follows…

If I put the name of the SMTP server in “config/environment.rb” I get
the following SocketError on the Linux box:

getaddrinfo: Temporary failure in name resolution
/usr/lib/ruby/1.8/net/protocol.rb:206:in `initialize'
...

If I put the SMTP’s IP address in “config/environment.rb” instead of
it’s name, it doesn’t work either, but this time with a Errno::EACCES
instead of the SocketError (again on the Linux box):

 Permission denied - socket(2)
 /usr/lib/ruby/1.8/net/protocol.rb:206:in `initialize'
 ...

Which is very strange, because if I run the following snippets on the
same Linux box, I don’t get any errors whatsoever:

  ruby -rsocket -e 'p Socket.getaddrinfo("NAME_OF_SMTP", "smtp")'
  ruby -rsocket -e 'p Socket.getaddrinfo("IP_OF_SMTP", "smtp")'

So it doesn’t appear to be a firewall, proxy or other configuration
issue on the box that’s causing the problem.

Also, a small Ruby program that invokes “TCPsocket.new” on port 25 of
the SMTP server works correctly on the Linux box, and another small Ruby
program that invokes “Net::SMTP.start” directly (ie. outside of Rails)
to deliver an email via the same SMTP server correctly delivers the
email from that Linux box… it’s just Rails that seems to be unable to
deliver the email via SMTP.

So it doesn’t appear to be a Rails configuration issue (as the config
works on my Windows laptop) but at the same time it also doesn’t appear
to be a configuration issue of the Linux box (as Ruby outside of Rails
can correctly deliver email via SMTP) ???

I’ve gone through the stacktrace (attached below) with a fine comb, and
“TCPsocket.new” and “Net::SMTP.start” seem to be the most relevant
things to check (which I’ve successfully done, as mentioned above).

An earlier post on this mailing list seems to describe the same (or - at
least - sufficiently similar) issue:

http://lists.rubyonrails.org/pipermail/rails/2006-January/013967.html

but it appears no-one ever posted a follow-up.

Many, many thanks in advance for any help (fix, further pointers) you
can offer.

Peter Vandenberk

PS - full Rails stacktrace (Rails 1.1.6, ActionMailer 1.2.5)

/usr/lib/ruby/1.8/net/protocol.rb:206:in initialize' /usr/lib/ruby/1.8/net/protocol.rb:206:inold_open’
/usr/lib/ruby/1.8/timeout.rb:56:in timeout' /usr/lib/ruby/1.8/timeout.rb:76:intimeout’
/usr/lib/ruby/1.8/net/protocol.rb:206:in old_open' /usr/lib/ruby/1.8/net/smtp.rb:393:indo_start’
/usr/lib/ruby/1.8/net/smtp.rb:378:in start' /usr/lib/ruby/1.8/net/smtp.rb:316:instart’
/usr/lib/ruby/gems/1.8/gems/actionmailer-1.2.5/lib/action_mailer/base.rb:511:in
perform_delivery_smtp' /usr/lib/ruby/gems/1.8/gems/actionmailer-1.2.5/lib/action_mailer/base.rb:397:indeliver!’
/usr/lib/ruby/gems/1.8/gems/actionmailer-1.2.5/lib/action_mailer/base.rb:322:in
`deliver’

For future reference, in case other people run into the same issue: as
it turns out, the SMTP problem I reported earlier was caused by the fact
that I was running my Rails application via Apache/CGI on a SELinux
box… the default SELinux policy disallows HTTPD scripts/modules from
connecting to the network, hence the error messages I was getting: SMTP
from WebRICK, “script/runner” etc. was working, but was failing from
Apache.

Changing the “Allow HTTP scripts and modules to connect to the network”
setting in the SELinux policy solved the SMTP problem I was experiencing
in my Rails app.

Doing so probably opened up a whole can of security issues, but at least
email is now working! :slight_smile: Luckily the CGI thing is just a temporary
setup… phew!

FYI,

Peter

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs