Skip to content
45 changes: 28 additions & 17 deletions lib/sendgrid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ module SendGrid

def self.included(base)
base.class_eval do

# NOTE: This code is required to correctly override the ActionMailer::Base
# #mail method. Otherwise, mail method for the class will hide completely
# the one included from the module.
alias_method :send_grid_old_mail, :mail
protected

# Sets the custom X-SMTPAPI header after creating the email but before delivery
# def mail(headers={}, &block)
def mail(headers={}, &block)
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

Rails.logger.debug "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if DEBUG

self.headers['X-SMTPAPI'] = sendgrid_json_headers(message)

# NOTE: Added to ensure that ActionMailer::Base#mail method is called,
# after SendGrid headers have been injected.
send_grid_old_mail(headers, &block)
end

public

class << self
attr_accessor :default_sg_category, :default_sg_options, :default_subscriptiontrack_text,
:default_footer_text, :default_spamcheck_score, :default_sg_unique_args
Expand All @@ -42,6 +69,7 @@ class << self
# end

base.extend(ClassMethods)

end

module ClassMethods
Expand Down Expand Up @@ -160,23 +188,6 @@ def sendgrid_ganalytics_options(options)
options.each { |option| @ganalytics_options << option if VALID_GANALYTICS_OPTIONS.include?(option[0].to_sym) }
end

protected

# Sets the custom X-SMTPAPI header after creating the email but before delivery
def mail(headers={}, &block)
m = 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

Rails.logger.debug "SendGrid X-SMTPAPI: #{sendgrid_json_headers(message)}" if DEBUG

self.headers['X-SMTPAPI'] = sendgrid_json_headers(message)
m
end

private

# Take all of the options and turn it into the json format that SendGrid expects
Expand Down
2 changes: 1 addition & 1 deletion lib/sendgrid/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SendGrid #:nodoc:
VERSION = "2.0.0"
VERSION = "2.0.1"
end