Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paperclip + ActionMailer - Adding an attachment?

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
like image 909
TheExit Avatar asked Dec 05 '10 01:12

TheExit


1 Answers

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
like image 85
dalf Avatar answered Oct 01 '22 05:10

dalf