I’m attempting to get email working and I should mention in Development.
Generating some errors and hope some one might catch my mistake.
Controller call:
def register
u = User.new(params[:user])
u.save @session[‘user’] = u.id
RegMailer.deliver_greeting(u) # this is the line that’s
throwing the error -
redirect_to :controller => “main”, :action => “welcome”
Here is the error:
undefined local variable or method `user’ for #RegMailer:0x36c2f58
RAILS_ROOT: ./script/…/config/…
Application Trace | Framework Trace | Full Trace
Here is the mailer:
class RegMailer < ActionMailer::Base
def greeting(sent_at = Time.now) @subject = ‘Thank you for registering’ @body[“first_name”]= user.first_name @recipients[“email”] = user.email @from = ‘[email protected]’ @sent_on = sent_at
end
end
Here is the view:
Dear <%= @u.first_name %>
Thank you for signing up at our webiste.
RegMailer.deliver_greeting(u) # this is the line that’s
class RegMailer < ActionMailer::Base
def greeting(sent_at = Time.now) @subject = ‘Thank you for registering’ @body[“first_name”]= user.first_name @recipients[“email”] = user.email
…
Here is the error:
undefined local variable or method `user’ for #RegMailer:0x36c2f58
I believe the user method you put in the class RegMailer is not really
defined for that class, (unlesss you defined this elsewhere?), and so
you are getting the ‘undefined user…’
when you do deliver_greeting(arg), did you pass the (arg) in the def
greeting…? it can’t find the ‘user’ method, because that argument was
not passed to it.
try modifying the greeting method to something like:
def greeting(user) @subject = ‘Thank you for registering’ @body[“first_name”]= user.first_name @recipients[“email”] = user.email
…
I’m not even sure why the argument to def greeting is “sent_at =
Time.now”
I went along with the same from AWDWR. Where in just setting up an
email in Ruby and ActionMailer I sent mail using def
simple_message(recipient) which seemed to work.
I believe the user method you put in the class RegMailer is not really
defined for that class, (unlesss you defined this elsewhere?), and so
you are getting the ‘undefined user…’
“NoMethodError in MainController#register
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.[]=”
Here is the mailer
def greeting(user) @subject = ‘Thank you for registering’ @body[“first_name”]= user.first_name @recipients[“email”] = user.email @from = ‘****@*****.com’ @sent_on = sent_at
end
and the action :
def register
u = User.new(params[:user])
u.save @session[‘user’] = u.id
RegMailer.deliver_greeting(u) <--------------ACTION for Mailer
redirect_to :controller => “main”, :action => “welcome”
end
…i also believe ActionMailer works only in production mode, and if you
need to check mistakes, go to the log file…?
is any of this helping?
–
I don’t know if that’s true, from what I read you configure in the
development config file and then should be allowed to send my mail
through and SMTP server. I’ve done it via regular Ruby program with
ActionMailer not sure why Rails would be any different.
Right now I’m still getting :
getaddrinfo: no address associated with hostname.
Anyone have any ideas why ? Everything looks fine in the config file.
Thanks Michael,
I’ll play around with the settings as you suggest.
I still wonder if something else within Rails is causing the issue.
My Ruby email program has the same settings as the environment.rb in
Rails, except that from what I see Rails uses double quotes, and the
Ruby has single quotes around the parameter. Didn’t seem to matter
much though.
…i also believe ActionMailer works only in production mode, and if you
need to check mistakes, go to the log file…?
is any of this helping?
–
I don’t know if that’s true, from what I read you configure in the
development config file and then should be allowed to send my mail
through and SMTP server. I’ve done it via regular Ruby program with
ActionMailer not sure why Rails would be any different.
Right now I’m still getting :
getaddrinfo: no address associated with hostname.
Anyone have any ideas why ? Everything looks fine in the config file.
Stuart
ActionMailer works in Dev mode - no problem! I configure it in
Environment.rb so not what happens if you put the settings in
development.rb as you mention above.
Regardless, I had a heck of a time getting mine setup originally and it
took a few combination of settings before it worked. For me, I
commented out the domain setting and that did the trick. For my ISP
host, I had to change the port number to 80 from 25 (I’m relaying).
Try that and see if it helps. All the other errors you mention had
nothing to do with actionmailer!
Thanks Michael,
I’ll play around with the settings as you suggest.
I still wonder if something else within Rails is causing the issue.
My Ruby email program has the same settings as the environment.rb in
Rails, except that from what I see Rails uses double quotes, and the
Ruby has single quotes around the parameter. Didn’t seem to matter
much though.
Stuart
Stuart - one more thing to think about… mail.XXX.com may be your
incoming server name - not the outgoing server name. Mine is smtpout.XXX.com. Maybe this isn’t the case since you have it working
elsewhere but just a thought.
Stuart - one more thing to think about… mail.XXX.com may be your
incoming server name - not the outgoing server name. Mine is smtpout.XXX.com. Maybe this isn’t the case since you have it working
elsewhere but just a thought.
Regards,
Michael
I just got back to working on this and did a registration to test.
This time the getaddrinfo error wasn’t thrown, but I haven’t seen an
email come through either.
I opened the development log and saw this:
Haven’t seen a reply come through so I’m throwing this out again.
Not sure what the code issues are but I want to send an email when a
new user registers.
I’m capturing their information in the variable u and that works for
database entry. Problem is passing it over to the mailer. I’m guess
becuase u is a local it’s not making it across to the mailer. From
the log the email is working but it says it went to [email protected]
Would like some suggestions maybe.
Hi Stuart,
It’s sending it exactly to the location you asked for! Take out the
single quotes around user.email and you should be good to go!
Haven’t seen a reply come through so I’m throwing this out again.
Not sure what the code issues are but I want to send an email when a
new user registers.
I’m capturing their information in the variable u and that works for
database entry. Problem is passing it over to the mailer. I’m guess
becuase u is a local it’s not making it across to the mailer. From
the log the email is working but it says it went to [email protected]
Haven’t seen a reply come through so I’m throwing this out again.
Not sure what the code issues are but I want to send an email when a
new user registers.
I’m capturing their information in the variable u and that works for
database entry. Problem is passing it over to the mailer. I’m guess
becuase u is a local it’s not making it across to the mailer. From
the log the email is working but it says it went to [email protected]
Would like some suggestions maybe.
Hi Stuart,
It’s sending it exactly to the location you asked for! Take out the
single quotes around user.email and you should be good to go!
Michael
Actually - you have a few syntax issues in the code you pasted!
user.first_name instead of ‘user.first_name’
user.email instead of ‘user.email’
and add a single quote in front of [email protected]
it’s being sent to the correct email address. Still no email is coming
through and so now I’m wondering if it’s a configuration issue with
ActionMailer. I believe that in order to actually use mailers in
development mode you make all the specific configurations in
config/environments/development.rb which I’ve done. I went ahead to
make sure that i could connect to my remote smtp server via a Ruby
program. Again the Ruby program sends the mail (and it’s using
ActionMailer). Rails though is not sending the email ?
Stuart
I also want to add that I put the configuration in environment.rb as
well. Still no change.
Stuart
Actually - you have a few syntax issues in the code you pasted!
I made all the syntax corrections and now the development log shows
I also want to add that I put the configuration in environment.rb as
well. Still no change.
Stuart
One more thing :
I’ve set the following line (instead of default)
config.action_mailer.raise_delivery_errors = true
and not seeing any errors in dev log.
I made all the syntax corrections and now the development log shows
it’s being sent to the correct email address. Still no email is coming
through and so now I’m wondering if it’s a configuration issue with
ActionMailer. I believe that in order to actually use mailers in
development mode you make all the specific configurations in
config/environments/development.rb which I’ve done. I went ahead to
make sure that i could connect to my remote smtp server via a Ruby
program. Again the Ruby program sends the mail (and it’s using
ActionMailer). Rails though is not sending the email ?
bump! sorry this is driving me nuts, development log say mail is
sending ,but the mail is not coming through.
Just to review here I’ve set ActionMailer configuration options in
config/environments/development.rb.
e[4;35;1mSQL (0.078000)e[0m e[0mCOMMITe[0m
Sent mail:
From: [email protected]
To: [email protected]
Subject: Thank you for registering
Content-Type: text/plain; charset=utf-8
I changed the username and domain name but in the log it’s correct.
I’ve gone through the environment.rb file to make sure nothing was
configured there for ActionMailer and even went and configured it.
then it’s most likely something outside of your app. I’m thinking
your ISP doesn’t allow that sort of traffic. If that’s the problem,
you can get around it if your host allows mail to be sent on an
alternate port (!= 25). I know Dreamhost allows that sort of
shenanigans.
Wait here is something weird! I started thinking maybe something is
wrong with the local server (webrick) and considered trying mongrel.
However instead of loading up the app in firefox via webrick and just
run it in RadRails under port 3003. Trying to register 3 x I get
recognition errors. Very odd since things were working right before.
4th time registration succeeds and the email goes through.
So it’s got to be something in my browser (only thing I can think of).
Though I’m not entirely sure what browser RadRails is using. Then
there is the weirdness of the browser inside RadRails giving me
routing errors (recognition).
Oiiiiiiiiiiiii!
Stuart
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.