Rails save attachment using CloudMailIn and Paperclip to S3

435 views Asked by At

I a Rails app that handles Customer Tickets (worequests). The Tickets have Comments back and forth between the Customer and an Employee. The Comment can have an attachment. It's running on Heroku and PaperClip is storing the attachments in S3.

When a new Comment is created and email is sent to both the Customer and the Employee assigned to the Ticket.

I'm using CloudMailIn so that the Customer or Employee can respond to a Comment email with a new Comment.

So far that works fine!

But, I would like to allow the returning email to contain a single attachment.

This is the working incoming mails controller:

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

The log results for Rails.logger.info params includes this:

"envelope"=>{"to"=>"[email protected]", "recipients"=>{"0"=>"[email protected]"}, "from"=>"[email protected]", "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 is a hash and “0” is the name of the key. params[:attachments][‘0’]

I can access these fields fine:

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

But, how do I set the PaperClip :attach file?

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

This is my current attempt:

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
)

The :attach => params[:attachments]['0'].path, is wrong.

Getting this:

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

I don't know what to use ?????

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

Thanks for the help!

1

There are 1 answers

1
Reddirt On BEST ANSWER

This worked:

        :attach => params[:attachments]['0'],