Logging outgoing emails in Rails 3
by Trevor Turk
Let’s say you want to have a log of all outgoing emails in your Rails 3 app. Thanks to some pretty sweet notification support in the Mail gem, it’s really simple. Here’s an example using Mongoid:
Initially, I was logging the entire contents of the email, but that included attachments as well. So, now I’m just logging the body. I wonder if there’s a better choice of what to log, though…
Any ideas?
Advertisement
Hi Trevor – it probably depends a lot on the design/behavior of your app. Most sites I've built have a limited number of different types of emails that are ever sent. So normally I would only log a few headers: to, subject (similar to the type) and date, etc. But not the body or attachments.
Logging the entire body can often be unnecessary since that is probably generated by other values from your db. Does the Mail gem use ERB to generate the body like action mailer?
Yeah, the Mail gem is used pretty much like the old one — I think it was tmail…? It's just like a version 2.
I agree with you — I don't think it's necessary to have much more than a one-liner for each email going out, because you can reconstruct what happened if you know the to/from/date/subject.