Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attaching remote URL file as attachment in action mailer

I have an ActionMailer setup to send an email with an attachment from S3. I'm doing so like this:

def job_apply(current_tenant, applicant)
   @current_tenant = current_tenant
   @applicant = applicant

   file = open(applicant.resume.url).read
   attachments[applicant.resume] = file

   email_with_name = "#{@current_tenant.name} <#{@current_tenant.help_email}>"
   mail(from: email_with_name, to: applicant.job.apply_email, subject: "New Job Application")
 end

The applicant.resume.url returns a correct URL which I can open up in the browser and retrieve the file with, however I am getting the following error:

Message undefined method `ascii_only?' for #<ApplicantResumeUploader:0x007fe6bc5c28a8>
File    /Users/cmalpeli/.rvm/gems/ruby-1.9.3-p194@job_board/gems/mail-2.4.4/lib/mail/encodings.rb
Line    103

This is the code in encodings.rb:

 98     def Encodings.decode_encode(str, output_type)
 99       case
 100       when output_type == :decode
 101         Encodings.value_decode(str)
 102       else
 103         if str.ascii_only?
 104           str
 105         else
 106           Encodings.b_value_encode(str, find_encoding(str))
 107         end
 108       end

I'm at a bit of a loss as to where to go from here...

Any help would be appreciated...

like image 817
cman77 Avatar asked Oct 05 '22 17:10

cman77


1 Answers

The problem is with your applicant.resume string in attachments[applicant.resume]. If this is not a string (like a JavaScript object) and is not a filename that can represent the file that you are attaching to the email, then this will cause problems for ActionMailer.

Verify that the applicant.resume string is indeed a filename like foo.pdf or resume.doc before proceeding.

like image 132
mccannf Avatar answered Oct 10 '22 01:10

mccannf