How to attach files that are stored in the database to the action mailer?

You mentioned how to store files in a database. How do I attach these files by mail using a mail program? I have searched many sites but cannot find a way to attach the database files. Support is provided everywhere for attaching files stored in the file system.

0


a source to share


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







All Articles