问题
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