2010-12-19 4 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/notifier/send_message.erb 파일 app/views/notifier/send_message.html.erb 파일을 만듭니다 app/views/[class_name]/[method_name]
올바른 파일을 확인하십시오.

+0

실제로보기를 'send_message.text.erb'라는 이름으로 지정하면 더 명확 해집니다. –

+0

동일한 작업을 수행하는 두 개의 개별 파일이있는 이유는 무엇입니까? – Trip

0

내가 생각 부적절하게 설정하면 모든 첨부 파일의 부품 헤더가 포함 된 방대한 "noname"파일이 생성됩니다.

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

나는이 작업을 수행 할 때이 올바른 이름이 개 개별 파일을 얻을 : 나는이 작업을 수행 할 때

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(body: '', attachments: [File.open('/path/to/file1.txt'), File.open('/path/to/file2.csv')]).deliver 
관련 문제