【问题标题】:Images Not Loading in Email from Rails App图像未从 Rails 应用程序加载到电子邮件中
【发布时间】:2019-10-04 20:58:32
【问题描述】:

有一些图像存储在我的 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>

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

【问题讨论】:

  • 使用 image_url 代替 image_tag。 image_url('banner.png')
  • @ShabiniRajadas 这会打印出我邮件中的整个公共 url 路径,但不会呈现图像
  • 只要使用image_tag('banner.png')..
  • @GokulM 那也没有帮助
  • 在环境下将 config.action_mailer.default_url_options = { :host => your host } 添加到你的 development.rb(或者如果你在 production.rb 中使用生产模式 thaen)并使用 image_tag('banner. png')

标签: ruby-on-rails actionmailer


【解决方案1】:

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

例如。

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 内嵌图片。

【讨论】:

    【解决方案2】:

    Action Mailer Rails Guides 中所述。

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

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

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

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

    <%= image_tag 'image.jpg' %>
    

    【讨论】:

    • 这个答案解决了我的问题并且快速/简单
    猜你喜欢
    • 1970-01-01
    • 2019-11-07
    • 2015-10-02
    • 2017-11-15
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多