Created
July 6, 2017 06:59
-
-
Save tskogberg/e359a5dd6d11993de877c133c4a4dcd2 to your computer and use it in GitHub Desktop.
Add sendgrid headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationMailer < ActionMailer::Base | |
default from: "Example.com <support@example.com>" | |
def process(name, *args) | |
set_headers_for_sendgrid(name, args) | |
super | |
end | |
private | |
def set_headers_for_sendgrid(mailer_action, mailer_action_arg_values) | |
mailer = self.class.name | |
# parameters gives us e.g. [ [:req, :arg1 ], … ] with types and names. | |
mailer_action_arg_names = method(mailer_action).parameters.map(&:last) | |
# Turn e.g. [ "k1", "k2" ] and ["v1", "v2" ] into { "k1" => "v1", "k2" => "v2" } | |
mailer_action_args = mailer_action_arg_names.zip(mailer_action_arg_values).to_h | |
headers["X-SMTPAPI"] = { | |
category: [ mailer, "#{mailer}##{mailer_action}" ], | |
unique_args: { | |
environment: Rails.env, | |
arguments: mailer_action_args.to_json, | |
} | |
}.to_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment