9
votes

I have a trouble with sending email message with attachment using ActionMailer.

The thing is my attachment has 'noname' filename when I reading my message in gmail.

Notifier function

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

Message headers:

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: ...

How can I send a message with right filename?

Thanks

3

3 Answers

11
votes

Make sure you have your views.

Make the correct files in app/views/[class_name]/[method_name]
Create a app/views/notifier/send_message.erb file and app/views/notifier/send_message.html.erb file.

1
votes

Drew is right, this problem occur if specific mailer view is missing.

IMHO, gmail does not get the encoding of message and renames attachments.

More info is available in Gmail help:

0
votes

I think when you don't define a body for the email, the parts get improperly set up, and you end up win a massive "noname" file that includes the parts headers for all of the attachments.

Using this mailer code:

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

I get a noname single file when I do this:

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

I get 2 individual files, with correct names, when I do this:

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