Last active
July 20, 2018 06:23
-
-
Save faizaankhan/1bf0cc9c312134909431935deff08fd2 to your computer and use it in GitHub Desktop.
Snedgrid API Integration in Rails 5 Application, add gem 'sendgrid-ruby' to GemFile
This file contains hidden or 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 SendgridWebMailer < ActionMailer::Base | |
require 'sendgrid-ruby' | |
include SendGrid | |
require 'json' | |
def initialize | |
@client = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']).client | |
end | |
def send_marketting_email(candidate) | |
mail = Mail.new | |
mail.from = Email.new(email: '[email protected]') | |
mail.subject = 'Kreeti Technologies Invitation' | |
personalization = Personalization.new | |
personalization.add_to(Email.new(email: candidate.email, name: candidate.name)) | |
personalization.add_substitution(Substitution.new(key: '[Unsubscribe]', value: '<%asm_group_unsubscribe_raw_url%>')) | |
personalization.add_substitution(Substitution.new(key: '[Unsub_All]', value: '<%asm_global_unsubscribe_raw_url%>')) | |
personalization.add_substitution(Substitution.new(key: '[Preferences]', value: '<%asm_preferences_raw_url%>')) | |
personalization.add_substitution(Substitution.new(key: '[Sender_Name]', value: 'Some Technologies')) | |
personalization.add_substitution(Substitution.new(key: '[Sender_Address]', value: 'Some Building, Salt Lake Sector V')) | |
personalization.add_substitution(Substitution.new(key: '[Sender_City]', value: 'Some City')) | |
personalization.add_substitution(Substitution.new(key: '[Sender_State]', value: 'Some State')) | |
personalization.add_substitution(Substitution.new(key: '[Sender_Zip]', value: '700105')) | |
mail.add_personalization(personalization) | |
mail.add_content(Content.new(type: 'text/html', value: '<html><body><h2>@kreeti we believe that the key of getting things done is connecting people as people are the real asset. We want to feel and be felt. Join us in our journey.</h2></body></html>')) | |
mail.template_id = 'a12fb809-eaff-4499-bcb8-ff212dfe51f7' | |
mail.asm = ASM.new(group_id: 6775) | |
response = @client.mail._('send').post(request_body: mail.to_json) | |
unless response.status_code == '202' | |
puts 'Mail Not Sent' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment