Problem adding a file stored on S3 via Paperclip as an attachment to ActionMailer

I use Paperclip and S3 to upload and store files in the Rails 3 application. Uploading files works well, but when you try to connect the downloaded file to an email using actionmailer, I have problems. After a lot of troubleshooting, I hope someone can offer a hint.

At a high level, it seems that I may need to first download a file with some kind of download method, which is offered here, but I donโ€™t quite understand how to implement it - paperclip + ActionMailer - Adding an attachment?

In the application, after the user uploads the file (in this case, the survey), the administrator must be notified by e-mail with the file that was downloaded by the user. I continue to work in "There is no such file or directory." Below is the code I'm working with right now. Any ideas or suggestions would be greatly appreciated!

Quiz.rb model - what users are uploading: class Quiz < ActiveRecord::Base attr_accessible :quiz_path, :user_id, :tutorial_id validates_presence_of :quiz_path validates_attachment_size :quiz_path, :less_than => 50.kilobytes validates_attachment_presence :quiz_path validates_attachment_content_type :quiz_path, :content_type => ["application/pdf","application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"], :message => 'We only accept Microsoft Excel files ending in .xlsx or .xls' belongs_to :user belongs_to :tutorial has_attached_file :quiz_path, :storage => :s3, :s3_permissions => :private, :path => "quizzes/:attachment/:style/:id.:extension", :storage => :s3, :s3_credentials => { :access_key_id => ENV["AWS_ACCESS_KEY_ID"], :bucket => ENV["S3_BUCKET_NAME"], :secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"] } end 

AdminMailer Class:

  class AdminMailer < ActionMailer::Base default from: "info@mydomain.com" def admin_upload_notification_email(quiz, current_user) @url = "http://mydomain.com" @quiz = quiz @user = current_user mail(to: "me@mydomain.com", :subject => "New Upload From #{@user.email}") attachments["#{quiz.quiz_path_file_name}"] = File.read("{quiz.quiz_path.expiring_url(60)}") end end 

Also adding a QuizzesController:

 def create @user = current_user @quiz = @user.quizzes.create(params[:quiz]) respond_to do |format| if @quiz.save UserMailer.upload_notification_email(@user).deliver AdminMailer.admin_upload_notification_email(@quiz, @user).deliver format.html { redirect_to @user, notice: 'Your skill assessment answer file was successfully uploaded. We will review your responses and email you a link to your results as soon as possible.' } format.json { render json: @user, status: :created, location: @quiz } elsif @quiz.errors.any? flash[:error] = 'Please make sure you selected your answer file and it ended in ".xlsx".' return(redirect_to :back) else format.html { redirect_to @user, notice: 'No file was selected. Please back and choose "Select Your Answer File" before submitting.' } format.json { render json: @quiz.errors, status: :unprocessable_entity } end end end 
+8
ruby-on-rails paperclip actionmailer
source share
2 answers

After he was messing with someone else, he was able to get him to work. Below is what worked for me - hope this helps someone. The key used the open uri, since the quiz files are on S3.

 class AdminMailer < ActionMailer::Base require 'open-uri' default from: "sales@mydomain.com" def admin_upload_notification_email(quiz, current_user) @url = "http://mydomain.com" @quiz = quiz @user = current_user attachments["#{quiz.quiz_path_file_name}"] = open("#{quiz.quiz_path.expiring_url(60)}").read mail(to: "admin@mydomain.com", :subject => "New Upload From #{@user.email}") end end 
+19
source share

In my case, the problem was defining content_type: "text/html" in the mail() call. Removing this fixed for me on Rails 4.

 require 'open-uri' # Allows us to read the S3 content into the mail attachment. class InvoiceMailer < ActionMailer::Base def send_invoice(user, invoice) @user = user @invoice = invoice attachments["invoice.pdf"] = open(@invoice.s3_url).read mail(to: @user.email, subject: "This Month Invoice") end end 

This ensured that the PDF was correctly embedded in Outlook and Gmail, but should work everywhere.

0
source share

All Articles