Skip to content

Instantly share code, notes, and snippets.

@ninetwentyfour
Created October 24, 2017 15:59
Show Gist options
  • Save ninetwentyfour/aa44d1d8f0c3132ea72e1e16aa85b145 to your computer and use it in GitHub Desktop.
Save ninetwentyfour/aa44d1d8f0c3132ea72e1e16aa85b145 to your computer and use it in GitHub Desktop.
require 'sendgrid-ruby'
include SendGrid
require 'json'
html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title></title></head><body>
<div>
<br>
<%CONTACT_SYNC_HASH%>
<br>
<%UNSUBSCRIBE%>
<a href="<%UNSUBSCRIBE%>" style="color:#99bc3a;text-decoration:none" title="Manage Preferences"><%EMAIL_ADDRESS%></a>
</body></html>'
mail = Mail.new
mail.from = Email.new(email: '[email protected]')
mail.subject = 'Hello World from the SendGrid Ruby Library'
per = Personalization.new
per.add_to Email.new(email: '[email protected]')
per.add_substitution Substitution.new(key: '<%EMAIL_ADDRESS%>', value: '[email protected]')
per.add_substitution Substitution.new(key: '<%UNSUBSCRIBE%>', value: 'http://example.com/subscription_preferences.php?li=MA1-96fde115878ea85a&cs=<%CONTACT_SYNC_HASH%>')
per.add_substitution Substitution.new(key: '<%CONTACT_SYNC_HASH%>', value: 'MA1-0ac79376dd2ca3c1cabcf1c4eddaec1a')
mail.add_personalization per
mail.add_content Content.new(type: 'text/html', value: html)
sg = SendGrid::API.new(api_key: 'API_KEY', host: 'https://api.sendgrid.com')
puts mail.to_json
response = sg.client.mail._('send').post(request_body: mail.to_json)
puts response.status_code
puts response.body
puts response.headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment