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.
Ddd318eb14b80b5b048a4adc1901e68e?d=identicon&s=25 Alexander Peretti (alexp)
on 2006-01-24 17: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!!
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstien (Guest)
on 2006-01-24 18: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 = "testto@test.com "
    @from = "testfrom@test.com"
    @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...

~~~~~~~~~~~~~~~~~~
69b2ef4bce76b5b27c94e898976dc6d8?d=identicon&s=25 matthew clark (Guest)
on 2006-01-24 18: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
Ddd318eb14b80b5b048a4adc1901e68e?d=identicon&s=25 Alexander Peretti (alexp)
on 2006-01-24 18:07
Thanks Ben Reubenstien,
I'll give that a try now.
Ddd318eb14b80b5b048a4adc1901e68e?d=identicon&s=25 Alexander Peretti (alexp)
on 2006-01-24 18: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 = "alex.peretti@pulsedev.net"
      @from = "testfrom@test.com"
      @subject = "Subject"

      @body['text'] = text
      @body['name'] = name
      @body['mail'] = mail
  end
end
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstien (Guest)
on 2006-01-24 18: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)
Ddd318eb14b80b5b048a4adc1901e68e?d=identicon&s=25 Alexander Peretti (alexp)
on 2006-01-24 18: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.
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstien (Guest)
on 2006-01-24 18: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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-24 18:41
(Received via mailing list)
On 1/24/06, Alexander Peretti <alex.peretti@pulsedev.net> 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.
Ddd318eb14b80b5b048a4adc1901e68e?d=identicon&s=25 Alexander Peretti (alexp)
on 2006-01-24 18:43
Ben Reubenstien 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.
Ddd318eb14b80b5b048a4adc1901e68e?d=identicon&s=25 Alexander Peretti (alexp)
on 2006-01-24 18:45
Joe Van Dyk wrote:
> On 1/24/06, Alexander Peretti <alex.peretti@pulsedev.net> 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.
Ddd318eb14b80b5b048a4adc1901e68e?d=identicon&s=25 Alexander Peretti (alexp)
on 2006-01-24 18:48
Joe Van Dyk wrote:
> On 1/24/06, Alexander Peretti <alex.peretti@pulsedev.net> 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.
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstien (Guest)
on 2006-01-24 18:51
(Received via mailing list)
Joe is correct, need to update my example.  You want to call the deliver
method.
Ddd318eb14b80b5b048a4adc1901e68e?d=identicon&s=25 Alexander Peretti (alexp)
on 2006-01-24 19:05
Ben Reubenstien 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.
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-24 19:30
(Received via mailing list)
On 1/24/06, Alexander Peretti <alex.peretti@pulsedev.net> wrote:
> Ben Reubenstien 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.