简体   繁体   English

如何将存储在数据库中的文件附加到动作邮件程序?

[英]how to attach files that are stored in the database to the action mailer?

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. 随处都提供了附加文件系统中存储文件的帮助。

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. 假设您有一个与文件系统中的文件相对应的Binary模型。 If it responds to content_type and data , then something like this should work: 如果它对content_typedata做出响应,则应该可以执行以下操作:

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 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. 当然,如果您存储数据的方式不同或数据库列的名称不同,则应在上述示例中调整Binary类(或使用的任何类)的方法。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM