From fc91d2f95b1c28647bb0c4c172fb30d80fc081b4 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Sun, 4 Sep 2011 01:16:51 +0800 Subject: [PATCH] Use 'tamtam' gem to inline CSS for html emails. Created shared mailer layouts. Re-added full backtrace, as well as including the 'app_backtrace' in the WHERE section. --- Gemfile | 1 + Gemfile.lock | 4 ++++ app/mailers/mailer.rb | 2 ++ app/views/layouts/mailer.html.haml | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/views/layouts/mailer.text.erb | 6 ++++++ app/views/mailer/_signature.html.haml | 7 ------- app/views/mailer/_signature.text.erb | 2 -- app/views/mailer/deploy_notification.text.erb | 3 +-- app/views/mailer/err_notification.html.haml | 94 +++++++++++++++++++++++++++++++++++++++++----------------------------------------------------- app/views/mailer/err_notification.text.erb | 3 +-- config/initializers/inline_css.rb | 2 ++ lib/inline_css.rb | 14 ++++++++++++++ 12 files changed, 142 insertions(+), 66 deletions(-) create mode 100644 app/views/layouts/mailer.html.haml create mode 100644 app/views/layouts/mailer.text.erb delete mode 100644 app/views/mailer/_signature.html.haml delete mode 100644 app/views/mailer/_signature.text.erb create mode 100644 config/initializers/inline_css.rb create mode 100644 lib/inline_css.rb diff --git a/Gemfile b/Gemfile index f51fe54..c7ea271 100644 --- a/Gemfile +++ b/Gemfile @@ -16,6 +16,7 @@ gem 'octokit' gem 'inherited_resources' gem 'SystemTimer', :platform => :ruby_18 gem 'hoptoad_notifier', "~> 2.4" +gem 'tamtam' platform :ruby do gem 'bson_ext', '~> 1.3.1' diff --git a/Gemfile.lock b/Gemfile.lock index 0f3d2d7..43ab784 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -75,6 +75,7 @@ GEM hoptoad_notifier (2.4.11) activesupport builder + hpricot (0.8.4) i18n (0.5.0) inherited_resources (1.2.2) has_scope (~> 0.5.0) @@ -177,6 +178,8 @@ GEM typhoeus ruby_core_source (0.1.5) archive-tar-minitar (>= 0.5.2) + tamtam (0.0.3) + hpricot thin (1.2.11) daemons (>= 1.0.9) eventmachine (>= 0.12.6) @@ -222,6 +225,7 @@ DEPENDENCIES ruby-debug ruby-debug19 ruby-fogbugz + tamtam thin useragent (~> 0.3.1) webmock diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index 6cb80ae..c17b995 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -3,6 +3,8 @@ require Rails.root.join('config/routes.rb') class Mailer < ActionMailer::Base + include InlineCss + default :from => Errbit::Config.email_from def err_notification(notice) diff --git a/app/views/layouts/mailer.html.haml b/app/views/layouts/mailer.html.haml new file mode 100644 index 0000000..ff010a8 --- /dev/null +++ b/app/views/layouts/mailer.html.haml @@ -0,0 +1,70 @@ +%html + %head + :css + td.header { + padding: 10px 20px 10px 20px; + height: 75px; + background-color: #11112f; + text-align: left; + border-bottom: 1px solid #ccccee; + } + td.header a { + display: block; + height: 31px; + width: 88px; + margin-top: 10px; + } + td.header a img { + border: none; + } + + td.section, td.content, td.footer { + font-family: Helvetica,Arial,sans-serif; + font-size: 14px; + background-color: #ffffff; + text-align: left; + } + td.section { + padding: 0; + border-bottom: 1px solid #dddddd; + } + td.content { + padding: 20px 20px 10px 20px; + line-height: 1.3em; + } + td.footer { + padding: 10px 20px 20px 20px; + font-size: 11px; + font-weight: bold; + color: #666666; + } + + a.bold, span.bold { font-weight: bold; } + + p { margin: 0 0 15px 0; } + p.heading { + color: #6a6a6a; + margin-bottom: 4px; + } + p.monospace, p.backtrace { font-family: monospace; } + p.backtrace { margin-bottom: 2px; } + + %body + %div + %table(cellpadding="0" cellspacing="0" border="0" width="100%") + %tbody + %tr + %td.header + = link_to image_tag(URI.join(root_url,"stylesheets/images/logo.png").to_s), root_url + + = yield + + %table(cellpadding="0" cellspacing="0" border="0" align="left" width="100%") + %tbody + %tr + %td.footer + Your loyal servant, + = link_to 'Errbit', root_path + + %br + diff --git a/app/views/layouts/mailer.text.erb b/app/views/layouts/mailer.text.erb new file mode 100644 index 0000000..b050bc8 --- /dev/null +++ b/app/views/layouts/mailer.text.erb @@ -0,0 +1,6 @@ +<%= yield %> + + +Your loyal servant, +Errbit + diff --git a/app/views/mailer/_signature.html.haml b/app/views/mailer/_signature.html.haml deleted file mode 100644 index bda26a8..0000000 --- a/app/views/mailer/_signature.html.haml +++ /dev/null @@ -1,7 +0,0 @@ -%table(cellpadding="0" cellspacing="0" border="0" align="left" width="100%") - %tbody - %tr - %td(style="padding:10px 20px 20px 20px; font-size:11px; font-family:Helvetica,Arial,sans-serif; font-weight:bold; color:#666666; text-align:left; border-top:1px solid #dddddd;") - Your loyal servant, - = link_to 'Errbit', root_path - diff --git a/app/views/mailer/_signature.text.erb b/app/views/mailer/_signature.text.erb deleted file mode 100644 index b083654..0000000 --- a/app/views/mailer/_signature.text.erb +++ /dev/null @@ -1,2 +0,0 @@ -Your loyal servant, -Errbit \ No newline at end of file diff --git a/app/views/mailer/deploy_notification.text.erb b/app/views/mailer/deploy_notification.text.erb index 99a9101..3cf93a0 100644 --- a/app/views/mailer/deploy_notification.text.erb +++ b/app/views/mailer/deploy_notification.text.erb @@ -1,9 +1,8 @@ <%= @app.name %> was just deployed to <%= @deploy.environment %> by <%= @deploy.username %>. Details: - + What: <%= @app.name %><%= "@#{@deploy.revision}" unless @deploy.revision.blank? %> When: <%= @deploy.created_at.to_s %> From: <%= @deploy.repository.blank? ? 'n/a' : @deploy.repository %> -<%= render :partial => 'signature' %> \ No newline at end of file diff --git a/app/views/mailer/err_notification.html.haml b/app/views/mailer/err_notification.html.haml index d51a03f..fe85851 100644 --- a/app/views/mailer/err_notification.html.haml +++ b/app/views/mailer/err_notification.html.haml @@ -1,54 +1,42 @@ -%html - %head - %body - %div - %table(cellpadding="0" cellspacing="0" border="0" width="100%") - %tbody - %tr - %td(style="padding:10px 20px 10px 20px; height: 75px; background-color: #11112f; text-align:left; border-bottom:1px solid #ccccee;") - = link_to image_tag(URI.join(root_url,"stylesheets/images/logo.png").to_s, :style => "border: none;"), root_url, :style => "display: block; height: 31px; width: 88px; margin-top: 10px;" - %tr - %td(style="padding:0px 0px 10px 0px; font-family:Helvetica,Arial,sans-serif; font-size:14px; background-color:#ffffff; text-align:left; border-bottom:1px solid #dddddd;") - %table(cellpadding="0" cellspacing="0" border="0" align="left") - %tbody - %tr - %td(valign="top" style="padding:0px 20px 10px 20px; font-family:Helvetica,Arial,sans-serif; font-size:14px; background-color:#ffffff; text-align:left;") - %div(style="line-height:1.3em;") - %p(style="margin-top: 22px; margin-bottom:2px") - An err has just occurred in - = link_to(@app.name, app_url(@app), :style => "color:#000; font-weight: bold;") << "," - on the - %span(style="color:#000; font-weight: bold;")= @notice.err.environment - environment. - %p(style="margin-top: 0; margin-bottom:2px") - This err has occurred #{pluralize @notice.err.notices_count, 'time'}. - %p(style="padding:10px 0 5px 0; margin: 0;") - = link_to("Click here to view the error on Errbit", app_err_url(@app, @notice.err), :style => "font-weight:bold;") << "." - %tr - %td(style="padding-top:0; padding-bottom:10px; text-align:left;") - %table(cellpadding="0" cellspacing="0" border="0" align="left") - %tbody - %tr - %td(valign="top" style="padding:10px 20px 10px 20px; font-family:Helvetica,Arial,sans-serif; font-size:14px; background-color:#ffffff; text-align:left;") - %div(style="line-height:1.3em;") - %p(style="color:#777777; margin-top: 10px; margin-bottom: 2px;") - Error Message: - %p(style="margin-top:0;") - = raw(@notice.err.message) - %p(style="color:#777777; padding:10px 0 0 0; margin-bottom:0px;") - Where: - %p(style="margin-top:0; font-family:monospace") - = @notice.err.where - %p(style="color:#777777; padding:10px 0 0 0; margin-bottom:0px;") - URL: - %p(style="margin-top:0; margin-bottom:0; font-family:monospace;") - - if @notice.request['url'].present? - = link_to @notice.request['url'], @notice.request['url'] - %p(style="color:#777777; padding:10px 0 0 0; margin-bottom:0px;") - App Backtrace: - - @notice.app_backtrace.map{|l| "#{l['file']}:#{l['number']}" }.each do |backtrace_line| - %p(style="margin-top:0; margin-bottom:0; font-family:monospace")= backtrace_line - - = render :partial => 'signature' - %br +%tr + %td.section + %table(cellpadding="0" cellspacing="0" border="0" align="left") + %tbody + %tr + %td.content(valign="top") + %div + %p + An err has just occurred in + = link_to(@app.name, app_url(@app), :class => "bold") << "," + on the + %span.bold= @notice.err.environment + environment. + %br + This err has occurred #{pluralize @notice.err.notices_count, 'time'}. + %p + = link_to("Click here to view the error on Errbit", app_err_url(@app, @notice.err), :class => "bold") << "." +%tr + %td.section + %table(cellpadding="0" cellspacing="0" border="0" align="left") + %tbody + %tr + %td.content(valign="top") + %div + %p.heading ERROR MESSAGE: + %p= raw(@notice.err.message) + %p.heading WHERE: + %p.monospace + = @notice.err.where + - if (app_backtrace = @notice.app_backtrace).any? + - app_backtrace.map {|l| "#{l['file']}:#{l['number']}" }.each do |line| + %p.backtrace= line + %br + %p.heading URL: + %p.monospace + - if @notice.request['url'].present? + = link_to @notice.request['url'], @notice.request['url'] + %p.heading BACKTRACE: + - @notice.backtrace.map {|l| l ? "#{l['file']}:#{l['number']}" : nil }.compact.each do |line| + %p.backtrace= line + %br diff --git a/app/views/mailer/err_notification.text.erb b/app/views/mailer/err_notification.text.erb index 441ad0c..c51ca25 100644 --- a/app/views/mailer/err_notification.text.erb +++ b/app/views/mailer/err_notification.text.erb @@ -7,5 +7,4 @@ This err has occurred <%= pluralize @notice.err.notices_count, 'time' %>. You sh Backtrace follows: <%= @notice.backtrace.collect { |l| l ? " #{l['file']}:#{l['number']}" : nil }.compact.join("\n") %> - -<%= render :partial => 'signature' %> + diff --git a/config/initializers/inline_css.rb b/config/initializers/inline_css.rb new file mode 100644 index 0000000..61a80e4 --- /dev/null +++ b/config/initializers/inline_css.rb @@ -0,0 +1,2 @@ +require 'inline_css' + diff --git a/lib/inline_css.rb b/lib/inline_css.rb new file mode 100644 index 0000000..97346d6 --- /dev/null +++ b/lib/inline_css.rb @@ -0,0 +1,14 @@ +# +# module for ActionMailer to inline css in html emails +# +module InlineCss + def render(*args) + if (template = args.first[:template]) && template.mime_type.html? + # TamTam expects a