Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create email with css and images from Rails?

How do you create and send emails from Rails application, that contain images and proper formatting? like the ones you get from facebook and like.

like image 624
Akshay Avatar asked Jan 23 '09 10:01

Akshay


People also ask

How do you put CSS in an email?

Use inline CSS. If you are set on using CSS, your best bet is to use inline code. This is the most effective and widely used method for including CSS in HTML emails. According to Litmus research, 86% of email designers inline their CSS.

Can you send an email with CSS?

CSS just doesn't work for email layout. Most email clients do not support CSS layout or will break your CSS layout—each in its own unique way. HTML tables may be old school, but they produce emails that look good across devices, and they are really the only way to go for email layout.


2 Answers

Assuming you know how to send normal plain-text emails from Rails using ActionMailer, to get HTML emails working you need to set a content type for your email.

For example, your notifer might look like this:

class MyMailer < ActionMailer::Base   def signup_notification(recipient)     recipients   recipient.email_address_with_name     subject      "New account information"     from         "[email protected]"     body         :user => recipient     content_type "text/html"   end end 

Note the content_type "text/html" line. This tells ActionMailer to send an email with a content type of text/html instead of the default text/plain.

Next you have to make your mailer views output HTML. For example, your view file app/views/my_mailer/signup_notification.html.erb might look like the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">  <html lang="en"> <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">     <style type="text/css" media="screen">       h3 { color: #f00; }       ul { list-style: none; }     </style> </head> <body>   <h3>Your account signup details are below</h3>   <ul>     <li>Name: <%= @user.name %></li>     <li>Login: <%= @user.login %></li>     <li>E-mail: <%= @user.email_address %></li>   </ul> </body> </html> 

As you can see the HTML view can include a <style> tag to define basic styles. Not all HTML and CSS is supported, especially across all mail clients, but you should definitely have sufficient formatting control over text styles.

Embedding images is a bit tricker if you plan to display attached emails. If you are simply including emails from external sites, you can use an <img /> tag as you usually would in HTML. However, many mail clients will block these images from being displayed until the user authorises it. If you need to display attached images, the Rails plug-in Inline Attachments might be worth a look.

For more information on Rails mailing support, the ActionMailer documentation is a great resource

like image 59
Bo Jeanes Avatar answered Sep 28 '22 06:09

Bo Jeanes


For the images you can just use the normal image_tag helper after you have defined the ActionMailer::Base.asset_host = 'http://www.your-domain.com'

I use Paperclip to store my images so in my case I can add an image to an email using this.

image_tag result.photo.url(:small) 
like image 34
Mika Avatar answered Sep 28 '22 05:09

Mika