【问题标题】:Email attachment for document files using rails action mailer使用 rails action mailer 的文档文件的电子邮件附件
【发布时间】:2019-06-19 06:39:42
【问题描述】:

我正在使用 Rails 动作邮件程序。我需要发送一封带有文档(doc、docx、txt)作为附件的电子邮件。当我附加 .txt 文件时,该 txt 文件中的内容可以正常查看。但是当我尝试附加 .doc 或 docx 文件时,该文件中的内容没有显示(它正在转换为编码格式)为什么会发生这种情况。有没有办法使用 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

如果我在某个地方出错了,请指导我。

提前致谢。

【问题讨论】:

  • 你可以尝试将 mimetype 设置为 zip 即attachments['document.docx'] = { mime_type: 'application/zip', content: File.read(file.path) }
  • 用 mime_type 试过也没有用 :-(

标签: ruby-on-rails email actionmailer


【解决方案1】:

尝试以二进制模式读取文件(见binary file mode & :mode option:

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

在我的例子中,Rails 在 CSV 文件中以“\n”(换行符)或“\r”结尾的每一行都添加了一个回车符(又名“CR”或“\r”)。

【讨论】:

    猜你喜欢
    • 2013-04-14
    • 2014-04-06
    • 2013-05-21
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    相关资源
    最近更新 更多