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

cma*_*n77 5 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
Run Code Online (Sandbox Code Playgroud)

申请人.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
Run Code Online (Sandbox Code Playgroud)

这是 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
Run Code Online (Sandbox Code Playgroud)

我对从这里去哪里有点不知所措......

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

mcc*_*nnf 4

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

在继续之前,请验证该applicant.resume字符串确实是一个文件名,如foo.pdf或。resume.doc