Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sendgrid unique args and rails 2.3 fix #1

Merged
merged 2 commits into from Jun 9, 2011
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 38 additions & 22 deletions lib/sendgrid.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down