2010-12-19 14 views
9

ActionMailerを使用して添付ファイル付きの電子メールメッセージを送信する際に問題があります。電子メールに無効なファイル名(ActionMailer)

私の添付ファイルは、Gmailで自分のメッセージを読むときに 'noname'というファイル名があります。

通知機能

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」と名付けて、より明確にすることができます。 –

+0

なぜ同じことをする2つの別々のファイルがあるのですか? – Trip

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 1つのファイルを取得する:

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 
関連する問題