无法在ActionMailer中呈现内联附件 [英] Unable to render inline attachments in ActionMailer

查看:62
本文介绍了无法在ActionMailer中呈现内联附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此处概述的方法在标准ActionMailer视图中渲染内嵌图像 Rails附件内联在gmail中未正确显示

I'm trying to render inline images in a standard ActionMailer view using the approach outlined here Rails attachments inline are not shown correctly in gmail.

我在邮件中的代码:

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字节序列错误:

Gives the familiar ruby UTF-8 byte sequence error:

UTF-8中无效的字节序列

要解决此问题,我尝试了以下操作:

To get around this I tried the following:

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

>

and also

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

使用与上面所示相同的 image_tag 语法。

Using the same image_tag syntax shown above.

这两个都解决了UTF错误,但视图中剩下这个荒谬的URL:

Both of these resolve the UTF error, but I'm left with this nonsensical URL in the view:

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

PNG图像有效且可以在标准格式下正确渲染HTML视图。我在Ruby 2.2.4中使用Rails 4.2.5

The PNG image is valid and renders properly in a standard HTML view. I'm using Rails 4.2.5 with Ruby 2.2.4

编辑

这有效:

邮件程序:

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}"

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

Very awkward, however, and I'm still wondering why the conventional approach doesn't work.

推荐答案

在我的应用程序中,我仅使用

In my application I use only

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

对我来说很好

这篇关于无法在ActionMailer中呈现内联附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆