通过电子邮件将回形针文件作为附件发送 - Rails

Send paperclip file via email as an attachment - Rails

我有一张表格,当潜在雇员有兴趣为公司工作时 he/she 填写表格并附上简历。提交的文件将连同附件一起发送给公司代表。电子邮件正在通过,但附件不是文档,我无法弄清楚如何正确配置它。在提交电子邮件中,附件只是说 "document".

career_mailer.rb

class CareerMailer < ApplicationMailer

  default from: "career@conciergenursingdirect.com"



  def career_inquiry(career)
    @career = career
     attachments['attachment.extension'] = document
    mail(to: "michele@conciergenursingdirect.com", subject: "This is just a test from Jay")
  end
end

career.rb(型号)

class Career < ApplicationRecord

    has_attached_file :document

    validates_attachment_size :document, :less_than => 25.megabytes    
    validates_attachment_presence :document
     validates_attachment_content_type :document, :content_type => ["application/pdf","application/vnd.ms-excel",     
                                                                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                                                    "application/msword", 
                                                                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 
                                                                    "text/plain"]

    email_regex = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i

    validates :name, :presence => true,
              :length          => { :maximum => 50 }
    validates :subject, :presence => true,
              :length          => { :maximum => 50 }
    validates :phone, :presence => true,
    :length          => { :maximum => 50 }
    validates :email, :presence => true,
              :format          => {:with => email_regex }
    validates :message, :presence => true,
              :length          => { :maximum => 5000 }

end

careers_controller.rb

class CareersController < ApplicationController
  def new
  @career = Career.new

  end
  def show
    @career = Career.find(params[:id])
  end 

  def create
    # fail
    @career = Career.create(career_params)
    if @career.save
      CareerMailer.career_inquiry(@career).deliver
      redirect_back(fallback_location: root_path)
    else
      flash[:error] = @career.errors.full_messages
      redirect_back(fallback_location: root_path)
    end


  end
  private
  def career_params
    params.require(:career).permit(:name, :phone, :subject, :email, :message, :document)
  end
end

更新

我正在我的职业邮件中尝试以下内容:

attachments[career.document.attach_file_name] = File.read(career.document.attach.path)

我收到以下错误:

更新 2

我仍在处理这个问题,但我认为根据我所阅读的所有内容,我需要在将回形针文件保存到模型之前提取回形针文件,所以我将弄清楚如何这样做,以便我可以发送已将简历作为附件上传。

经过几个小时的反复试验,我终于弄明白了,但很糟糕,因为它只有 1 行。基本上我所要做的就是将以下内容添加到我的 career_mailer.rb:

attachments[@career.document_file_name] = File.read(@career.document.path )

里面的document_file_name其实就是我的table里面paperclip保存文档名字的那一栏的名字。如果您将回形针用于文件、图像等,这可能会改变。我选择使用word文档。

这是对我有用的最终产品:

class CareerMailer < ApplicationMailer

  default from: "career@conciergenursingdirect.com"



  def career_inquiry(career)
    @career = career

    attachments['resume'] = File.read( @career.document.path ) 



    mail(to: "michele@conciergenursingdirect.com", subject: "This is just a test from Jay")
  end
end