Skip to content

Instantly share code, notes, and snippets.

@nickjacob
Created August 13, 2016 05:52
Show Gist options
  • Save nickjacob/6a3e3cd1df1368039bab8119945a94eb to your computer and use it in GitHub Desktop.
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
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