views:

1325

answers:

2

I need help with e-mail templates. I have an html template with three embedded images in it. I am using the restful authentication plugin and have tried to customize the default mailer. The template works great as a standalone webpage but for some reason is not rendering properly with the images. I can either get the images to attach but not render inline, or the don't attach at all.

Anyway the mailer is as follows:

class UserMailer < ActionMailer::Base
  def signup_notification(user)
    setup_email(user)
    @subject << 'Please activate your thredUP account'
    @body[:url] = "#{APP_CONFIG[:site_url]}/activate/#{user.activation_code}"
  end

  def activation(user)
    setup_email(user)
    @subject << 'Your account has been activated - Welcome to thredUP!'
    @url = APP_CONFIG[:site_url]
    @user = user
    content_type "text/html"  
    attachment :content_type => "image/gif",  :body => File.read("#{Rails.root}/public/images/email/bottom-border.gif")
    attachment :content_type => "image/gif",  :body => File.read("#{Rails.root}/public/images/email/top-border.gif")
    attachment :content_type => "image/png",  :body => File.read("#{Rails.root}/public/images/email/footer.png")
    attachment :content_type => "image/png",  :body => File.read("#{Rails.root}/public/images/email/logo-lid.png")
    render :layout => 'standard'
  end

  protected
    def setup_email(user)
      @recipients = "#{user.email}"
      @from = APP_CONFIG[:admin_email]
      @subject = "[#{APP_CONFIG[:site_name]}] "
      @sent_on = Time.now
      @body[:user] = user
    end
end

I have also built the template as follows:

<html>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" bgcolor='#EFEFEF' >
<table width="100%" cellpadding="10" cellspacing="40" border="0" class="backgroundTable" bgcolor='#EFEFEF' >
    <tr>
     <td valign="top" align="center">
      <table width="600" cellpadding="0" cellspacing="0">
       <tr>
        <td style="padding-bottom:15px;"><img src="cid:logo-lid.png"> </td>
       </tr>
      </table>
      <table width="600" cellpadding="0" cellspacing="0">
       <tr>
        <td><img src="cid:top-border.gif"></td>
       </tr>
       <tr  bgcolor="#FFFFFF">
        <td style="padding:20px;">
         <%= yield %>
        </td>
       </tr>
       <tr>
        <td><img src="cid:bottom-border.gif"></td>
       </tr>
       <tr>
        <td style="text-align:center; padding-top:15px;">
         <img src="cid:footer.png">
        </td>
       </tr> 
      </table>
     </td>
    </tr>
</table>
</body>
</html>
+2  A: 

is there any particular reason that they need to be attached rather than hosted on your server and then referenced in the email (e.g. <img src="http://your.server/image.png" />)?

I would imagine that that would simplify it.

Ben Hughes
ok I will try that. I guess I remember back in my windows days, having to approve the download of server side images. I assume this has changed?
ChrisH
I can't speak for every client, but that is how most newsletter services seem to handle it, and it works.
Ben Hughes
A: 

If you insist on attaching them, try using Jason King's inline_attachment gem. Works best for static images.

Link: http://github.com/JasonKing/inline_attachment/tree/master

Swanand