CSS images in email with Rails 3

I am trying to send an email with Rails 3 and Action Mailer. The letter is going fine, but I want it to be in HTML format with some basic style that includes background images. I understand that images can be locked until the user can show them, but I still think that it is best to link to images on my web server.

An email template named registration_confirmation.html.erb starts as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <style type="text/css"> body { background: url(/images/mainbg_repeat.jpg) top repeat-x #cfcfcf; margin: 0px 0px 0px 0px; font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #565656; } 

What is the best way to get the url link for the background image to include the full host so that the background appears in the email?

+4
ruby-on-rails html-email actionmailer
Sep 22 '11 at 20:18
source share
3 answers

In response to my other answer, @Kevin wrote:

Thanks for the answer, I really thought about doing something, but I donโ€™t think that this is possible with the way I configured it. A mailing list call occurs in the after_create call in the model, and not therefore I do not think that I have access to the request object, since you (or I'm wrong). I have this in my mail program initializer: ActionMailer :: Base.default_url_options [: host] = "localhost: 3000" Can I somehow use this hos parameter in my mail program to make it work?

The answer is yes. All link helper rails should use these defualt_url_options. In addition to setting :host , you must also force it to use absolute URLs using the settings for this option:

 ActionMailer::Base.default_url_options[:only_path] = false 

Also set the host resource as follows:

 config.action_mailer.asset_host = 'http://localhost:3000' 

Then simply use the image_path instead of manually writing the URL, for example:

 <style type="text/css"> body { background: url(<%= image_path('mainbg_repeat.jpg') %>) top repeat-x #cfcfcf; } </style> 

NOTE. Setting default_url_options is just as deprecated. Here is a new way to do this:

 config.action_mailer.default_url_options = { :host => 'localhost:3000', :only_path => false } 
+16
Sep 22 2018-11-21T00:
source share
โ€” -

Pass your request host as a parameter to the mailer method, and then pass it from the method to the view. So, for example, your mail program method may look like this (the example is removed from the docs rails and changed here):

 class UserMailer < ActionMailer::Base default :from => "notifications@example.com" def registration_confirmation(user, host) @user = user @host = host mail(:to => user.email, :subject => "Welcome to My Awesome Site") end end 

You would call it this way:

 def some_action UserMailer.registration_confirmation(@user, request.host).deliver end 

Then, in your opinion, you just use @host:

 <style type="text/css"> body { background: url(http://<%= @host %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf; } </style> 

All this assumes that the image server is the same as the server on which the request is made. If the image server is located elsewhere, you should output a constant here. You can add something like this in lib / settings.rb:

 module Settings IMAGE_HOST = 'superawesome.images.com' end 

Then, in your opinion, you simply output a constant, for example:

 <style type="text/css"> body { background: url(http://<%= Settings::IMAGE_HOST %>/images/mainbg_repeat.jpg) top repeat-x #cfcfcf; } </style> 
+2
Sep 22 '11 at 20:26
source share

If you don't care about performance, a road symbol can handle the URLs in the stylesheets for you.

0
Jul 01 '13 at 3:26
source share



All Articles