1

我是一个处理客户工单(工作请求)的 Rails 应用程序。工单在客户和员工之间来回评论。评论可以有附件。它在 Heroku 上运行,PaperClip 将附件存储在 S3 中。

当创建新评论并向客户和分配给工单的员工发送电子邮件时。

我正在使用 CloudMailIn,以便客户或员工可以使用新评论回复评论电子邮件。

到目前为止,效果很好!

但是,我想允许返回的电子邮件包含一个附件。

这是正在工作的传入邮件控制器:

class IncomingMailsController < ApplicationController
  skip_before_filter :verify_authenticity_token

  def create
    Rails.logger.info params
    worequest = params[:envelope][:to].split('@')[0]
    contents = params[:plain].split('---')[0]
    message = Comment.new(
        :worequest_id  => worequest,
        :user_id => User.find_by_email(params[:envelope][:from]).id,
        :comments => contents,
        :tenant_id => 1
    )
    if message.save
      render :text => 'Success', :status => 200
    else
      render :text => message.errors.full_messages, :status => 422, :content_type => Mime::TEXT.to_s
    end
end

的日志结果Rails.logger.info params包括:

"envelope"=>{"to"=>"60@mail.myapp.com", "recipients"=>{"0"=>"60@mail.myapp.com"}, "from"=>"someguy@gmail.com", "helo_domain"=>"mail-wi0-f175.google.com", "remote_ip"=>"xxx.xx.xxx.xxx", "spf"=>{"result"=>"temp_error", "domain"=>"mydomain.com"}}, 

"attachments"=>{"0"=>#<ActionDispatch::Http::UploadedFile:0x00000007194040 @original_filename="five guys.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachments[0]\"; filename=\"five guys.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20140115-5-1n2rb24>>}, "action"=>"create", "controller"=>"incoming_mails"} 

attachments 是一个散列,“0”是密钥的名称。params[:attachments][‘0’]

我可以很好地访问这些字段:

Rails.logger.info params[:envelope][:from]
Rails.logger.info params[:envelope][:to]
Rails.logger.info params[:attachments]['0'].original_filename
Rails.logger.info params[:attachments]['0'].content_type

但是,如何设置 PaperClip:attach文件?

:attach => params[:attachments]['0'].tempfile  ?
:attach => params[:attachments]['0'].read  ?

这是我目前的尝试:

attach = Attachment.new(
    :comment_id  => 346,
    :name => "Email Attachment",
    :attach_file_name => params[:attachments]['0'].original_filename,
    :attach_content_type =>  params[:attachments]['0'].content_type,
    :attach => params[:attachments]['0'].path,
    :tenant_id => 1
)

:attach => params[:attachments]['0'].path,是错的。

得到这个:

  Paperclip::AdapterRegistry::NoHandlerError (No handler found for "/tmp/RackMultipart20140115-13-tcpvtw"): 

不知道用什么????

:attach => params[:attachments]['0'].read,
:attach => params[:attachments]['0'].path.to_file,
:attach => params[:attachments]['0'].path.read,

谢谢您的帮助!

4

1 回答 1

2

这有效:

        :attach => params[:attachments]['0'],
于 2014-01-16T23:05:55.073 回答