diff --git a/README.md b/README.md index c51970a..f8cef50 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,14 @@ Once you have correctly configured Mandrill, you can go ahead and delete this co [Adding Routes]: http://help.mandrill.com/entries/21699367-Inbound-Email-Processing-Overview +#### Allowing domains without SPF configured +Sometimes, due to circumstances outside our control, a domain may not have SPF set up. In those cases, you can set the adapter to pass results to your email processor. + +``` +Griddler::Mandrill::Adapter.allow_spf_none = true +``` + + ## Credits Griddler::Mandrill was extracted from Griddler by [Stafford Brunk](https://github.com/wingrunr21). diff --git a/lib/griddler/mandrill/adapter.rb b/lib/griddler/mandrill/adapter.rb index 7b052f3..6d5d782 100644 --- a/lib/griddler/mandrill/adapter.rb +++ b/lib/griddler/mandrill/adapter.rb @@ -5,14 +5,33 @@ def initialize(params) @params = params end + def self.allow_spf_none? + @allow_spf_none || false + end + + def allow_spf_none? + self.class.allow_spf_none? + end + + def self.allow_spf_none=(allow_spf_none) + @allow_spf_none = allow_spf_none + end + def self.normalize_params(params) adapter = new(params) adapter.normalize_params end + def event_passes_spf?(event) + event[:spf].present? && + ((event[:spf][:result] == 'pass' || event[:spf][:result] == 'neutral') || + (allow_spf_none? && event[:spf][:result] == 'none')) + end + def normalize_params events.select do |event| - event[:spf].present? && (event[:spf][:result] == 'pass' || event[:spf][:result] == 'neutral') + event_passes_spf?(event) + end.map do |event| { to: recipients(:to, event), diff --git a/spec/griddler/mandrill/adapter_spec.rb b/spec/griddler/mandrill/adapter_spec.rb index 4abbfaf..2eb9952 100644 --- a/spec/griddler/mandrill/adapter_spec.rb +++ b/spec/griddler/mandrill/adapter_spec.rb @@ -118,6 +118,33 @@ end end + describe 'when the spf record is none' do + before do + @params = params_hash + @params.first[:msg][:spf] = { result: 'none', detail: '' } + end + + it 'does not include the emails without spf results by default' do + params = default_params(@params) + normalized_params = Griddler::Mandrill::Adapter.normalize_params(params) + expect(normalized_params).to be_empty + end + + context 'when the adapter is configured to allow none' do + before do + Griddler::Mandrill::Adapter.allow_spf_none = true + end + after do + Griddler::Mandrill::Adapter.allow_spf_none = false + end + it 'includes the message' do + params = default_params(@params) + normalized_params = Griddler::Mandrill::Adapter.normalize_params(params) + expect(normalized_params).not_to be_empty + end + end + end + describe 'when the spf record is softfail' do before do @params = params_hash