Reddirt
Reddirt

Reputation: 5953

Rails embed image in email

In a Rails app, I'm trying to embed (inline) an image (logo) into an email.

This is the 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

This is the _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>

I'm getting this error from the image_tag code line:

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

Thanks for your help!

Upvotes: 0

Views: 2197

Answers (1)

NM Pennypacker
NM Pennypacker

Reputation: 6942

Why don't you just hard-code the path in your image_tag helper?

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

If it must be an attachment, you have to make it an instance variable:

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

and in your mailer:

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

Upvotes: 1

Related Questions