From 49f8a2baa78789c098eef9ef980570deb6cf5a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 13 Jun 2016 12:31:01 +0200 Subject: [PATCH] FEATURE: support for mandrill webhooks --- app/controllers/webhooks_controller.rb | 17 ++++++++++++++ config/routes.rb | 1 + lib/email/sender.rb | 7 ++++-- spec/controllers/webhooks_controller_spec.rb | 24 ++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb index 79265ee2aea..90edfe99a77 100644 --- a/app/controllers/webhooks_controller.rb +++ b/app/controllers/webhooks_controller.rb @@ -71,6 +71,23 @@ class WebhooksController < ActionController::Base render nothing: true, status: 200 end + def mandrill + events = params["mandrill_events"] + events.each do |event| + message_id = event["msg"]["metadata"]["message_id"] rescue nil + next unless message_id + + case event["event"] + when "hard_bounce" + process_bounce(message_id, Email::Receiver::HARD_BOUNCE_SCORE) + when "soft_bounce" + process_bounce(message_id, Email::Receiver::SOFT_BOUNCE_SCORE) + end + end + + render nothing: true, status: 200 + end + private def mailgun_failure diff --git a/config/routes.rb b/config/routes.rb index 717fe6da517..92497616fc6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -19,6 +19,7 @@ Discourse::Application.routes.draw do post "webhooks/mailgun" => "webhooks#mailgun" post "webhooks/sendgrid" => "webhooks#sendgrid" post "webhooks/mailjet" => "webhooks#mailjet" + post "webhooks/mandrill" => "webhooks#mandrill" if Rails.env.development? mount Sidekiq::Web => "/sidekiq" diff --git a/lib/email/sender.rb b/lib/email/sender.rb index e40cf09a675..d3774641820 100644 --- a/lib/email/sender.rb +++ b/lib/email/sender.rb @@ -133,9 +133,12 @@ module Email @message.header['X-Discourse-Post-Id'] = nil if post_id.present? @message.header['X-Discourse-Reply-Key'] = nil if reply_key.present? - # it's the only way to pass the original message_id when using mailjet - if ActionMailer::Base.smtp_settings[:address][".mailjet.com"] + # pass the original message_id when using mailjet/mandrill + case ActionMailer::Base.smtp_settings[:address] + when /\.mailjet\.com/ @message.header['X-MJ-CustomID'] = @message.message_id + when "smtp.mandrillapp.com" + @message.header['X-MC-Metadata'] = { message_id: @message.message_id }.to_json end # Suppress images from short emails diff --git a/spec/controllers/webhooks_controller_spec.rb b/spec/controllers/webhooks_controller_spec.rb index 0bf1b361144..6e599a2aaf4 100644 --- a/spec/controllers/webhooks_controller_spec.rb +++ b/spec/controllers/webhooks_controller_spec.rb @@ -75,4 +75,28 @@ describe WebhooksController do end + context "mandrill" do + + it "works" do + user = Fabricate(:user, email: email) + email_log = Fabricate(:email_log, user: user, message_id: message_id) + + post :mandrill, mandrill_events: [{ + "event" => "hard_bounce", + "msg" => { + "metadata" => { + "message_id" => message_id + } + } + }] + + expect(response).to be_success + + email_log.reload + expect(email_log.bounced).to eq(true) + expect(email_log.user.user_stat.bounce_score).to eq(2) + end + + end + end