如何在不将此图像作为文件附加以供下载的情况下发送带有图像背景的电子邮件?

How to send email with image background without attach this image as a file for download?

我发送电子邮件,它如我所愿,但当电子邮件到达时,它附带了我用作邮件背景的附件

这就是我在电子邮件中添加图片的方式

<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'

然后在我们的电子邮件中,您应该使用 css 和完整的 url 图像:

 background-image:url('http://yoursite.com/public/images/background.jpg');

我建议在您的邮件程序中使用样式标签,只需像这样添加 class:

<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 是否支持它,您可能想要 have a look here