2010-12-19 39 views
9

我在使用ActionMailer發送包含附件的電子郵件時遇到了麻煩。電子郵件中的文件名無效(ActionMailer)

事情是我的附件有'noname'文件名,當我在Gmail中閱讀我的消息。

通知功能

class Notifier < ActionMailer::Base 
    def send_message 
    attachments["text.txt"] = {:content => "hello"} 
    mail(:to => "[email protected]", :subject => 'test') 
    end 
end 

郵件標題:

 
Date: Sun, 19 Dec 2010 23:18:00 +0100 
Mime-Version: 1.0 
Content-Type: text/plain; 
charset=UTF-8; 
filename=text.txt 
Content-Transfer-Encoding: 7bit 
Content-Disposition: attachment; 
filename=text.txt 
Content-ID: ... 

我怎麼能發送帶有正確的文件名的消息?

謝謝

回答

11

確保您有意見。

進行正確的文件app/views/[class_name]/[method_name]
創建app/views/notifier/send_message.erb文件和app/views/notifier/send_message.html.erb文件。

+0

實際上,您可以將視圖命名爲'send_message.text.erb'以使其更清晰。 – 2011-10-05 00:28:02

+0

爲什麼有兩個單獨的文件做同樣的事情? – Trip 2014-07-29 22:24:00

0

我認爲當你沒有定義的電子郵件的主體,各部分得到設置不當,最終贏得一個龐大的「noname」文件,其中包含所有附件的部分頭文件。

使用這個郵件代碼:

class Mailer < ActionMailer::Base 
    def generic(args) 
    args.reverse_merge! to: '[email protected]', from: '[email protected]' 
    add_attachments! args.delete(:attachments) 
    mail(args) 
    end 

    protected 
    def add_attachments!(*files) 
    files.flatten.compact.each do |file| 
     attachments[File.basename(file)] = File.read(file) 
    end 
    end 
end 

,我收到了NONAME單個文件當我這樣做:

Mailer.generic(attachments: [File.open('/path/to/file1.txt'), File.open('/path/to/file2.csv')]).deliver 

我得到2個個人檔案,以正確的名字,當我這樣做:

Mailer.generic(body: '', attachments: [File.open('/path/to/file1.txt'), File.open('/path/to/file2.csv')]).deliver 
相關問題