Rails Action Mailer:电子邮件中的图像

Pav*_*bin 21 ruby-on-rails actionmailer ruby-on-rails-4

我正在尝试将图像粘贴到电子邮件中.问题是电子邮件内部没有图像

development.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'
Run Code Online (Sandbox Code Playgroud)

查看文件:

<div class="image">
  <%= image_tag image_path('email-logo.png') %>
</div>
Run Code Online (Sandbox Code Playgroud)

我在哪里弄错了?请询问您是否需要更多信息.

a14*_*14m 20

尝试

<div class="image">
  <%= image_tag('email-logo.png') %>
</div>
Run Code Online (Sandbox Code Playgroud)

确保你设置config.action_controller.asset_hostconfig.action_mailer.asset_host

  • 确保你设置`config.action_controller.asset_host`和`config.action_mailer.asset_host`,这很有效. (2认同)
  • 你不需要也可能不想设置 `config.action_controller.asset_host`,因为它会给你的非电子邮件图像提供完整的 URL。只需将`config.action_mailer.asset_host` 设置为`http://example.com` 之类的东西就对我有用。 (2认同)

Uts*_*ani 9

您发送的电子邮件localhost:3000不可公开(仅限于您的计算机).

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

使用像ngrok这样的服务来公开您的本地域.

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

使用ngrok URL(类似的东西config.action_mailer.asset_host = 'http://<xxx>.ngrok.com')

此外,在视图文件中,您必须确保为图像指定绝对URL(而不仅仅是相对路径).您可以在此处阅读更多内容:如何在Rails 3.1中获取资产的绝对URL?