Branded Emails with Devise

I’ve been working on some email notifications for both admin and client recently and needed some nice branded emails instead of the default plain text ones that rails gives you. So for this post, we’ll look at how to create branded emails for your own mailers, and carry the same design over to our Devise emails for when a user signs up.

Firstly, lets create a template. I used a free responsive email template generator I found online to create my template and tweaked it accordingly. Responsive templates can be a hassle, so save the time and get one from someone who knows what they’re doing and adapt it to your needs.

I put this in a file in my layouts folder called mailer.html.erb and edited the template to have a <%= yield %> tag where my content would be.

Now we can use our mailer template for any emails from a mailer we’ve created. We can use them on a per method basis, or, as I prefer, we can just set it as a default template for our class to use. For instance, in the top of my admin_notification_mailer.rb, just below the default from setting, we can add:

To use our mailer layout. Rails is smart enough to know to look in the layouts folder for this. But what about getting Devise to adopt this same template?

Simple. In our config/application.rb

Now anything we send from Devise will automatically use the ‘mailer’ layout we created.




Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">