Trouble with email delivery

Over the past few days I’ve been going crazy trying to get email
delivery to work using RoR. From what I’ve read, settings look right,
I have no clue what is wrong. I’m hoping the community can give me
some insight on how to get this thing running.

All testing is done in development environment on port 3000.

Users must follow the link emailed to them in order to activate an
account.

If I set “config.action_mailer.raise_delivery_errors = false”
everything appears to work fine, but the email doesn’t get sent. (log
shows the email was sent to the correct address, activation link works
fine).
Once set to true I get a refused connection. Bellow is actual code
from each email related file, error details at the end.

environment.rb:
config.active_record.observers = :user_observer
config.action_mailer.delivery_method = :smtp

development.rb:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_charset = “utf-8”
config.action_mailer.perform_deliveries = true
config.action_mailer.default_content_type = “text/html”

mailsetup.rb:

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

email.yml:
development:
address: “shawmail.wp.shawcable.net
port: 25
domain: “shaw.ca”
authentication: :login
user_name: “[email protected]
password: “secret”

production:
address: “shawmail.wp.shawcable.net
port: 25
domain: “shaw.ca”
authentication: :login
user_name: “[email protected]
password: “secret”

test:
address: smtp.gmail.com
port: 587
domain: “gmail.com
authentication: :login
user_name: [email protected]
password: secret

account_controller.rb :
def signup
@title = “Sign Up”
@user = User.new(params[:user])

return unless request.post?
@user.btn1 = "Default"
@user.btn2 = "Default"
@user.btn3 = "Default"
@user.save!   #<--  this is line 99

redirect_back_or_default( :action => 'welcome')

rescue ActiveRecord::RecordInvalid
render :action => 'signup'

end

user_observer.rb:
class UserObserver < ActiveRecord::Observer
def after_create(user)
UserNotifier.deliver_signup_notification(user)
end

def after_save(user)
UserNotifier.deliver_activation(user) if user.recently_activated?
end
end

user_notifier:
class UserNotifier < ActionMailer::Base
def signup_notification(user)
setup_email(user)
@subject += ’ account activation’
@body[:url] = “http://localhost:3000/account/activate/#
{user.activation_code}”
end

def activation(user)
setup_email(user)
@subject += ’ ’
@body[:url] = “http://localhost:3000/
end

protected
def setup_email(user)
@recipients = “#{user.email}”
@from = “admin”
@subject = “Controllers”
@sent_on = Time.now
@body[:user] = user
end
end

Error:
Errno::ECONNREFUSED (No connection could be made because the target
machine actively refused it. - connect(2)):
C:/Ruby/lib/ruby/1.8/net/protocol.rb:206:in initialize' C:/Ruby/lib/ruby/1.8/net/protocol.rb:206:in new’
C:/Ruby/lib/ruby/1.8/net/protocol.rb:206:in old_open' C:/Ruby/lib/ruby/1.8/timeout.rb:62:in timeout’
C:/Ruby/lib/ruby/1.8/timeout.rb:93:in timeout' C:/Ruby/lib/ruby/1.8/net/protocol.rb:206:in old_open’
C:/Ruby/lib/ruby/1.8/net/smtp.rb:392:in do_start' C:/Ruby/lib/ruby/1.8/net/smtp.rb:377:in start’
C:/Ruby/lib/ruby/1.8/net/smtp.rb:315:in start' C:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/ action_mailer/base.rb:628:in perform_delivery_smtp’
C:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/
action_mailer/base.rb:508:in __send__' C:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/ action_mailer/base.rb:508:in deliver!’
C:/Ruby/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/
action_mailer/base.rb:383:in method_missing' /app/models/user_observer.rb:3:in after_create’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/
active_record/observer.rb:171:in send' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/ active_record/observer.rb:171:in update’
C:/Ruby/lib/ruby/1.8/observer.rb:185:in notify_observers' C:/Ruby/lib/ruby/1.8/observer.rb:184:in each’
C:/Ruby/lib/ruby/1.8/observer.rb:184:in notify_observers' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/ active_record/callbacks.rb:309:in notify’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/
active_record/callbacks.rb:296:in callback' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/ active_record/callbacks.rb:221:in create_without_timestamps’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/
active_record/timestamp.rb:29:in create' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/ active_record/base.rb:2483:in create_or_update_without_callbacks’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/
active_record/callbacks.rb:207:in create_or_update' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/ active_record/base.rb:2217:in save_without_validation!’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/
active_record/validations.rb:921:in save_without_dirty!' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/ active_record/dirty.rb:83:in save_without_transactions!’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/
active_record/transactions.rb:110:in save!' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/ active_record/connection_adapters/abstract/database_statements.rb: 66:in transaction’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/
active_record/transactions.rb:79:in transaction' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/ active_record/transactions.rb:98:in transaction’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/
active_record/transactions.rb:110:in save!' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/ active_record/transactions.rb:118:in rollback_active_record_state!’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/
active_record/transactions.rb:110:in save!' /app/controllers/account_controller.rb:99:in signup’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/base.rb:1166:in send' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/ action_controller/base.rb:1166:in perform_action_without_filters’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/filters.rb:579:in call_filters' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/ action_controller/filters.rb:572:in perform_action_without_benchmark’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/benchmarking.rb:68:in
perform_action_without_rescue' C:/Ruby/lib/ruby/1.8/benchmark.rb:293:in measure’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/benchmarking.rb:68:in
perform_action_without_rescue' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/ action_controller/rescue.rb:201:in perform_action_without_caching’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/caching/sql_cache.rb:13:in perform_action' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/ active_record/connection_adapters/abstract/query_cache.rb:33:in cache’
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/
active_record/query_cache.rb:8:in cache' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/ action_controller/caching/sql_cache.rb:12:in perform_action’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/base.rb:529:in send' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/ action_controller/base.rb:529:in process_without_filters’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/filters.rb:568:in
process_without_session_management_support' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/ action_controller/session_management.rb:130:in process’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/base.rb:389:in process' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/ action_controller/dispatcher.rb:149:in handle_request’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/dispatcher.rb:107:in dispatch' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/ action_controller/dispatcher.rb:104:in synchronize’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/dispatcher.rb:104:in dispatch' C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/ action_controller/dispatcher.rb:120:in dispatch_cgi’
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/dispatcher.rb:35:in dispatch' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../ lib/mongrel/rails.rb:76:in process’
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/…/
lib/mongrel/rails.rb:74:in synchronize' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/../ lib/mongrel/rails.rb:74:in process’
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/
mongrel.rb:159:in process_client' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/ mongrel.rb:158:in each’
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/
mongrel.rb:158:in process_client' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/ mongrel.rb:285:in run’
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/
mongrel.rb:285:in initialize' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/ mongrel.rb:285:in new’
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/
mongrel.rb:285:in run' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/ mongrel.rb:268:in initialize’
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/
mongrel.rb:268:in new' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/ mongrel.rb:268:in run’
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/
mongrel/configurator.rb:282:in run' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/ mongrel/configurator.rb:281:in each’
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/
mongrel/configurator.rb:281:in run' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/ mongrel_rails:128:in run’
C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/lib/
mongrel/command.rb:212:in run' C:/Ruby/lib/ruby/gems/1.8/gems/mongrel-1.1.5-x86-mswin32-60/bin/ mongrel_rails:281 C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/ active_support/dependencies.rb:503:in load’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/
active_support/dependencies.rb:503:in load' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/ active_support/dependencies.rb:355:in new_constants_in’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/
active_support/dependencies.rb:503:in load' C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.1.1/lib/commands/servers/ mongrel.rb:64 C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in gem_original_require’
C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
require' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/ active_support/dependencies.rb:510:in require’
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/
active_support/dependencies.rb:355:in new_constants_in' C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/ active_support/dependencies.rb:510:in require’
C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.1.1/lib/commands/server.rb:
39
C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
gem_original_require' C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in require’
script/server:3

Rendering C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/
action_controller/templates/rescues/layout.erb (internal_server_error)

sounds like the mail server isn’t set up to accept connections from your
server. Check that you can telnet to port 25 of that server to see if it
accepts the connection.

Simon

On Sat, 04 Jul 2009 01:32:28 +0800, Marco O. [email protected]

Hey Simon,

Yes, I am able to telnet into shawmail.wp.shawcable.ca 25.

Thanks for the reply,
Marco

then I guess the next step is to make sure the credentials you are using
are ok

On Sat, 04 Jul 2009 09:11:30 +0800, Marco O. [email protected]

MAIL FROM:[email protected]

=> 250 2.1.0 Ok

Between these two lines you need to put:

RCPT TO:[email protected]

DATA

That error message generally means that no server is listening on the
host/port name you entered. Have you tried actively testing mail calls
to all environments via telnet?

telnet mail.yourdomain.com 25

=> should receive a 220 response

greet your postman

HELO test.yourdomain.com

=> should get a 250 mail response from the postman telling you who it
is.

MAIL FROM:[email protected]

=> 250 2.1.0 Ok

DATA

=> 354 End data with .

Subject: test message
Press enter/return and then you can type the body of the message.

=> 250 2.0.0 Ok: queued as blahblahblah

=============

You should run a full test message through telnet not just a connect.
Since you are using multiple mail domains you need to test each and
every one listed in devel, test, production…

@Simon

Hey Simon,
Yes, I’m using the correct login info.

@Alpha

Hey Alpha,

All environments now use the same email settings.

I’ve telnet into shawmail.wp.shawcable.ca 25 and received all positive
returns (as per your message).

I don’t really have a domain to test so I entered “HELO test.localhost:
3000”, (I’m unsure if that’s accurate, but still returned a 250)

Thanks for the comments guys,
Marco

On Jul 3, 8:34 pm, “Älphä Blüë” [email protected]

On Jul 3, 1:32 pm, Marco O. [email protected] wrote:

Over the past few days I’ve been going crazy trying to get email
delivery to work using RoR. From what I’ve read, settings look right,
I have no clue what is wrong. I’m hoping the community can give me
some insight on how to get this thing running.

Sounds like your mail setup stuff isn’t being run and so actionmailer
is defaulting to trying to talk to localhost on port 25

Fred

Have you checked script/console to see if it at least generates the
message? Even if it cannot contact the server it should at the very
minimum show you that’s it attempting to send the message…

The only thing that you are doing differently than what I do is I don’t
use any of the individual environment files.

I put my mail settings directly in environment.rb at the very bottom…

Set up email server

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => “mail.domain.com” ,
:domain => “domain.com” ,
:authentication => :login,
:user_name => “[email protected]” ,
:password => “secret”
}

CONSTANTS BELOW

Define our mail recipient

CONTACT_RECIPIENT = “[email protected]

Issue is now resolved.

This was done by removing the authentication, user_name, and password
from the smtp settings.

The following site was of great help with telnet debugging:
http://www.yuki-onna.co.uk/email/smtp.html

I’m still unsure of how the mailer is able to send without having to
be logged into a valid email account, but it does.

This was the last piece of a small now fully functional project I
decided to tackle. As a recent computer programmer graduate I now have
something to “showoff” to potential employers.

@Simon
Thanks for trying bud.

@Alpha
Geez…
I can’t begin to describe how frustrating action mailer has been.
Your telnet guidance and debugging method was indispensable to my
solution. Thank you!

It’s great to see people taking time from their daily life to help
fellow programmers. I’ll certainly do my best to contribute to this
community.

Sincerely,
Marco

On Jul 3, 9:30 pm, “Älphä Blüë” [email protected]

Glad you got it working mate.

@Fred
You’re probably right. I’d like to point out that I changed all
environments to use the same smtp settings.

Thanks,
Marco