9

我在 Rails 3 应用程序中使用 Paperclip 和 S3 进行文件上传和存储。文件的上传运行良好,但是在尝试使用 actionmailer 将上传的文件附加到电子邮件时,我遇到了问题。经过大量故障排除后,希望有人可以提供提示。

从高层次来看,似乎我可能需要先下载文件,在附加之前使用某种下载方法,这里建议但我不太了解如何实现 -回形针 + ActionMailer - 添加附件?

在应用程序中,用户上传文件(在本例中为测验)后,应通过电子邮件通知管理员用户上传的文件。我一直遇到“没有这样的文件或目录”。下面是我现在正在使用的代码。任何想法或建议将不胜感激!

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

还添加了 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
4

2 回答 2

20

在搞砸了一些之后,能够让它工作。以下是对我有用的方法-希望对某人有所帮助。关键是使用 open uri,因为测验文件位于 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
于 2013-03-18T05:24:36.073 回答
0

就我而言,问题content_type: "text/html"出在mail()调用中。删除它为我解决了 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's Invoice")
  end
end

这会在 Outlook 和 Gmail 上正确呈现 PDF 文档附件,但应该可以在任何地方使用。

于 2015-07-30T23:44:44.713 回答