Created
August 13, 2016 05:52
-
-
Save nickjacob/6a3e3cd1df1368039bab8119945a94eb to your computer and use it in GitHub Desktop.
using sendgrid4r to create a custom ActionMailer delivery method that uses the sendgrid v3 api
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
require 'mail/check_delivery_params' | |
require 'sendgrid4r' | |
class SendgridSender | |
include ::Mail::CheckDeliveryParams | |
def initialize(options = {}) | |
@api_key = options[:api_key] | |
end | |
def factory | |
SendGrid4r::Factory::MailFactory | |
end | |
def client | |
@client ||= SendGrid4r::Client.new(api_key: @api_key) | |
end | |
def deliver!(mail) | |
check_delivery_params(mail) | |
per = factory.create_personalization( | |
to: mail.to.map do |email| | |
factory.create_address(email: email) | |
end | |
) | |
html = factory.create_content( | |
type: 'text/html', | |
value: mail.body.to_s | |
) | |
from = factory.create_address( | |
email: mail.from.first | |
) | |
params = factory.create_params( | |
personalizations: [per], | |
from: from, | |
subject: mail.subject, | |
content: [html] | |
) | |
client.send(params: params) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment