GithubHelp home page GithubHelp logo

premailer / actionmailer_inline_css Goto Github PK

View Code? Open in Web Editor NEW
74.0 4.0 25.0 43 KB

Seamlessly integrate Alex Dunae's premailer with ActionMailer.

Home Page: http://madebynathan.com

License: MIT License

Ruby 100.00%
premailer actionmailer inline css

actionmailer_inline_css's Introduction

ActionMailer Inline CSS TravisCI

Seamlessly integrate Alex Dunae's premailer gem with ActionMailer.

Problem?

Gmail doesn't support <style> or <link> tags for HTML emails. Other webmail clients also have problems with <link> tags.

This means that CSS must be inlined on each element, otherwise the email will not be displayed correctly in every client.

Solution

Inlining CSS is a pain to do by hand, and that's where the premailer gem comes in.

From http://premailer.dialect.ca/:

  • CSS styles are converted to inline style attributes. Checks style and link[rel=stylesheet] tags, and preserves existing inline attributes.
  • Relative paths are converted to absolute paths. Checks links in href, src and CSS url('')

This actionmailer_inline_css gem is a tiny integration between ActionMailer and premailer.

Inspiration comes from @fphilipe's premailer-rails3 gem, but I wasn't completely happy with it's conventions.

Installation & Usage

To use this in your Rails app, simply add gem "actionmailer_inline_css" to your Gemfile.

  • If you already have an HTML email template, all CSS will be automatically inlined.
  • If you don't include a text email template, premailer will generate one from the HTML part. (Having said that, it is recommended that you write your text templates by hand.)

NOTE:

The current version of premailer doesn't support UTF-8, so I have written a small workaround to enforce it. This works for both Ruby 1.9 and 1.8.

Including CSS in Mail Templates

You can use the stylesheet_link_tag helper to add stylesheets to your mailer layouts. actionmailer_inline_css contains a premailer override that properly handles these CSS URIs.

Example

Add the following line to the <head> section of app/views/layouts/build_mailer.html.erb:

<%= stylesheet_link_tag '/stylesheets/mailers/build_mailer' %>

This will add a stylesheet link for public/stylesheets/mailers/build_mailer.css. Premailer will then inline the CSS from that file, and remove the link tag.

More Info

See this Guide to CSS support in email from campaignmonitor.com for more info about CSS in emails.

Outlook 27.62%
iOS Devices 16.01%
Hotmail 12.14%
Apple Mail 11.13%
Yahoo! Mail 9.54%
Gmail 7.02%

actionmailer_inline_css's People

Contributors

akzhan avatar cwsaylor avatar glebm avatar guiocavalcanti avatar kevintyll avatar leehambley avatar ndbroadbent avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

actionmailer_inline_css's Issues

UTF-8 problems

To me it still having UTF-8 related problems. Some chars are displayed as .

Deadlock at development server when using :host option

When passing base_url Premailer tries to fetch the CSS file from local development server, which has been blocked by processing the previous request. Once most of the development servers may process only one request at time, the whole process is being blocked until timeout.

More info at premailer issues.

Ignore CSS

Is there a way to ignore CSS in <style> or ?

I need this to implement CSS with media queries for mobile.
So by ignoring the CSS, premailer will not make it inline and mobile devices mail app still can read the CSS.

Deprecation Warning with Rails 6.1 & Zeitwerk

I realize this is a project that isn't currently maintained, so we're just leaving this here for other people who run into this issue.

Tracked down the following issue related to actionmailer_inline_css. We're seeing deprecation warnings while upgrading to Rails 6.1:

DEPRECATION WARNING: Initialization autoloaded the constants ActionText::ContentHelper and ActionText::TagHelper.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload ActionText::ContentHelper, for example,
the expected changes won't be reflected in that stale Module object.

These autoloaded constants have been unloaded.

In order to autoload safely at boot time, please wrap your code in a reloader
callback this way:

    Rails.application.reloader.to_prepare do
      # Autoload classes and modules needed at boot time here.
    end

That block runs when the application boots, and every time there is a reload.
For historical reasons, it may run twice, so it has to be idempotent.

Check the "Autoloading and Reloading Constants" guide to learn more about how
Rails autoloads and reloads.

Once we removed actionmailer_inline_css from our gemfile, problem went away.

This matches up with the following Rails issue: rails/rails#36546

Problem appears to be here:

ActionMailer::Base.register_interceptor ActionMailer::InlineCssHook

which is being called on initialization.

I believe the new way in Rails 6 and Zeitwerk is to do something like this:

ActiveSupport.on_load(:action_mailer) do
  ActionMailer::Base.register_interceptor ActionMailer::InlineCssHook
end

We'll be doing our own thing, so if someone wants to provide a pull request or something else to fix this, feel free to do so.

Styles aren't applied inline in email itself, but in preview it's ok

Hello, i have a problem. I have mailer layout and in i includes styles:
= stylesheet_link_tag 'mailers/application', media: 'all'
and it's working on Action Mailer Preview- styles are converting into inline styles, everything it's fine.
But the problem is when i'm sending the real email (invoke method through the console to be precise, and gmail is the receiver). None of styles aren't applied in inline-way (or as normal styles nethier) and mail looks almost like non-styling html.
Can anyone know the answer why is that happend?

Missing license

There isn't any mention about which license – if any – actionmailer_inline_css is licensed under. Could you please clarify this matter?

Thanks.

Errno::EACCES: Permission denied

Hi,

Just installed premailer (windows 7, ruby 1.9.3p484, rails 4.1.3) and trying to send a css email from the console. I get following error:

irb(main):009:0> BottlyMailer.invitation_email(e, "[email protected]", "Name").deliver
Errno::EACCES: Permission denied - C:/Users/mike/Dropbox/kalenderfee/webapp/public
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionmailer_inline_css-1.5.3/lib/overrides/premailer/premailer.rb:13:in `read'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionmailer_inline_css-1.5.3/lib/overrides/premailer/premailer.rb:13:in `load_css_from_local_file_with_rails_path!'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/premailer-1.8.3/lib/premailer/premailer.rb:306:in `block in load_css_from_html!'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/nokogiri-1.6.6.2-x86-mingw32/lib/nokogiri/xml/node_set.rb:187:in `block in each'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/nokogiri-1.6.6.2-x86-mingw32/lib/nokogiri/xml/node_set.rb:186:in `upto'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/nokogiri-1.6.6.2-x86-mingw32/lib/nokogiri/xml/node_set.rb:186:in `each'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/premailer-1.8.3/lib/premailer/premailer.rb:285:in `load_css_from_html!'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/premailer-1.8.3/lib/premailer/premailer.rb:234:in `initialize'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionmailer_inline_css-1.5.3/lib/action_mailer/inline_css_hook.rb:12:in `new'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionmailer_inline_css-1.5.3/lib/action_mailer/inline_css_hook.rb:12:in `delivering_email'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/mail.rb:230:in `block in inform_interceptors'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/mail.rb:229:in `each'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/mail.rb:229:in `inform_interceptors'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/message.rb:220:in `inform_interceptors'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/message.rb:230:in `deliver'
        from (irb):9
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.1.3/lib/rails/commands/console.rb:90:in `start'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.1.3/lib/rails/commands/console.rb:9:in `start'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.1.3/lib/rails/commands/commands_tasks.rb:69:in `console'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.1.3/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
        from C:/Rails/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.1.3/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'irb(main):010:0>

Here's the template for the mail (haml)

!!!
%html
  %head
    %meta{:content => "width=device-width, initial-scale=1.0", :name => "viewport"}/
    %title= content_for?(:title) ? yield(:title) : "Kalenderfee"
    %meta{:content => content_for?(:description) ? yield(:description) : "Kalenderfee", :name => "description"}/
    = stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true
    = javascript_include_tag "application", "data-turbolinks-track" => true
  %body
    .application
      .row
        .container
          %p
            Hallo #{@name},
          %p
            = @event.owner.full_name
            möchte dich gerne zu #{@event.name} am #{I18n.l(@event.date.to_date, format: :local_date_short)} einladen. Kannst du dich bitte hierfür unter diesem Link an- oder abmelden? #{@event.owner.first_name} wird dir sicherlich sehr verbunden sein.
          %p
            %li
              %a{:href => event_url(@event)}= event_url(@event)  
          %p
            Danke und lieben Gruss
          %p
            \- Deine Kalenderfee        %footer

Appreciate your help

  • Mike

Tags Missing

Hi! In order to help audit releases, would you be able to push up some tags? It appears that the following tags are missing:

  • 1.5, all
  • 1.6

Many thanks!

UTF-8 Warning

I get this every warning every time I send an email.

Non US-ASCII detected and no charset defined.
Defaulting to UTF-8, set your own if this is incorrect.

I have defined the charset in the head tag of my email. Not sure how else to set the default.

Adding schema.org markup to e-mails.

In attempting to add actions to e-mails I noticed the premailer remove_scripts option is true by default. But actionmailer_inline_css doesn't support passing custom options to premailer.

I'd like to patch one of the libraries to allow it, but unclear on which I should do:

  • Disable removal of application/ld+json scripts in premailer.
  • Support passing custom options to premailer from actionmailer_inline_css.
  • Or both, with support for customizing the premailer removal query via configuration.

Does not clear up the html part nor picks up CSS

Thanks for this gem!
With Rails 3.2.1 and mailers that use layouts it seems to keep the html_part with tags, and it doesn't seem to find the css file.

This is how I include the stylesheet

!!! HTML
%html
  %head
    %meta(content="text/html; charset=UTF-8" http-equiv="Content-Type")
    = stylesheet_link_tag 'mailers/all'
    = yield(:head)
  %body
    #content
      = yield

How do you visualize e-mail views in development mode

I'm using this Gem and it's working very well. But I can't realize how to visualize the delivered e-mail with images.

I tried sending to my GMail account but I can't see the images (the generated HTML image path is just /images/mail/my-img.png). There is any config to add the http://0.0.0.0:3000 part to the image path?

Warning raised by Mail gem from test nlineCssHookTest#test_inline_css_hook_with_utf_8_characters

$ rake TESTOPTS="-v"
Loaded suite /Users/codebeaker/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started
InlineCssHookTest#test_inline_css_hook_with_base_url: 0.17 s: .
InlineCssHookTest#test_inline_css_hook_with_only_html_part: 0.03 s: .
InlineCssHookTest#test_inline_css_hook_with_text_and_html_parts: 0.06 s: .
InlineCssHookTest#test_inline_css_hook_with_utf_8_characters: Non US-ASCII detected and no charset defined.
Defaulting to UTF-8, set your own if this is incorrect.
0.03 s: .
InlineCssHookTest#test_preservation_of_attachments: 0.07 s: .
PremailerStylesheetLinkTagTest#test_premailer_stylesheet_link_tag: 0.07 s: .

Finished in 0.436325 seconds.

6 tests, 11 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 439 --verbose

Perhaps one aught to take something like silence_warnings from ActiveSupport, or a simpler implementation, without having to pull in an extensive dependency.

Wherever the Mail gem is producing the messages

Non US-ASCII detected and no charset defined.
Defaulting to UTF-8, set your own if this is incorrect.

Should be silenced in these tests.

I Can Has Release?

Rails 5.1 is out, and it really hates the current release of this gem due to its use of alias_method_chain. Would you mind cutting a release? Whilst you're at it, including the Github URL of this gem in the gemspec would be swell.

simply not working with SLIM

Is using .erb mandatory? I'm using SLIM and it's not working @ all.

I have a list_mailer.html.slim layout with:

html
head
= stylesheet_link_tag 'list_mail'
body = yield

Any clue?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.