开发者

Rails - ActionMailer - How to send an attachment that you create?

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开发者_C百科 of text that I have.

Possible? Ideas? Thanks


It's described for files in the API documentation of ActionMailer::Base

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

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


First the method to send email

class ApplicationMailer < ActionMailer::Base
   def welcome(user, filename, path)
      attachments[filename] = File.read(path)
      mail(:to => user.email, :subject => "New account information")
   end
end

Call the method with the params

UserMailer.welcome(user, filename, path).deliver
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜