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

时间:2016-10-19 08:06:27

标签: ruby-on-rails actionmailer

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

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

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

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

My Mailer View如下所示:

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

我在这里做错了什么?在原始电子邮件源中,我看到图像标签也引用了正确的指纹文件,但它似乎仍然显得破碎。有任何帮助吗?

2 个答案:

答案 0 :(得分:2)

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

例如。

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

答案 1 :(得分:1)

与控制器不同,邮件程序实例没有任何关于传入请求的上下文,因此您需要自己提供:asset_host参数。

由于:asset_host通常在整个应用程序中是一致的,您可以在config / application.rb中全局配置它:

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

现在,您可以在电子邮件中显示图像。

<%= image_tag 'image.jpg' %>