首页 > 解决方案 > 回形针图像未显示在 Action Mailer 电子邮件中

问题描述

我正在编写一个非常简单的电子邮件模板,它发送带有嵌入图像的 HTML 电子邮件。图像使用 Paperclip gem 存储在 AWS S3 中:

<%= image_tag @press_release.image.try(:attachment).try(:url, :preview), alt: 'zimplePR', style: 'width: 85%; max-height: 200px; object-fit: cover;' %>

在生产中,该image_tag助手在我的 web 视图中充当了一个魅力,但由于某种原因,它没有显示在我发送的电子邮件中。

我在某处读到可能是因为 Paperclip 的完整附件 url 类似于//s3.amazonaws.com/...并且在前面加上“http:”应该可以,但这仅适用于 Gmail,但不适用于 Apple 电子邮件。

有没有办法使用 Paperclip 将图像嵌入到 ActionMailer 模板中并显示在所有电子邮件客户端中?

标签: amazon-s3ruby-on-rails-5paperclipactionmailer

解决方案


我发现了问题,这个错误的原因是因为我没有指定附件的 :http 协议:

Class Image < ApplicationRecord
  has_attached_file :attachment,
                    :styles => {
                      :thumb   => ['80x80>', :png],
                      :preview => ['600x600>', :png]
                    },
                    :convert_options =>{
                      :thumb   => "-quality 80 -interlace Plane",
                      :preview => "-quality 80 -interlace Plane",
                    },
                    :default_url => 'images/missing_:style.png',
                    :s3_protocol => :https,
                    :s3_credentials => {
                      :bucket => Rails.application.credentials[:aws][:s3_bucket_production]
                    }
end 

推荐阅读