rubyhanami

How to Transform Emails in Hanami to include inline the styles defined in an external css file?


If I use roadie/premailer to transform my emails in Hanami, then where should I place the transformation code in Hanami?

Thx


Solution

  • Here is the code which works:

    In the gemfile:

    gem 'roadie', '~> 3.4'
    

    And assuming that we put the style in the: lib/app/mailers/assets/stylesheets/mail.css the prepare method of the email:

    html_part = mail.html_part
    old_html = html_part.body.decoded
    
    document = Roadie::Document.new old_html
    document.add_css File.read( File.join(Hanami.root, "lib", "app", "mailers", "assets", "stylesheets", "mail.css"))
    new_html = document.transform
    
    html_part.body = new_html