I'm having trouble configuring a mailer. Prudence would dictate that
I should upload at least portions of the relevant files, etc. If it
comes to that, I'll certainly do it. However, before I go that route I
would really like to see if I can't use this problem to develop some
trouble shooting skills because I know that I'm going to need them in
the future.
Basically the situation is that I have followed the general setup
detailed in 'Agile Web Development with Rails'. Amongst other things I
have specifically included the line:
config.action_ mailer.delivery_ method=:sendmail
in my config/environments/test.rb file and /usr/sbin/sendmail is a sym
link to my sendmail-compatible smtp server. The single symptom is that
the email doesn't actually get sent. Otherwise, the whole process
completes exactly as one would expect. There are no indications of
problems logged to either the rails log file or the log file of my
smtp server. The fact that nothing is logged to the log file of my
smtp server makes me believe that it is not actually receiving a
request.
So, my question is this: Given this fact pattern how should I proceed
in order to isolate and correct the problem?
Are you running the application in development mode?
No. I'm running in test mode. I've gotten this thing to
work before in test mode; but, not this time. The issue
I'm trying to address is: What does one do when
something like this happens. I'm thinking that there
must be some sort of troubleshooting procedure to
follow.
What I generally do is to first try the obvious. Step through the code
mentally looking for any obvious mistakes. I catch most of my errors
this way. If that doesn't work, sometimes you can use debug logging to
validate your assumptions. Spit out the value of variables, write a
log entry when you enter a function to make sure you're actually
entering it, etc.
When the above fails (actually I usually go to this next, before I
bother with the logging thing), the best thing you can do for yourself
is to use a decent debugger. The value of having a good debugger at
your disposal - and using it - cannot be overstated. They can save you
many many minutes of head-scratching time. Sometimes hours. I can't
tell you the number of times I've figured out an issue with a debugger
in a few minutes that I'm sure would have taken me hours to figure out
without one.
For my money (which is sort of a misnomer, because this one's free),
the best Ruby/Rails debugger available right now is the one in
NetBeans 6. If you've used a debugger with Java or C++ or other
languages before, this one works pretty much the same. If you haven't,
now is the time to learn how. Don't worry, it really isn't hard.
It's pretty easy to get started, and just takes some practice to
become proficient.
I strongly recommend downloading NetBeans 6 M9 and running your
project inside of its debugger. Set a breakpoint on the line where you
send the mail, and when you hit it, start stepping through the code
line by line, inspecting variables and watching where the execution
path goes along the way. You'll find your problem. Make sure you
switch NetBeans from using JRuby to using your native Ruby
interpreter, and see this page for setting up the fast debugger:
Good point, and maybe this is something I need to look into because
that's exactly how it's behaving. That is to say that it's behaving
as though it were in testing mode and not actually sending the e-
mail. However, what I *MEANT* was that I'm running in the testing
*ENVIRONMENT*. I do have:
Thanks, Bill, for the thorough response. Your point is well
taken about the debugger. A debugger is an invaluable tool
to be used as a debugging AID after one has completed
the first step that you described which is:
Step through the code
mentally looking for any obvious mistakes.
The problem I'm having with Rails is that I don't know what
code to step through. I read these instructions on how to make
the mailer work. I follow them and when it doesn't work, I have
no idea what to do. This has got to be an easy thing. I can
send a test e-mail very simply using the following shell script:
> Step through the code
> mentally looking for any obvious mistakes.
The problem I'm having with Rails is that I don't know what
code to step through.
I meant stepping through the code you wrote. Not necessarily Rails
code (although the debugger will take you in there if you use it; you
won't have to know what Rails code to look at, the debugger will).
You have
mentioned 'debug logging'. I wasn't aware that there was such
a thing. Presumably that advances the logging level. I'll look
into that as that may be the key to solving my problem.
Actually I was talking about adding logging statements to your code,
such as "some_variable is currently #{some_variable}" in order to see
what's going on without necessarily using a debugger (which can tell
you all the same things and then some). However, now that you mention
it, there is such a thing as debug logging. It's already turned on by
default if you're running in development mode, but you can enable it
for other modes by putting this in your environment.rb file: