Rails - ActionMailer - 如何发送您创建的附件? [英] Rails - ActionMailer - How to send an attachment that you create?

查看:37
本文介绍了Rails - ActionMailer - 如何发送您创建的附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 rails3 w ActionMailer 中,我想发送一个 .txt 文件附件.挑战是这个 txt 文件不存在,而是我想根据我拥有的一大块文本创建 txt 文件.

In rails3 w ActionMailer, I want to send a .txt file attachment. The challenge is this txt file does not exist but rather I want to create the txt file given a large block of text that I have.

可能吗?想法?谢谢

推荐答案

ActionMailer::Base 的 API 文档

class ApplicationMailer < ActionMailer::Base
  def welcome(recipient)
    attachments['free_book.pdf'] = File.read('path/to/file.pdf')
    mail(:to => recipient, :subject => "New account information")
  end
end

但这不一定是一个文件,它也可以是一个字符串.所以你可以做一些类似的事情(我也使用更长的基于哈希的形式,你也可以在其中指定你自己的 mimetype,你可以在 ActionMailer::Base#attachments):

But that doesn't have to be a File, it can be a string too. So you could do something like (I'm also using the longer Hash-based form where you can specify your own mimetype too, you can find documentation for this in ActionMailer::Base#attachments):

class ApplicationMailer < ActionMailer::Base
  def welcome(recipient)
    attachments['filename.jpg'] = {:mime_type => 'application/mymimetype',
                                   :content => some_string }
    mail(:to => recipient, :subject => "New account information")
  end
end

这篇关于Rails - ActionMailer - 如何发送您创建的附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆