How do I specify a stylesheet for my HTML email with Premailer-Rails gem?
Asked Answered
O

2

14

I have a stylesheet I want to use in my HTML emails at the path: app/vendor/assets/stylesheets/inspinia/email_templates/email-styles.css

I am using the Premailer-Rails gem for my email styling.

In my mailer.html.erb layout, I have the following:

<%= stylesheet_link_tag "inspinia/email_templates/email-styles.css", media: 'all' %>

However, in my logs on Heroku, I get the following message:

app[web.1]: [923202d9-b3f1-4b9a-9f42-7b70dcc01d92] Started GET "/stylesheets/inspinia/email_templates/email-styles.css" for 54.167.56.21 at 2016-12-23 23:25:08 +0000
app[web.1]: [923202d9-b3f1-4b9a-9f42-7b70dcc01d92]   
2016-12-23T23:25:08.286725+00:00 app[web.1]: [923202d9-b3f1-4b9a-9f42-7b70dcc01d92] ActionController::RoutingError (No route matches [GET] "/stylesheets/inspinia/email_templates/email-styles.css"):

So how do I figure out/specify the correct path for this stylesheet in production?

Orlandoorlanta answered 23/12, 2016 at 23:27 Comment(1)
Try to put your email styles to vendor/assets/... instead of app/vendor/assets/...Sillsby
A
4

try move email-styles.css to /public/assets/ folder, or add it to the assets pipeline. Premailer-rails wiki:

  1. File System: If there's a file inside public/ with the same path as in the URL, it is read from disk. E.g. if the URL is http://cdn.example.com/assets/email.css the contents of the file located at public/assets/email.css gets returned if it exists.

  2. Asset Pipeline: If Rails is available and the asset pipeline is enabled, the file is retrieved through the asset pipeline. E.g. if the URL is http://cdn.example.com/assets/email-fingerprint123.css, the file email.css is requested from the asset pipeline. That is, the fingerprint and the prefix (in this case assets is the prefix) are stripped before requesting it from the asset pipeline.

Anishaaniso answered 10/1, 2017 at 3:49 Comment(0)
L
2

Add the following into config/initializers/assets.rb:

Rails.application.config.assets.precompile << 'email-styles.css'
Rails.application.config.assets.paths << Rails.root.join('app/vendor/assets/stylesheets/inspinia/email_templates')

Then use <%= stylesheet_link_tag 'email-styles.css', media: 'all' %> in the template.

Lazulite answered 5/1, 2017 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.