Send paperclip file via email as an attachment - Rails

Jermaine Subia

I have a form that when a potential hire is interested in working for the company he/she fills it out and attaches a resume. The submission will go to a company representative with the attachment. The email is going through but the attachment is not the document and I cannot figure out how to configure it properly. In the submission email the attachment just says "document".

career_mailer.rb

class CareerMailer < ApplicationMailer

  default from: "[email protected]"



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

career.rb (model)

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

UPDATE

I am trying the following in my career mailer:

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

I am getting the following error:

error

UPDATE 2

I am still working on this but I think based on everything I've read I need to pull the paperclip file before it saves to the model so I am going to figure out how to do so so I can send the uploaded resume as an attachment.

Jermaine Subia

After hours of trial and error I finally figured it out and it sucks cause it was only 1 line. Basically all I had to do was add the following to my career_mailer.rb:

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

The document_file_name is actually the name of the column in my table where paperclip saves the name of the document. This could change if you used paperclip for file, image, et cetera. I chose to use the word document.

This is the final product that worked for me:

class CareerMailer < ApplicationMailer

  default from: "[email protected]"



  def career_inquiry(career)
    @career = career

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



    mail(to: "[email protected]", subject: "This is just a test from Jay")
  end
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java send email with file in attachment

single model to have two file attachment columns - Rails 4 + Paperclip

Python Sendgrid send email with PDF attachment file

Python cannot find file to send as email attachment

How to send excel file as an attachment in email in laravel?

SSIS Send Email File Attachment Failure

Rails 4 - Wicked pdf file send via email

Rails: Send email using Gmail API with attachment return only encoded file not

Rails send email via GET

C# - Send email with inline attachment WITHOUT Outlook's paperclip icon?

Send attachment file in outgoing mail body in rails

Send email with an attachment

send a html email with attachment

send email with attachment in CodeIgniter

Python send email with an attachment

VBA - send email with attachment

How do I send a generated csv file as an email attachment in React

Sendmail command to send a file as an email body as well as attachment

Create ics file and send email with Attachment using c#

Trying to send email using aws ses and attach zip file as attachment

Android send email with multiple attachment using file provider

using phpmailer class to send attachment in email: Could not access file

How to write a new CSV file and send as Email attachment

Move file to other folder and send the folder URL on email attachment

Rails send email via pop up

Download link on Paperclip Attachment in Rails_admin

Sending a file via email - cannot get attachment to work - Android

Django rest framework: sendgird email with attachment without model only filefield to open file and send button to send email

Rails ActionMailer - Attaching text file causes email body to be sent as attachment

TOP Ranking

HotTag

Archive