views:

172

answers:

1

Hi, you have mention about how to store files in database. How to attach those files with mail using action mailer? I have search many sites but not able to find the way to attach database files. Everywhere the help of attaching files stored in file system is given.

A: 

It's not very different from sending attachments stored on disk.

Say you have a model Binary that corresponds to files in the file system. 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 "[email protected]"

    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("[email protected]", binary)

Of course, if you store your data differently or if the database columns are named differently, you should adjust the methods of the Binary class (or whichever class you use) in the above example.

I hope this helps.

molf
thanks molf.you have solved my problem almost.the only remaining thing is :filename => binary.filename (# here binary.filename means binary table and filename is a column which stores filename).So it is done!