繁体   English   中英

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

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

您提到了如何在数据库中存储文件。 如何使用动作邮件程序将这些文件附加到邮件中? 我搜索了许多站点,但是找不到附加数据库文件的方法。 随处都提供了附加文件系统中存储文件的帮助。

这与发送存储在磁盘上的附件没有太大区别。

假设您有一个与文件系统中的文件相对应的Binary模型。 如果它对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)

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

我希望这有帮助。

暂无
暂无

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

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