在操作邮件程序中附加远程URL文件作为附件

时间:2012-12-09 21:49:19

标签: ruby-on-rails actionmailer

我有一个ActionMailer设置,用于发送带有S3附件的电子邮件。我是这样做的:

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

applicant.resume.url返回一个正确的URL,我可以在浏览器中打开并检索文件,但是我收到以下错误:

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

这是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

我从这里离开的地方有点不知所措......

任何帮助将不胜感激......

1 个答案:

答案 0 :(得分:4)

问题在于applicant.resume中的attachments[applicant.resume]字符串。如果这不是一个字符串(如JavaScript对象),并且不是可以表示您附加到电子邮件的文件的文件名,那么这将导致ActionMailer出现问题。

在继续操作之前,请确认applicant.resume字符串确实是foo.pdfresume.doc等文件名。

相关问题