paperclip + ActionMailer - Adding an attachment?

。_饼干妹妹 提交于 2019-12-18 15:35:28

问题


I have a paperclip'd file that I want to add as an attachment to my email....

class UserMailer < ActionMailer::Base def XXXXXX_notification(record) @record = record

  attachments ??? How to add a paperclip file?

  mail( :to => "#{record.email}", 
        :subject => "XXXXXXXX"
        )
end

There seems to be nothing on the topic via google, if you have any ideas, I'd love to hear it :)

Thanks

UPDATE

  @comment.attachments.each do |a|
    tempfile = File.new("#{Rails.root.to_s}/tmp/#{a.attachment_file_name}", "w")
    tempfile << open(a.authenticated_url())
    tempfile.puts
    attachments[a.attachment_file_name] = File.read("#{Rails.root.to_s}/tmp/#{a.attachment_file_name}")
    # Delete it tempfile
    #File.delete("#{Rails.root.to_s}/tmp/#{a.filename}")
  end

回答1:


From the Ruby on Rails guides (only reachable through Bing):

http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments

All that is left is to download the attachment (if in S3) to a file object or access it it is stored locally. Try using open-uri.




回答2:


It has been already answered but I just want to share a slightly different way to do it:

Here is my model Report. I'm using Paperclip.

class Report < ActiveRecord::Base
  has_attached_file :pdf_file
  ...
end

And here is my mailer ReportMailer

class ReportMailer < ActionMailer::Base
  def monthly_report_email(emails, report)
    attachments[report.pdf_file_file_name] = File.read(report.pdf_file.path)
    mail(:to => emails, :subject => 'monthly report')
  end
end


来源:https://stackoverflow.com/questions/4356936/paperclip-actionmailer-adding-an-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!