【问题标题】:Rails embed image in emailRails 在电子邮件中嵌入图像
【发布时间】:2015-01-29 22:52:20
【问题描述】:

在 Rails 应用程序中,我正在尝试将图像(徽标)嵌入(内联)到电子邮件中。

这是 invoice.mailer.rb:

class InvoiceMailer < ActionMailer::Base
  def invoice_email(invoice)
    @invoice = invoice
    @recipients = @invoice.workorder.client.contacts.where(:receiveinvoices => true)
    tomail = @recipients.collect(&:email).join(",")
    frommail = @invoice.tenant.from_email
    copymail = @invoice.tenant.copy_email
    attachments.inline['AME_Logo.gif'] = File.read( Rails.root.join('app/assets/images/AME_Logo.gif') )

    mail(
        :to => tomail,
        :bcc => copymail,
        :from => frommail,
        :subject => "New Invoice from " + @invoice.tenant.name

    )
  end
end

这是 _invoice_email.html.erb:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <style type="text/css" media="screen">
  </style>
</head>
<body style="margin-left: auto; margin-right: auto; width: 700px">
<div class="invoice">
<div class="inv_header" style="margin-left:10px; margin-right: auto; width: 650px">
  <h4>Invoice</h4>
  <%= image_tag attachments['AME_Logo.gif'].url -%>
</div>

我从 image_tag 代码行收到此错误:

undefined local variable or method `attachments' for #<#<Class:0x007f8a49ce7b08>:0x007f8a4e352110>

感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    你为什么不在你的 image_tag 助手中硬编码路径?

    <%= image_tag('http://path-to-your-site/assets/AME_Logo.gif') %>
    

    如果必须是附件,则必须将其设为实例变量:

    @attachments.inline['AME_Logo.gif'] = File.read( Rails.root.join('app/assets/images/AME_Logo.gif') )
    

    在您的邮件中:

    <%= image_tag @attachments['AME_Logo.gif'].url -%>
    

    【讨论】:

    • 我试过&lt;%= image_tag("/images/AME_Logo.gif", :class =&gt; 'pull-right padding-right') %&gt; - 图片是附件,但不是内联的。
    猜你喜欢
    • 2011-10-06
    • 2014-10-03
    • 2015-03-14
    • 2011-10-30
    • 2017-11-05
    • 2010-12-18
    • 2018-12-24
    • 2019-05-20
    相关资源
    最近更新 更多