How to attach files that are stored in the database to the action mailer?
1 answer
It is not very different from sending attachments stored on disk.
Let's say you have a model Binary
that matches files in the filesystem. If it responds to content_type
and data
, then something like this should work:
class AttachmentMailer < ActionMailer::Base
def attachment(recipient, binary)
recipients recipient
subject "Your requested file"
from "example@example.com"
attachment :content_type => binary.content_type, :body => binary.data
end
end
# Wherever you want to e-mail something:
binary = Binary.find(:first)
Notifier.deliver_attachment("user@example.com", binary)
Of course, if you store your data differently, or your database columns are named differently, you must customize the methods of the class Binary
(or whatever class you use) in the example above.
Hope this helps.
0
a source to share