Commit a37a95ff6fe881bde453bf290f96c02c91e062dd
1 parent
abc10614
Exists in
master
and in
1 other branch
Added Email Headers with lots of metadata
Added a shared_example to handle default specs for emails
Showing
2 changed files
with
55 additions
and
8 deletions
Show diff stats
app/mailers/mailer.rb
... | ... | @@ -6,7 +6,12 @@ class Mailer < ActionMailer::Base |
6 | 6 | helper ApplicationHelper |
7 | 7 | helper BacktraceLineHelper |
8 | 8 | |
9 | - default :from => Errbit::Config.email_from | |
9 | + default :from => Errbit::Config.email_from, | |
10 | + 'X-Errbit-Host' => Errbit::Config.host, | |
11 | + 'X-Mailer' => 'Errbit', | |
12 | + 'X-Auto-Response-Suppress' => 'OOF, AutoReply', | |
13 | + 'Precedence' => 'bulk', | |
14 | + 'Auto-Submitted' => 'auto-generated' | |
10 | 15 | |
11 | 16 | def err_notification(notice) |
12 | 17 | @notice = notice |
... | ... | @@ -15,6 +20,10 @@ class Mailer < ActionMailer::Base |
15 | 20 | count = @notice.similar_count |
16 | 21 | count = count > 1 ? "(#{count}) " : "" |
17 | 22 | |
23 | + errbit_headers 'App' => @app.name, | |
24 | + 'Environment' => @notice.environment_name, | |
25 | + 'Error-Id' => @notice.err_id | |
26 | + | |
18 | 27 | mail :to => @app.notification_recipients, |
19 | 28 | :subject => "#{count}[#{@app.name}][#{@notice.environment_name}] #{@notice.message.truncate(50)}" |
20 | 29 | end |
... | ... | @@ -23,6 +32,11 @@ class Mailer < ActionMailer::Base |
23 | 32 | @deploy = deploy |
24 | 33 | @app = deploy.app |
25 | 34 | |
35 | + errbit_headers 'App' => @app.name, | |
36 | + 'Environment' => @deploy.environment, | |
37 | + 'Deploy-Revision' => @deploy.revision, | |
38 | + 'Deploy-User' => @deploy.username | |
39 | + | |
26 | 40 | mail :to => @app.notification_recipients, |
27 | 41 | :subject => "[#{@app.name}] Deployed to #{@deploy.environment} by #{@deploy.username}" |
28 | 42 | end |
... | ... | @@ -36,7 +50,18 @@ class Mailer < ActionMailer::Base |
36 | 50 | |
37 | 51 | recipients = @comment.notification_recipients |
38 | 52 | |
53 | + errbit_headers 'App' => @app.name, | |
54 | + 'Environment' => @notice.environment_name, | |
55 | + 'Problem-Id' => @problem.problem_id, | |
56 | + 'Comment-Author' => @user.name | |
57 | + | |
39 | 58 | mail :to => recipients, |
40 | 59 | :subject => "#{@user.name} commented on [#{@app.name}][#{@notice.environment_name}] #{@notice.message.truncate(50)}" |
41 | 60 | end |
61 | + | |
62 | + private | |
63 | + | |
64 | + def errbit_headers(header) | |
65 | + header.each { |key,value| headers["X-Errbit-#{key}"] = value.to_s } | |
66 | + end | |
42 | 67 | end | ... | ... |
spec/mailers/mailer_spec.rb
1 | 1 | require 'spec_helper' |
2 | 2 | |
3 | +shared_examples "a notification email" do | |
4 | + it "should have X-Mailer header" do | |
5 | + expect(@email).to have_header('X-Mailer', 'Errbit') | |
6 | + end | |
7 | + | |
8 | + it "should have X-Errbit-Host header" do | |
9 | + expect(@email).to have_header('X-Errbit-Host', Errbit::Config.host) | |
10 | + end | |
11 | + | |
12 | + it "should have Precedence header" do | |
13 | + expect(@email).to have_header('Precedence', 'bulk') | |
14 | + end | |
15 | + | |
16 | + it "should have Auto-Submitted header" do | |
17 | + expect(@email).to have_header('Auto-Submitted', 'auto-generated') | |
18 | + end | |
19 | + | |
20 | + it "should have X-Auto-Response-Suppress header" do | |
21 | + # http://msdn.microsoft.com/en-us/library/ee219609(v=EXCHG.80).aspx | |
22 | + expect(@email).to have_header('X-Auto-Response-Suppress', 'OOF, AutoReply') | |
23 | + end | |
24 | + | |
25 | + it "should send the email" do | |
26 | + expect(ActionMailer::Base.deliveries.size).to eq 1 | |
27 | + end | |
28 | +end | |
29 | + | |
3 | 30 | describe Mailer do |
4 | 31 | context "Err Notification" do |
5 | 32 | include EmailSpec::Helpers |
... | ... | @@ -19,9 +46,8 @@ describe Mailer do |
19 | 46 | @email = Mailer.err_notification(notice).deliver |
20 | 47 | end |
21 | 48 | |
22 | - it "should send the email" do | |
23 | - expect(ActionMailer::Base.deliveries.size).to eq 1 | |
24 | - end | |
49 | + it_should_behave_like "a notification email" | |
50 | + | |
25 | 51 | |
26 | 52 | it "should html-escape the notice's message for the html part" do |
27 | 53 | expect(@email).to have_body_text("class < ActionController::Base") |
... | ... | @@ -62,10 +88,6 @@ describe Mailer do |
62 | 88 | @email = Mailer.comment_notification(comment).deliver |
63 | 89 | end |
64 | 90 | |
65 | - it "should send the email" do | |
66 | - expect(ActionMailer::Base.deliveries.size).to eq 1 | |
67 | - end | |
68 | - | |
69 | 91 | it "should be sent to comment notification recipients" do |
70 | 92 | expect(@email.to).to eq recipients |
71 | 93 | end | ... | ... |