how to test your SMTP server with ActionMailer (using the Rails console)

I was having a problem delivering notifications in my Rails application; so I had to check that the SMTP server was working properly with the credentials I had. After ‘googling’ for ten minutes I found a very useful code to test this situation.

This is not my code, it is something I found here: http://lists.radiantcms.org/pipermail/radiant/2007-February/003394.html, I’m just spreading the word.

1. Open your terminal, go to your application directory and fire up the Rails console:

[etagwerker@benteveo application]$ ./script/console
Loading development environment.

2. To setup your SMTP settings, paste this code:

ActionMailer::Base.smtp_settings = {
:address  => “yoursmtpserver.com”,
:port  => 25,
:domain  => “yourdomain.com”,
:user_name  => “yourusername”,
:password  => “yourpassword”,
:authentication  => “login”
}

You should see:

=> {:port=>25, :address=>”yoursmtpserver.com”, :user_name=>”yourusername”, :domain=>”yourdomain.com”, :authentication=>”login”, :password=>”yourpassword”}

3. To setup a mail job, paste this code:

class MyMailer < ActionMailer::Base
def test_email
@recipients = “etagwerker@aycron.com”
@from = “donotreply@yourdomain.com”
@subject = “this is a subject”
@body = “this is the body”
end

end

You should see:

=> nil

4. To send the test email, paste this code:

MyMailer::deliver_test_email

You should see:

#<TMail::Mail port=#<TMail::StringPort:id=0x..fdbb06368> bodyport=#<TMail::StringPort:id=0x..fdbb02664>>

That’s it! This was very helpful for me, it helped me find a problem in my Rails environment (which had nothing to do with the SMTP server)

In my case, I was using mail.authsmtp.com, which is an authenticated SMTP server (which requires username and password to login)

This entry was posted in programming and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Nathan Bertram
    Posted January 27, 2009 at 9:13 pm | Permalink

    Thanks a ton. Also try add to see what response you get back:

    ActionMailer::Base.delivery_method = :smtp
    ActionMailer::Base.raise_delivery_errors = true

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free