If you have an issue logging into your Twilio SendGrid account, please read this document. For any questions regarding login issues, please contact our support team.
If you have a non-library Twilio SendGrid issue, please contact our support team.
If you can't find a solution below, please open an issue.
- Migrating from v2 to v3
- Continue Using v2
- Testing v3 /mail/send Calls Directly
- Error Messages
- Versioning
- Environment Variables and Your Twilio SendGrid API Key
- Using the Package Manager
- Rails Specifics
- Ruby Versions
- Viewing the Request Body
- Verifying Event Webhooks
Please review our guide on how to migrate from v2 to v3.
Here is the last working version with v2 support.
Using RubyGems:
Add this line to your application's Gemfile:
gem 'sendgrid-ruby', '1.1.6'
And then execute:
bundle
Or install it yourself using:
gem install sendgrid-ruby -v 1.1.6
Download:
Click the "Clone or download" green button in GitHub and choose download.
Here are some cURL examples for common use cases.
To read the error message returned by SendGrid's API:
begin
response = sg.client.mail._("send").post(request_body: mail.to_json)
rescue Exception => e
puts e.message
end
We follow the MAJOR.MINOR.PATCH versioning scheme as described by SemVer.org. Therefore, we recommend that you always pin (or vendor) the particular version you are working with your code and never auto-update to the latest version. Especially when there is a MAJOR point release since that is guaranteed to be a breaking change. Changes are documented in the CHANGELOG and releases section.
All of our examples assume you are using environment variables to hold your Twilio SendGrid API key.
If you choose to add your Twilio SendGrid API key directly (not recommended):
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
becomes
sg = SendGrid::API.new(api_key: 'SENDGRID_API_KEY')
In the first case, SENDGRID_API_KEY is in reference to the name of the environment variable, while the second case references the actual Twilio SendGrid API Key.
We upload this library to RubyGems whenever we make a release. This allows you to use RubyGems for easy installation.
In most cases we recommend you download the latest version of the library, but if you need a different version, please use:
Add this line to your application's Gemfile:
gem 'sendgrid-ruby', 'X.X.X'
And then execute:
bundle
Or install it yourself using:
gem install sendgrid-ruby -v X.X.X
-
Namespace collision between Rails own
Mail
class and sendgrid classMail
. To avoid that issues please useSendGrid:: Mail
instead. -
The possibility of a namespace collision between the sendgrid class
Email
and your own definedEmail
class. To avoid these issues, you can skip theinclude SendGrid
line and use theSendGrid::
prefix for Email. Please see this SO answer for specifics.
This SDK does not work with ruby version 2.6.0 because of this bug. Please use any other supported version.
When debugging or testing, it may be useful to examine the raw request header to compare against the documented format.
You can do this before response = sg.client.mail._('send').post(request_body: mail.to_json)
like so:
puts mail.to_json
Twilio SendGrid's Event Webhook will notify a URL via HTTP POST with information about events that occur as your mail is processed. This article covers all you need to know to secure the Event Webhook, allowing you to verify that incoming requests originate from Twilio SendGrid. The sendgrid-ruby library can help you verify these Signed Event Webhooks.
You can find the usage example here and the tests here. If you are still having trouble getting the validation to work, follow the following instructions:
- Be sure to use the raw payload for validation
- Be sure to include a trailing carriage return and newline in your payload
- In case of multi-event webhooks, make sure you include the trailing newline and carriage return after each event