Configure rails 7 ActionMailer to work with tailwind css (Premailer-rails + PostCSS)

Hello,

We are creating a RoR project (Rails 7 + Esbuild). We are using tailwindcss to do most of our styling. We re looking to leverage tailwind in our mailer templates. A recommendation came through suggesting that we could actually use premailer to help us use tailwind classes in our email templates.

This sounded fun until we remembered that tailwind heavily uses css variables, for which case we could have these them compiled to normal css that can be interpreted by mail clients.

A few searches online pointed to using postcss but I am not so sure how to configure it with tailwind css.

// app/views/layouts/mailer.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>
//postcss.config.js
module.exports = {
  plugins: [
    require('postcss-css-variables'),
    require('tailwindcss'),
    require('autoprefixer'),
  ],
};

// tailwind config
module.exports = {
  content: [
    "./app/views/**/*.html.erb",
    "./app/helpers/**/*.rb",
    "./app/assets/stylesheets/**/*.css",
    "./app/javascript/**/*.js",
  ],
...

Our build:css script is as thus:

    "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"

The question is I am not so sure how I would include postcss the build script to work with both tailwind and compile that to normal css.

Is there a way around this, preferably with an example of how to get all the 3 moving parts working together or any better suggestions on the whole approach?

I’m not sure if you figured it out, but I’ve been experimenting with a second tailwindcss config file, and using slightly different settings for emails. It’s based on maizzle’s configuration, and then I add second build in yarn.

"build:css:email": "tailwindcss -i ./app/assets/stylesheets/email.tailwind.css -c ./tailwind_email.config.js -o ./app/assets/builds/email.css"

Then using premailer, you can bring in the email specific css to your mailer layout.

Did you manage to use Tailwind for ActionMailer successfully?

I’m really interested

Made a video about an alternative way. Not sure if this is what you’re going for.

How to Send TailwindCSS-Styled Emails With Ruby on Rails 7