首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法让Rails 3 ActionMailer接受附件

无法让Rails 3 ActionMailer接受附件
EN

Stack Overflow用户
提问于 2012-02-11 07:34:44
回答 1查看 946关注 0票数 0

我正在将一个Rails 2.3.14应用程序迁移到Rails 3.0。在其中,邮件发送程序发送带有附件的消息。使用下面的代码,这在2.3.x中没有问题。

代码语言:javascript
复制
def notification(material, recipient, path_to_file)  
  enctype = "base64"

  @recipients  = recipient.email
  @from        = material.person.email
  @reply_to    = material.person.email
  @subject     = "New or updated materials: " + material.name
  @sent_on     = Time.now
  @content_type = "multipart/mixed"
  @headers['sender'] = material.person.email

  part    :content_type => "text/plain", 
          :body => render_message('notification', 
          :material => material, 
          :url =>  material.full_url_to_material)

  attachment  :content_type => "application" + "/" + material.file_type, 
              :body => File.read(path_to_file), 
              :filename => File.basename(material.file),
              :transfer_encoding => enctype, 
              :charset => "utf-8" if !!material.send_as_attachment

end

通过阅读Rails3.0 ActionMailer instructions,我将该方法修改为以下内容:

代码语言:javascript
复制
  def notification(material, recipient, path_to_file)
    @material = material
    @url = material.full_url_to_material
    attachments[material.file_file_name] = File.open(path_to_file, 'rb'){|f| f.read} if material.send_as_attachment?
    headers['sender'] = material.person.email
    mail(:to => recipient.email,
         :subject => "New or updated materials: " + material.name,
         :reply_to => material.person.email, 
         :from =>  material.person.email)
  end

MaterialMailer#notification在创建材质时调用。我有以下规范来测试它:

代码语言:javascript
复制
  it "will include the materials as an attachement with the the send_as_attachment field is set to 1" do
    it = Material.create(@materials_hash.merge(:send_notification => "1", :send_as_attachment => "1"))
    email =  ActionMailer::Base.deliveries[0] 
    email.body.should =~ Regexp.new("Name of the posted material: " + it.name )
    email.has_attachments?.should be_true
  end

正如我所提到的,这在2.3中工作得很好。现在,如果我将send_as_attachment标志设置为1,就会得到以下引用email.body.should行的错误:

代码语言:javascript
复制
  1) Material will include the materials as an attachement with the the send_as_attachment field is set to 1
     Failure/Error: email.body.should =~ Regexp.new("Name of the posted material: " + it.name )
       expected: /Name of the posted material: My material/
            got:  (using =~)
       Diff:
       @@ -1,2 +1 @@
       -/Name of the posted material: My material/

如果我更改规范并将send_as_attachment设置为0,则会出现以下错误,引用has_attachments?行:

1)物料将包括物料作为附件,并且send_as_attachment字段设置为1失败/错误: email.has_attachments?.should be_true expected为true

因此,包含附件以某种方式破坏了电子邮件。

我已经尝试了附加材料的其他方法:

代码语言:javascript
复制
  attachments[material.file_file_name] =  {:mime_type => "application" + "/" + material.file_content_type, 
              :content => File.read(material.file.path),  
              :charset => "utf-8"}

我已经尝试将文件路径硬编码到已知文件。但没那么走运。

还有没有其他地方我应该看看?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-12 01:01:43

根据Christopher的建议,下面是我用来让它工作的邮件和规范代码:

代码语言:javascript
复制
  def notification(material, recipient, path_to_file)
    @material = material
    @url = material.full_url_to_material
    attachments[material.file_file_name] = File.open(material.file.path, 'rb'){|f| f.read} if material.send_as_attachment?
    headers['sender'] = material.person.email
    mail(:to => recipient.email,
         :subject => message_subject("New or updated materials: " + material.name),
         :reply_to => material.person.email, 
         :from =>  material.person.email)
  end

  it "will include the materials as an attachment with the the send_as_attachment field is set to 1" do
    it = Material.create(@materials_hash.merge(:send_notification => "1", :send_as_attachment => "1"))
    Delayed::Worker.new(:quiet => true).work_off
    email =  ActionMailer::Base.deliveries[0] 
    email.parts.each.select{ |email| email.body =~ Regexp.new("Name of the posted material: " + it.name )}.should_not be_empty
    email.has_attachments?.should be_true
  end

在规范中,我必须检查电子邮件的每个部分的正文,因为附件是第一部分还是第二部分并不一致。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9236342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档