ruby-on-rails - 如何发送带有图像背景的电子邮件而不将此图像附加为文件以供下载?
问题描述
我发送电子邮件,它正在按我的意愿工作,但是当电子邮件到达时,它附带了我用作消息背景的附加图像
这就是我在电子邮件中添加图像的方式
<div style="background-image:url('<%= email_bg_tag("bg-email.jpg") %>');background-position:top center;background-repeat:no-repeat;background-color:transparent;">
这是我的 email_helper.erb
module EmailHelper
def email_bg_tag(image)
attachments[image] = File.read(Rails.root.join("app/assets/images/#{image}"))
attachments[image].url
end
end
我的图片在assets/image
解决方案
跳过静态图像的资产管道以在您的电子邮件中工作,您应该将它们放入:
Rails.root + 'public/images'
然后在我们的电子邮件中,您应该使用带有完整 url 的 css 到图像:
background-image:url('http://yoursite.com/public/images/background.jpg');
我建议在您的邮件中使用样式标签,然后像这样添加类:
<style>
.my-fancy-background {
background-image: url('http://yoursite.com/public/images/bg-email.jpg');
background-position: top center;
background-repeat: no-repeat;
background-color: transparent;
}
</style>
<div class="my-fancy-background">
Here is my email div with some fancy background
</div>
但是如果你仍然想要辅助方法,你可能还需要在 application.rb 中设置它
config.action_mailer.asset_host = 'http://example.com/public/'
# use public url of your app where your static images can be served from.
更新
根据 gwally 的评论,Outlook 可能不支持此功能,因此如果您关心 Outlook 是否支持此功能,您可能想看看这里
推荐阅读
- python - Docker-Compose 在不同的容器中运行 NodeJS 和 Python3
- python - 更改数据框中列的日期格式
- swift - 如何将当前日期与用户输入日期进行比较?
- date - Formik Yup 日期验证 - 接受欧洲 DD/MM/YYYY 格式
- c# - 从超过 100 亿个数字的双打流中计算运行中位数
- gradle - 停止执行 finalizedBy 任务,或仅在某个条件下执行后续任务
- c++ - 改进解决方案
- react-native - React Native RNDateTimePicker:无法选择日期
- python - 删除 txt 文件中特定字符之间的所有内容
- python - 为什么gdb和python中的十六进制地址添加不一样