views:

38

answers:

1

I think this may be a common situation… I'm working on a password recovery system for a Rails app that sends a link to the user to trigger a new password form. Pretty standard stuff.

On my development server I don't have any mail-sending software enabled or configured (sendmail, SMTP settings, etc.) In config/environments/development.rb I have config.action_mailer.raise_delivery_errors = false to suppress any errors that arise since I don't have a local mail server enabled. This is all good and fine.

However, I would like to view the content of the e-mails while in production without actually sending the mail. I know that it's possible to kind of do this through testing, asserting that the sent (or faux-sent) mail has the correct content. Is there any way to reroute views or something, just temporarily in production, to view an HTTP-served version of the e-mail rather than blindly making assertions?

+1  A: 

If you have a UserMailer setup with a "password_reminder" method, you can call create_password_reminder instead of deliver_password_reminder and it will create the message without actually sending. Then you could send the output to the log file:

Where you would have:

UserMailer.deliver_password_reminder

You can replace with:

logger.info UserMailer.create_password_reminder.encoded

Or if you want to send it to a file, you can do that as well.

That being said, the production environment really isn't the place for this sort of thing. I've never had a need to do this, because my mailers have full test coverage. I'd look into that option instead, but I gave the answer you asked for because I don't know your full situation. Happy coding :)

Jaime Bellmyer
Logging will do it. I'm newer to Rails testing and (sadly) wanted to see the created messages so I could base my tests on them, rather than blindly imagining what the messages should look like by following the code. It's most definitely a crutch :)
Andrew
Actually, it automatically logs sent messages without making a call to logger (at least in Rails 2.3), so I don't have to change my code at all--I just have to check the log.
Andrew
Sure, I understand. TDD has a steep learning curve, and just about anything new has to be done first before you can understand how to test it properly :)
Jaime Bellmyer
That sounds familiar, now that you mention it! The good news is that I think it only does that in development mode, and not in production. So you won't have all that extra fluff in your production logs.
Jaime Bellmyer