In this particular project I was using the Delayed_job gem to process jobs in the background. After installing and testing it out, it works great.
The first time I noticed some strange behavior that practically took me more than a hour to find out what the cause was. I was using another useful gem called letter_opener by Ryan Bates. This is especially great for testing mailer when you do not want the mail to actually go out but you want to see the output of the mail and to know that the mailer is doing its job. So any mail that you sent will open up in a browser with the mail content displayed, as though you are reading it with a mail client. That saves you the trouble of always having to check your mail in your mail client. Another great thing about letter opener is that you can be using fake email address and it will not be a problem since the mail does not actually go out to the mail server.
Anyway I have gone off in a tangent. Back to my issue. After using letter_opener for a while, it was time for me to test the actual smtp sending part. While that should be easy… in theory. All I need to do is to change the mailer configuration in the config/development.rb from:
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.delivery_method = :smtp
And Rails should be using the smtp from then on, right? Well, it didn’t behave as expected. It was still displaying the result with the letter_opener. That was a rather strange behavior. I thought perhaps restarting of the web server would help resolve the issue. Nope. Still giving the same result. In the end I even tried uninstalling the letter_opener gem, and the Rails mailer was still trying to send to letter_opener. But in this case, nothing happened. It’s like the mail went into the never-never land. It just doesn’t make sense until I noticed the mailer job was actual queuing in the delayed_job table. And there was an error message inside the delayed_job record in its table. It said it was unable to find the letter_opener gem in the specified gem path.
All of a sudden a light turned on in my head. It must have something to do with Delayed_job caching the previous setting somewhere. I don’t know how delayed_job work internally (do not have time at this point to check its code) but it is definitely caching something somewhere. So I did the next logical thing which was to restart the delayed_job process that was running in the background. And viola! Now it finally recognized the change I made in the development.rb.
One more thing to watch out for. I made a change in the mailer file and the change did not take effect until I restarted delayed_job.
So the conclusion is if you are using delayed-job, be aware that whenever you make any changes in the configuration and things still does work as you expected, try restarting the delayed_job process. You can do it by this command script/delayed_job restart. This will save you a lot of troubleshooting frustrations.