2012-02-10 57 views
0

我正在將Rails 2.3.14應用程序遷移到Rails 3.0。其中,郵件發送附件的消息。使用下面的代碼,這在2.3.x中沒有問題。無法讓Rails 3 ActionMailer接受附件

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 

通過Rails 3.0 ActionMailer instructions閱讀,我已修改方法爲以下:在創建時材料

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#通知被調用。我有以下規格來測試:

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標誌之一,我得到以下錯誤,引用email.body.should行:

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。應be_true 預計假爲真

所以包括附件以某種方式破壞了電子郵件。

我曾嘗試其他方法用於材料附着:

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

我試圖硬編碼的文件路徑已知的文件。但沒有運氣。

任何我應該看的地方?

+1

諷刺的諷刺,完成這個作品後,我只是想出了它。我需要查看電子郵件正文的第一部分。 – TDH 2012-02-10 23:36:09

+0

mail.parts [0] .body.should =〜Regexp.new(「發佈材料的名稱:」+ it.name) – TDH 2012-02-10 23:36:16

+1

您應該發佈自己的答案,以便其他遇到此問題的人可以找到它:} – 2012-02-10 23:38:21

回答

0

每克里斯托弗的建議,這裏是我用得到它的工作郵件和規格代碼:

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 

在規範,我必須檢查每個電子郵件的部位的身體,因爲附件是第一部分還是第二部分並不一致。