简体   繁体   中英

Email attachment for document files using rails action mailer

I am using Rails action mailer. I need to send a email with document(doc, docx, txt) as attachment. When I am attaching .txt files, the content in that txt file are viewing properly. But when I am trying to attach .doc or docx files, the content in that files are not displaying(it is converting into encoding format) Why is that happening. Is there any way to attach document files in email using rails action mailer.

def send_email(email, file)
  @email = email
  attachments['document.docx'] = File.read(file.path)
  mail(from: @email, to: "aa@gmail.com", subject: "Document attachment")
end

original document attached

document received in email

Please guide me, if I am going wrong in some place.

Thanks in advance.

Try reading the file in binary mode (see binary file mode & :mode option :

attachments['document.docx'] = File.read(file.path, mode: 'rb')

In my case, Rails was adding a carriage return (aka "CR", or "\\r") to every line in a CSV file that ended with either a "\\n" (newline) or "\\r".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM