Forum: Ruby on Rails A bad day with Action Mailer

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alexander P. (Guest)
on 2006-01-24 18:57
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:in `new'
    /usr/lib/ruby/1.8/net/protocol.rb:83:in `connect'
    /usr/lib/ruby/1.8/net/protocol.rb:82:in `timeout'
    /usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
    /usr/lib/ruby/1.8/net/protocol.rb:82:in `connect'
    /usr/lib/ruby/1.8/net/protocol.rb:64:in `initialize'
    /usr/lib/ruby/1.8/net/smtp.rb:393:in `open'
    /usr/lib/ruby/1.8/net/smtp.rb:393:in `do_start'
    /usr/lib/ruby/1.8/net/smtp.rb:378:in `start'
    /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:in
`perform_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:in
`deliver!'
    /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:in `sent'
    /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:in
`perform_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:in
`perform_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:in
`perform_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:in
`send'
    /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:in
`process'
    /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:in
`process_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:in
`each_cgi'
    /usr/lib/ruby/1.8/fcgi.rb:597:in `each'
    /usr/lib/ruby/1.8/fcgi.rb:597:in `each_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:in
`process!'
    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!!
Ben R. (Guest)
on 2006-01-24 19:05
(Received via mailing list)
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...

~~~~~~~~~~~~~~~~~~
matthew clark (Guest)
on 2006-01-24 19:05
(Received via mailing list)
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
Alexander P. (Guest)
on 2006-01-24 19:07
Thanks Ben R.,
I'll give that a try now.
Alexander P. (Guest)
on 2006-01-24 19:23
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
Ben R. (Guest)
on 2006-01-24 19:29
(Received via mailing list)
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)
Alexander P. (Guest)
on 2006-01-24 19:33
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:in
`method_missing'
#{RAILS_ROOT}/app/controllers/email_controller.rb:8:in `test_message'

I dont think it likes that sendtest method very much.
Ben R. (Guest)
on 2006-01-24 19:38
(Received via mailing list)
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
Joe Van D. (Guest)
on 2006-01-24 19:41
(Received via mailing list)
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.
Alexander P. (Guest)
on 2006-01-24 19:43
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.
Alexander P. (Guest)
on 2006-01-24 19:45
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.
Alexander P. (Guest)
on 2006-01-24 19:48
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.
Ben R. (Guest)
on 2006-01-24 19:51
(Received via mailing list)
Joe is correct, need to update my example.  You want to call the deliver
method.
Alexander P. (Guest)
on 2006-01-24 20:05
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.
Joe Van D. (Guest)
on 2006-01-24 20:30
(Received via mailing list)
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.
This topic is locked and can not be replied to.