2012-02-23 51 views
4

我試圖將pdf附件發送到我的一個rails應用程序中,但附件是作爲文本而不是作爲正確的pdf發送的。Rails ActionMailer附件顯示爲亂碼文本

我的郵件看起來是這樣的:

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 

而我得到我的郵件收件箱:

-- 
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]> 

其次是ASCII字符

一個很長的字符串無需添加任何附件,該頭信息不會被髮送,而我只在我的收件箱中收到「消息正文」部分。我懷疑我的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 

我敢肯定有多餘的線條,但它的工作是爲最少。除了已將該文件複製到contact.html.erb之外,我還將contact.erb的視圖文件重命名爲contact.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 – 2014-11-13 15:08:40

6

僅供參考,我有同樣的問題,在我的情況下,解決辦法是換附件和郵件線。首先附上,然後致電郵件。

WRONG

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

GOOD

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)