Himberjack
Himberjack

Reputation: 5792

ActionMailer throws an error while attaching files

I have ActionMailer working and sending plain emails. But when I try to attach a file, I get: #<Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8> - nil

My snippet for doing that is:

mail(subject: "test test",
         from: "[email protected]",
         to: ["[email protected]"],
         date: Time.now,
         content_type: "text/html",
         attachments[file[:filename]] => file[:data] )

Any suggestion what may go wrong?

Thanks!

Upvotes: 0

Views: 309

Answers (2)

Simone Carletti
Simone Carletti

Reputation: 176352

This error is normally generated when you try to attach a file handler without using binary mode but the file is not a text file.

I can't see in the code where you create the file handler. You should use wb to open it.

File.open(path, 'wb')

instead of

File.open(path, 'w')

Keep in mind that if the attachment is not a text file, the content_type also is wrong.

What type of file where you attaching?

Upvotes: 2

Pavel S
Pavel S

Reputation: 1543

Try this

class Mailer < ActionMailer::Base

def my_mailing_method
  attachments[file[:filename]] => file[:data]
  mail(subject: "test test",
         from: "[email protected]",
         to: ["[email protected]"],
         date: Time.now,
         content_type: "text/html")
end

Upvotes: 1

Related Questions