Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

sendgrid unique args and rails 2.3 fix #1

Merged
merged 2 commits into from

2 participants

@yenif

added sendgrid_unique_args function to support unique_args per http://support.sendgrid.com/entries/353200-implementing-bounce-notification-for-multiple-from-addresses

Also added rails version check to determine which actionmailer override to use

@stephenb stephenb merged commit e5c2a09 into stephenb:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 9, 2011
  1. @yenif

    add unique args header

    yenif authored
  2. @yenif
This page is out of date. Refresh to see the latest.
Showing with 38 additions and 22 deletions.
  1. +38 −22 lib/sendgrid.rb
View
60 lib/sendgrid.rb
@@ -128,32 +128,43 @@ def sendgrid_spamcheck_maxscore(score)
@spamcheck_score = score
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
+ # Call within mailer method to set unique args for this email.
+ def sendgrid_unique_args(args)
+ @sg_unique_args = args
+ end
+
+ # 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
@@ -197,6 +208,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
Something went wrong with that request. Please try again.