From 936ab7697e73ce5bae9c39c9a11f7b8d350e8d8a Mon Sep 17 00:00:00 2001 From: Brian Finney Date: Wed, 8 Jun 2011 23:41:06 +0000 Subject: [PATCH 1/2] add unique args header --- lib/sendgrid.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index 1852129..6adbeba 100644 --- a/lib/sendgrid.rb +++ b/lib/sendgrid.rb @@ -128,6 +128,11 @@ def sendgrid_spamcheck_maxscore(score) @spamcheck_score = score end + # Call within mailer method to set unique args for this email. + def sendgrid_unique_args(args) + @sg_unique_args = args + end + # Sets the custom X-SMTPAPI header after creating the email but before delivery # NOTE: This override is used for Rails 2 ActionMailer classes. def create!(method_name, *parameters) @@ -197,6 +202,11 @@ def sendgrid_json_headers(mail) if !enabled_opts.empty? || (@sg_disabled_options && !@sg_disabled_options.empty?) header_opts[:filters] = filters_hash_from_options(enabled_opts, @sg_disabled_options) end + + # Set unique_args + if @sg_unique_args && !@sg_unique_args.empty? + header_opts[:unique_args] = @sg_unique_args + end header_opts.to_json.gsub(/(["\]}])([,:])(["\[{])/, '\\1\\2 \\3') end From d68321380f30c9d162143d4262b5763ab5454748 Mon Sep 17 00:00:00 2001 From: Brian Finney Date: Thu, 9 Jun 2011 00:11:50 +0000 Subject: [PATCH 2/2] only override methods for current version of rails --- lib/sendgrid.rb | 50 +++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/lib/sendgrid.rb b/lib/sendgrid.rb index 6adbeba..bfd48ad 100644 --- a/lib/sendgrid.rb +++ b/lib/sendgrid.rb @@ -133,32 +133,38 @@ def sendgrid_unique_args(args) @sg_unique_args = args end - # Sets the custom X-SMTPAPI header after creating the email but before delivery - # NOTE: This override is used for Rails 2 ActionMailer classes. - def create!(method_name, *parameters) - super - if @sg_substitutions && !@sg_substitutions.empty? - @sg_substitutions.each do |find, replace| - raise ArgumentError.new("Array for #{find} is not the same size as the recipient array") if replace.size != @sg_recipients.size + # only override the appropriet methods for the current rails version + if Rails.version < '3.0.0' + + # Sets the custom X-SMTPAPI header after creating the email but before delivery + # NOTE: This override is used for Rails 2 ActionMailer classes. + def create!(method_name, *parameters) + super + if @sg_substitutions && !@sg_substitutions.empty? + @sg_substitutions.each do |find, replace| + raise ArgumentError.new("Array for #{find} is not the same size as the recipient array") if replace.size != @sg_recipients.size + end end + puts "SendGrid X-SMTPAPI: #{sendgrid_json_headers(mail)}" if Object.const_defined?("SENDGRID_DEBUG_OUTPUT") && SENDGRID_DEBUG_OUTPUT + @mail['X-SMTPAPI'] = sendgrid_json_headers(mail) end - puts "SendGrid X-SMTPAPI: #{sendgrid_json_headers(mail)}" if Object.const_defined?("SENDGRID_DEBUG_OUTPUT") && SENDGRID_DEBUG_OUTPUT - @mail['X-SMTPAPI'] = sendgrid_json_headers(mail) - end - - protected - - # Sets the custom X-SMTPAPI header after creating the email but before delivery - # NOTE: This override is used for Rails 3 ActionMailer classes. - def mail(headers={}, &block) - super - if @sg_substitutions && !@sg_substitutions.empty? - @sg_substitutions.each do |find, replace| - raise ArgumentError.new("Array for #{find} is not the same size as the recipient array") if replace.size != @sg_recipients.size + + else + + protected + + # Sets the custom X-SMTPAPI header after creating the email but before delivery + # NOTE: This override is used for Rails 3 ActionMailer classes. + def mail(headers={}, &block) + super + if @sg_substitutions && !@sg_substitutions.empty? + @sg_substitutions.each do |find, replace| + raise ArgumentError.new("Array for #{find} is not the same size as the recipient array") if replace.size != @sg_recipients.size + end end + puts "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if Object.const_defined?("SENDGRID_DEBUG_OUTPUT") && SENDGRID_DEBUG_OUTPUT + self.headers['X-SMTPAPI'] = sendgrid_json_headers(message) end - puts "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if Object.const_defined?("SENDGRID_DEBUG_OUTPUT") && SENDGRID_DEBUG_OUTPUT - self.headers['X-SMTPAPI'] = sendgrid_json_headers(message) end private