Recording Sent Mail with Observers

11 Jan 2013

I’ve had scenarios before where the users of an application want access to mail sent out through the application. Fortunately, it’s easy to craft an Observer to store a quick copy of the mail object.

A few notes:

  • It’s possible to just serialise the message object directly. Storing the attributes makes it easier to query the Email object.
  • Most of the fields on message (eg. message.from) act or display like arrays or strings, but are actually instances of classes like Mail::AddressContainer. They’ll respond to most array or string commands.
  • In multipart emails, message.body will not be sufficient to retreive the body. You might need to use message.html_part and message.text_part. message.parts will retrieve all parts (including eg. attachments), and is an instance of Mail::PartsList. It behaves like an array.
comments powered by Disqus