图片未从 Rails 应用的电子邮件中加载

Images Not Loading in Email from Rails App

有一些图像存储在我的 rails assets/images 文件夹中。我需要在从我的 rails 应用程序发送的电子邮件中显示这些图像。我可以发送电子邮件,但图片显示为损坏的链接。

我的development.rb文件如下所示:

config.action_mailer.asset_host = 'http://localhost:3000'

config.action_controller.asset_host = 'http://localhost:3000'

我的邮件视图如下所示:

<div class = "container">
  <%= image_tag(asset_path('com_name.jpg')) %>
</div>

<div class = "container">
    <%= image_tag(asset_path('banner.png')) %>
</div>

<h4>Hi! This mail is to inform you that your request has been successfully processed and the processed output has been attached along with your mail for your perusal</h4>

<h4>Looking forward to serve you once again.</h4>

<h5>Thanks,</h5>
<h4>Team Introhive</h4>

我在这里做错了什么?在原始电子邮件来源中,我也看到图像标签引用了正确的指纹文件,但它似乎仍然出现 broken.Any 请帮忙?

Action Mailer Rails Guides 中所述。

Unlike controllers, the mailer instance doesn't have any context about the incoming request so you'll need to provide the :asset_host parameter yourself.

As the :asset_host usually is consistent across the application you can configure it globally in config/application.rb:

config.action_mailer.asset_host = 'http://example.com'

Now you can display an image inside your email.

<%= image_tag 'image.jpg' %>

请尝试以下操作, 在邮件方法中,

例如

class NotifierMailer < ApplicationMailer
  def welcome(recipient)
    attachments.inline['photo.png'] = File.read('path/to/photo.png')
    mail(to: recipient, subject: "Here is what we look like")
  end
end

在您看来,使用以下内容,

<%= image_tag attachments['photo.png'].url -%>

如果你想传递任何选项,你可以使用这个,

<%= image_tag attachments['photo.png'].url, alt: 'Our Photo', class: 'photo' -%>

请查看有关内联图像的文档 http://api.rubyonrails.org/classes/ActionMailer/Base.html