2012-02-23 12 views
4

내 레일 애플리케이션 중 하나에서 PDF 첨부 파일을 보내려고하지만 첨부 파일이 적절한 pdf 대신 텍스트로 제공됩니다. 첨부 파일없이 ASCII 문자레일스 ActionMailer 첨부 파일이 깨진 텍스트로 표시됩니다.

의 매우 긴 문자열로 이어

-- 
Date: Thu, 23 Feb 2012 13:50:58 -0800 
Mime-Version: 1.0 
Content-Type: text/plain; 
charset=UTF-8 
Content-Transfer-Encoding: 7bit 
Content-ID: <[mailserver]> 

Message Body 

-- 
Date: Thu, 23 Feb 2012 13:50:58 -0800 
Mime-Version: 1.0 
Content-Type: application/pdf; 
charset=UTF-8; 
filename=invoice.pdf 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; 
filename=invoice.pdf 
Content-ID: <[mailserver]> 

:

내 우편물은 다음과 같습니다 : 내 메일받은 편지함에서 무엇을 얻을

class Notifications < ActionMailer::Base 
    def contact(email_params, sent_at = Time.now) 
    subject "" << email_params[:subject] 
    recipients "" << email_params[:client] 
    from "#{email_params[:name]} <#{email_params[:address]}>" 
    attachments['invoice.pdf'] = File.read("#{Rails.root.to_s}/public#{email_params[:attach]}") unless email_params[:attach].blank? 
    sent_on sent_at 
    body :message => email_params[:body], :sender_name => email_params[:name] 
    end 
end 

그리고 그 헤더 정보는 전송되지 않으며,받은 편지함에 "Message Body"부분 만 있습니다. 나는 내 mailer.rb의 첨부 파일 라인의 File.read() 부분이 어떻게 든 범인이라고 의심하지만 브라우저를 통해 액세스 할 수있는 내 서버의 유효한 pdf 파일을 가리키고 있습니다. 로그는 메일이 올바른 매개 변수와 함께 전송되었다고 말하고 있지만 일반 텍스트 편집기에서 pdf를 여는 경우 디 컴파일 된 텍스트 또는 이와 비슷한 것으로 전송됩니다.

메일 프로그램보기 contact.html.erb는 간단히 : <%= @message %>입니다. 도움이된다면.

내가 뭘 잘못하고 있을지 모르겠다.

감사합니다.

는 업데이트 :

분명히, 나는 나의 견해 디렉토리에 모두 contact.erb뿐만 아니라 contact.html.erb이 필요했습니다. 그냥 파일을 복사하고 이름을 바꾸면됩니다. 그러나 이제 메시지 본문은 비어 있습니다. 첨부 파일 만 표시됩니다.

답변

3

음,이 마지막 일 결국 :

class Notifications < ActionMailer::Base 
    def contact(email_params, sent_at = Time.now) 
    subject "" << email_params[:subject] 
    recipients "" << email_params[:client] 
    from "#{email_params[:name]} <#{email_params[:address]}>" 

    part "text/plain" do |p| 
     p.part :content_type => "text/plain", :body => render("contact.erb") 
    end 

    @message = email_params[:body] 

    attachments['invoice.pdf'] = File.read("#{Rails.root.to_s}/public#{email_params[:attach]}") unless email_params[:attach].blank? 

    sent_on sent_at 

    mail(:to => email_params[:client], 
     :from => email_params[:address], 
     :subject => email_params[:subject]) do |format| 
     format.html { render 'contact' } 
     format.text { render :text => email_params[:body] } 
    end 
    end 
end 

내가 외부 선이 확신 해요,하지만 적어도 같이 일하고있다. view.html.erb에 이미 파일을 복사 한 것 외에도 contact.erb에서 view.text.erb로보기 파일의 이름을 변경했습니다.

내가 내 컨트롤러에서 메소드를 호출하고 있습니다 : 그냥 참조 용

def send_mail 
    Notifications.deliver_contact(params[:email]) 
    redirect_to "/mail/success" 
    #Notifications.contact(params[:email]).deliver 
    end 
+0

이 정보는 도움이됩니다. http://stackoverflow.com/questions/4610689/email-attachments#comment42373470_4610864 –

6

, 나는 같은 문제가 있고, 내 경우에 해결책은 첨부 파일과 메일 라인을 교체하는 것이 었습니다. 먼저 첨부하고 메일을 보내십시오.

잘못

def pdf_email(email, subject, pdfname, pdfpath) 
    mail(:to => email, :subject => subject) 
    attachments[pdfname] = File.read(pdfpath) 
end 

좋은

def pdf_email(email, subject, pdfname, pdfpath) 
    attachments[pdfname] = File.read(pdfpath) 
    mail(:to => email, :subject => subject) 
end 
2

그래, 나는 최근에 같은 문제가 발생하고, 메일 내용 본문에 뭔가를 적는 후, 뭔가 같은 :

mail(:to => reciever, :subject => "Hello, this is a mail from rails!") do |formate| 
     formate.text {render :text => "mail content body "} 
    end.deliver 

첨부 파일이 정상적으로 표시되기 시작합니다.

0

나는 동일한 문제에 직면했지만 첨부 파일 및 메일 전달 순서를 변경하는 것은 저에게 효과적이지 않았습니다.

나를 위해 일한 것은 파일 이름에 .pdf 확장자를 추가하는 것입니다. 내 파일 이름에 이미 pdf 확장자가 있었음에도 불구하고 작업 메일러는 단순히 확장명을 읽지 않았습니다. 다시 추가하면 완벽하게 작동했습니다.

attachments[file_name + '.pdf'] = File.read(path) 
관련 문제