A bad day with Action Mailer


#1

Hi, I’ve setup Action Mailer today to email the contents of a form.
Every seemes fine except when the email is sending.

Here is the error I get on the production server:

SocketError (getaddrinfo: Name or service not known):
/usr/lib/ruby/1.8/net/protocol.rb:83:in initialize' /usr/lib/ruby/1.8/net/protocol.rb:83:innew’
/usr/lib/ruby/1.8/net/protocol.rb:83:in connect' /usr/lib/ruby/1.8/net/protocol.rb:82:intimeout’
/usr/lib/ruby/1.8/timeout.rb:62:in timeout' /usr/lib/ruby/1.8/net/protocol.rb:82:inconnect’
/usr/lib/ruby/1.8/net/protocol.rb:64:in initialize' /usr/lib/ruby/1.8/net/smtp.rb:393:inopen’
/usr/lib/ruby/1.8/net/smtp.rb:393:in do_start' /usr/lib/ruby/1.8/net/smtp.rb:378:instart’
/usr/lib/ruby/1.8/net/smtp.rb:316:in start' /usr/lib/ruby/gems/1.8/gems/actionmailer-1.1.5/lib/action_mailer/base.rb:436:inperform_delivery_smtp’
/usr/lib/ruby/gems/1.8/gems/actionmailer-1.1.5/lib/action_mailer/base.rb:327:in
send' /usr/lib/ruby/gems/1.8/gems/actionmailer-1.1.5/lib/action_mailer/base.rb:327:indeliver!’
/usr/lib/ruby/gems/1.8/gems/actionmailer-1.1.5/lib/action_mailer/base.rb:223:in
method_missing' /app/controllers/email_controller.rb:8:insent’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in
send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:inperform_action_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:in
perform_action_without_benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in
measure' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:in
perform_action' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:insend’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in
process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:inprocess’
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in
dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/fcgi_handler.rb:141:inprocess_request’
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/fcgi_handler.rb:53:in
process!' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/fcgi_handler.rb:52:ineach_cgi’
/usr/lib/ruby/1.8/fcgi.rb:597:in each' /usr/lib/ruby/1.8/fcgi.rb:597:ineach_cgi’
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/fcgi_handler.rb:52:in
process!' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/fcgi_handler.rb:22:inprocess!’
dispatch.fcgi:24

It does do this before it though Processing EmailController#sent (for
84.12.155.53 at 2006-01-24 08:50:41) [POST] So the code generating the
email is fine.

I would really appreciate any help!!


#2

Hi Alex ~

I have posted this before, but it might help. In this configuration the
smtp server is on the same server as the app. This works in both my
production and development environment.

Here is an example of all the “pieces” to get actionmailer working.

rails_root/config/environment.rb

ActionMailer::Base.server_settings = {
  :address  => "localhost",
  :port  => 25,
  }

rails_root/app/controllers/my_controller.rb

def test_message_send
    Notifier::sendtest()
end

rails_root/app/models/notifier.rb

class Notifier < ActionMailer::Base

  def sendtest()

    @recipients = "removed_email_address@domain.invalid "
    @from = "removed_email_address@domain.invalid"
    @subject = "Subject"

    @body["first_name"] = "first_test"
    @body["last_name"] = "last_test"
  end

end

rails_root/app/views/notifier/sendtest.rhtml


Hello <%= @first_name %> <%= @last_name %>,

Test Msg...


#3

When I read the subject on this, I thought the body was going to be -

Is still a better day than mailing in PHP/ASP/Perl.

matt


#4

Thanks Ben R.,
I’ll give that a try now.


#5

Hi Alex ~

I don’t think all your variables need to be @ variables… When you call
your mailer object, try:

mail = params[‘mail’]
name = params[‘name’]
text = params[‘text’]
Mail::sendtest(mail, name, text)


#6

Okay did that and now I have

NoMethodError in Email#test_message
undefined method sendtest' for Mail:Class RAILS_ROOT: /Users/alexp/Sites/pulse/script/../config/.. Application Trace | Framework Trace | Full Trace /usr/lib/ruby/gems/1.8/gems/actionmailer-1.1.5/lib/action_mailer/base.rb:225:inmethod_missing’
#{RAILS_ROOT}/app/controllers/email_controller.rb:8:in `test_message’

I dont think it likes that sendtest method very much.


#7

Alex ~

Have you restarted your app lately? I have had some instances where
changes
where not picked up on my mailer model… Worth a shot.

~ Ben


#8

I cant get past this error now

You have a nil object when you didn’t expect it!
The error occured while evaluating nil.sendtest
RAILS_ROOT: /Users/alexp/Sites/pulse/script/…/config/…
Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/controllers/email_controller.rb:8:in `test_message’

my controller is this
class EmailController < ApplicationController
def index
end
def test_message
@mail = @params[‘mail’]
@name = @params[‘name’]
@text = @params[‘text’]
@Mail::sendtest(@mail, @name, @text)
end
end

my mail.rb is this
class Mail < ActionMailer::Base
def sendtest(mail, name, text)
@recipients = “removed_email_address@domain.invalid”
@from = “removed_email_address@domain.invalid”
@subject = “Subject”

  @body['text'] = text
  @body['name'] = name
  @body['mail'] = mail

end
end


#9

On 1/24/06, Alexander P. removed_email_address@domain.invalid wrote:

I dont think it likes that sendtest method very much.
Please include the previous messages that you are replying to.

What the heck is Mail::sendtest?

Try Mail::deliver_sendtest.

Read http://api.rubyonrails.com/classes/ActionMailer/Base.html and
brush up on your Ruby.


#10

Ben R. wrote:

Alex ~

Have you restarted your app lately? I have had some instances where
changes
where not picked up on my mailer model… Worth a shot.

~ Ben

Yeah I’ve been doing that alot, didn’t change anything.

I’m quite dissapointed with the documentation on ActionMailer. There are
no decent examples on the subject.


#11

Joe Van D. wrote:

On 1/24/06, Alexander P. removed_email_address@domain.invalid wrote:

I dont think it likes that sendtest method very much.
Please include the previous messages that you are replying to.

What the heck is Mail::sendtest?

Try Mail::deliver_sendtest.

Read http://api.rubyonrails.com/classes/ActionMailer/Base.html and
brush up on your Ruby.

I’ve been working on it all day, Mail::deliver_sendtest. is what I used
at the start then I was advised to change it so I did.


#12

Joe Van D. wrote:

On 1/24/06, Alexander P. removed_email_address@domain.invalid wrote:

I dont think it likes that sendtest method very much.
Please include the previous messages that you are replying to.

What the heck is Mail::sendtest?

Try Mail::deliver_sendtest.

Read http://api.rubyonrails.com/classes/ActionMailer/Base.html and
brush up on your Ruby.

I changed it back to deliver_sendtest now and it works. My problem was
with the smtp server in the end it seems. Although I haven’t tested it
in production yet and that’s where I had the problem.


#13

Joe is correct, need to update my example. You want to call the deliver
method.


#14

Ben R. wrote:

Joe is correct, need to update my example. You want to call the deliver
method.

Thanks again guys.

And for your information, you still need to use
@mail = @params[‘mail’]
@name = @params[‘name’]
@text = @params[‘text’]

Or it doesn’t work.


#15

On 1/24/06, Alexander P. removed_email_address@domain.invalid wrote:

Ben R. wrote:

Joe is correct, need to update my example. You want to call the deliver
method.

Thanks again guys.

And for your information, you still need to use
@mail = @params[‘mail’]
@name = @params[‘name’]
@text = @params[‘text’]

er, I assume this is in the controller.

def YourController < AC
def a_method
mail = params[‘mail’]
name = params[‘name’]
text = params[‘text’]
Mail::deliver_sendtest(mail, name, test)
end
end

That should work fine. There’s no need to create controller instance
variables.