2012-02-10 2 views
0

Rails 2.3.14 애플리케이션을 Rails 3.0으로 마이그레이션하고 있습니다. 그것에서, 우편물은 부착을 가진 메시지를 보낸다. 아래 코드를 사용하면 2.3.x에서 문제없이 작동합니다. 레일 3.0 ActionMailer instructions을 통해 읽기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 

, 나는 다음에 방법을 수정 한 : 재료가 생성 될 때

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 라인 참조, 다음과 같은 오류가 발생합니다 : 내가 사양을 변경하고 0으로 send_as_attachment을 설정하면

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/ 

를, 나는 오류를 다음 얻을 has_attachments를 참조 하시겠습니까? 라인 : 을 be_true .should email.has_attachments

는 그래서 등의 사실이 거짓 예상 :

1) 재질이 (가) send_as_attachment 필드와 함께 첨부 파일로 자료를 포함 1 개 장애/오류로 설정 첨부 파일이 어떻게 든 이메일을 위반합니다.

나는 재료를 부착하기위한 다른 방법을 시도 : 나는 알려진 파일의 파일 경로를 하드 코딩하는 시도

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

. 그러나 운이 없다.

어디에서 봐야합니까?

+1

아이러니 한 아이러니,이 오푸스를 마친 후에, 나는 그것을 알아 냈습니다. 나는 시체에 대한 이메일의 첫 부분을 볼 필요가있다. – TDH

+0

mail.parts [0] .body.should = ~ Regexp.new ("게시 된 자료의 이름 :"+ it.name) – TDH

+1

당신이 그것을 얻은 방법에 대한 답을 게시하여 다른 사람들이이 자료를 찾을 수 있도록하십시오 그것 :} –

답변

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 

을 사양, 나는, 이메일의 각 부분의 몸을 확인했다 그것이 첨부 파일이 첫 번째 부분인지 두 번째 부분인지에 대해 일관성이 없었기 때문에.

관련 문제