From a37a95ff6fe881bde453bf290f96c02c91e062dd Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto Date: Wed, 26 Feb 2014 16:32:44 -0300 Subject: [PATCH] Added Email Headers with lots of metadata --- app/mailers/mailer.rb | 27 ++++++++++++++++++++++++++- spec/mailers/mailer_spec.rb | 36 +++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index a33256f..769fe0b 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -6,7 +6,12 @@ class Mailer < ActionMailer::Base helper ApplicationHelper helper BacktraceLineHelper - default :from => Errbit::Config.email_from + default :from => Errbit::Config.email_from, + 'X-Errbit-Host' => Errbit::Config.host, + 'X-Mailer' => 'Errbit', + 'X-Auto-Response-Suppress' => 'OOF, AutoReply', + 'Precedence' => 'bulk', + 'Auto-Submitted' => 'auto-generated' def err_notification(notice) @notice = notice @@ -15,6 +20,10 @@ class Mailer < ActionMailer::Base count = @notice.similar_count count = count > 1 ? "(#{count}) " : "" + errbit_headers 'App' => @app.name, + 'Environment' => @notice.environment_name, + 'Error-Id' => @notice.err_id + mail :to => @app.notification_recipients, :subject => "#{count}[#{@app.name}][#{@notice.environment_name}] #{@notice.message.truncate(50)}" end @@ -23,6 +32,11 @@ class Mailer < ActionMailer::Base @deploy = deploy @app = deploy.app + errbit_headers 'App' => @app.name, + 'Environment' => @deploy.environment, + 'Deploy-Revision' => @deploy.revision, + 'Deploy-User' => @deploy.username + mail :to => @app.notification_recipients, :subject => "[#{@app.name}] Deployed to #{@deploy.environment} by #{@deploy.username}" end @@ -36,7 +50,18 @@ class Mailer < ActionMailer::Base recipients = @comment.notification_recipients + errbit_headers 'App' => @app.name, + 'Environment' => @notice.environment_name, + 'Problem-Id' => @problem.problem_id, + 'Comment-Author' => @user.name + mail :to => recipients, :subject => "#{@user.name} commented on [#{@app.name}][#{@notice.environment_name}] #{@notice.message.truncate(50)}" end + + private + + def errbit_headers(header) + header.each { |key,value| headers["X-Errbit-#{key}"] = value.to_s } + end end diff --git a/spec/mailers/mailer_spec.rb b/spec/mailers/mailer_spec.rb index 76bd50d..cf8fe18 100644 --- a/spec/mailers/mailer_spec.rb +++ b/spec/mailers/mailer_spec.rb @@ -1,5 +1,32 @@ require 'spec_helper' +shared_examples "a notification email" do + it "should have X-Mailer header" do + expect(@email).to have_header('X-Mailer', 'Errbit') + end + + it "should have X-Errbit-Host header" do + expect(@email).to have_header('X-Errbit-Host', Errbit::Config.host) + end + + it "should have Precedence header" do + expect(@email).to have_header('Precedence', 'bulk') + end + + it "should have Auto-Submitted header" do + expect(@email).to have_header('Auto-Submitted', 'auto-generated') + end + + it "should have X-Auto-Response-Suppress header" do + # http://msdn.microsoft.com/en-us/library/ee219609(v=EXCHG.80).aspx + expect(@email).to have_header('X-Auto-Response-Suppress', 'OOF, AutoReply') + end + + it "should send the email" do + expect(ActionMailer::Base.deliveries.size).to eq 1 + end +end + describe Mailer do context "Err Notification" do include EmailSpec::Helpers @@ -19,9 +46,8 @@ describe Mailer do @email = Mailer.err_notification(notice).deliver end - it "should send the email" do - expect(ActionMailer::Base.deliveries.size).to eq 1 - end + it_should_behave_like "a notification email" + it "should html-escape the notice's message for the html part" do expect(@email).to have_body_text("class < ActionController::Base") @@ -62,10 +88,6 @@ describe Mailer do @email = Mailer.comment_notification(comment).deliver end - it "should send the email" do - expect(ActionMailer::Base.deliveries.size).to eq 1 - end - it "should be sent to comment notification recipients" do expect(@email.to).to eq recipients end -- libgit2 0.21.2