【问题标题】:Unable to render inline attachments in ActionMailer无法在 ActionMailer 中呈现内联附件
【发布时间】:2016-04-08 12:44:34
【问题描述】:

我正在尝试使用Rails attachments inline are not shown correctly in gmail 此处概述的方法在标准 ActionMailer 视图中呈现内联图像。

我的邮件中的代码:

attachments.inline["sample.png"] = {
  mime_type: "image/png",
  encoding: "base64",   # Including this line causes byte sequence error
  data: File.read('public/sample.png')
}

在邮件视图中:

image_tag(attachments["sample.png"].url)

给出熟悉的 ruby​​ UTF-8 字节序列错误:

invalid byte sequence in UTF-8

为了解决这个问题,我尝试了以下方法:

attachments.inline["logo.png"] = {
      mime_type: "image/png",
      data: Base64.encode64(File.read('public/logo.png')) 
    }

还有

attachments.inline["logo.png"] = File.read('public/logo.png')

使用与上述相同的 image_tag 语法。

这两个都解决了 UTF 错误,但我在视图中留下了这个无意义的 URL:

<img src="cid:5707a64ededbc_7bd83ffd648601e029875@localhostname.mail">

PNG 图像是有效的,并且可以在标准 HTML 视图中正确呈现。我正在使用带有 Ruby 2.2.4 的 Rails 4.2.5

编辑

这行得通:

邮寄者:

attachments.inline["cape.png"] = {
  mime_type: "image/png",
  # encoding: "base64",
  content: Base64.encode64(File.read(Rails.root.join("public/", "cape.png")))
}

查看:

= image_tag "data:image/png;base64,#{attachments['logo.png'].read}"

但是非常尴尬,我仍然想知道为什么传统方法不起作用。

【问题讨论】:

  • 在图片网址前添加Rails.root_path
  • 谢谢,试过了。它能够很好地找到图像,因此添加 Rails.root 并不能解决问题。
  • 尝试在图片路径之前添加您的实时服务器网址
  • 没有变化。尽管检查attachments.inline["logo.png"] 对象显示Content-ID: <5707ade638337_7bd83ffd648601e0346d1@localhostname.mail>attachments.inline[logo.png].url 给出的内容相同。
  • 遇到了同样的问题。感谢您的解决方案!但是,我觉得自己很沮丧。

标签: ruby-on-rails ruby actionmailer


【解决方案1】:

在我的应用程序中我只使用

attachments.inline["logo.png"] = File.read('public/logo.png')

对我来说很好用

【讨论】:

  • 谢谢,已经尝试过了,但正如我在上面的问题中解释的那样,image_tag 中的 URL 无法正确显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-15
  • 2014-01-15
  • 2011-07-01
  • 1970-01-01
相关资源
最近更新 更多