【问题标题】:Rails Action Mailer: images in emailsRails Action Mailer:电子邮件中的图像
【发布时间】:2014-04-06 14:49:55
【问题描述】:

我正在尝试将图像粘贴到电子邮件中。问题是发送的电子邮件中没有图片

开发.rb

  config.action_mailer.default_url_options = {
    :host => 'localhost:3000',
    :only_path => false
  }
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.asset_host = 'http://localhost:3000'

查看文件:

<div class="image">
  <%= image_tag image_path('email-logo.png') %>
</div>

我在哪里做错了?如果您需要更多信息,请询问。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 actionmailer


    【解决方案1】:

    我不是 Rails 专家,但我找到了这个:

    1. 将您的图片 email-logo.png 放入 rails 项目的公共文件夹中;
    2. 在您的电子邮件中(例如views/devise/mailer/my_email.html.erb,像这样设置图像标签:

      <%= image_tag("http://#{ActionMailer::Base.default_url_options[:host]}:#{ActionMailer::Base.default_url_options[:port]}/email-logo.png") %>
      

    【讨论】:

      【解决方案2】:

      我同意 Utsav Kesharwani 的观点。 我最近遇到了同样的问题。由于 localhost 不公开,因此无法访问 localhost 的图像。

      因此,解决此问题的一种实用方法是将图像上传到云端,并在您想要显示这些图像时访问它们。

      我个人更喜欢Cloudinary——一种基于云的服务,提供端到端的图像管理解决方案,包括上传、存储、操作、优化和交付。

      您可以参考 Cloudinary 的 rails 文档:http://cloudinary.com/documentation/rails_integration#getting_started_guide

      我希望这会有所帮助。

      【解决方案3】:

      您从 localhost:3000 发送电子邮件,该电子邮件不公开(仅限于您的机器)。

      您必须公开您的本地环境,以便可以在您的邮件客户端中下载图像。

      使用ngrok 之类的服务来公开您的本地域。

      完成后,请务必更换 config.action_mailer.asset_host = 'http://localhost:3000'

      使用 ngrok URL(类似于config.action_mailer.asset_host = 'http://&lt;xxx&gt;.ngrok.com'

      此外,在您的视图文件中,您必须确保指定图像的绝对 url(而不仅仅是相对路径)。你可以在这里阅读更多内容:How do I get an absolute URL for an asset in Rails 3.1?

      【讨论】:

        【解决方案4】:

        试试

        <div class="image">
          <%= image_tag('email-logo.png') %>
        </div>
        

        确保您设置了config.action_controller.asset_hostconfig.action_mailer.asset_host

        【讨论】:

        • 确保您设置了config.action_controller.asset_hostconfig.action_mailer.asset_host,这样效果很好。
        • 设置这两个配置选项为我做到了!谢谢
        • 您不需要也可能不想设置config.action_controller.asset_host,因为它会为您的非电子邮件图像提供完整的 URL。只需将 config.action_mailer.asset_host 设置为 http://example.com 对我有用。
        【解决方案5】:

        如果你使用

        image_url('e-mail-logo.png')
        

        而不是

        image_path()
        

        它将渲染图像的绝对路径,这应该可以工作。图像的内部路径在消息的上下文中是没有意义的。

        【讨论】:

        • 问题不在于 image_tag,而在于您的电子邮件客户端无法访问本地主机
        猜你喜欢
        • 2013-05-21
        • 2013-04-14
        • 2015-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-06
        • 1970-01-01
        • 2012-07-25
        相关资源
        最近更新 更多