Skip to contentSkip to navigationSkip to topbar
On this page

v2 API Ruby Code Example


(warning)

This documentation is for the SendGrid Web API v2

We will deprecate this version on February 10, 2025. To access all the latest features and upcoming developments, please see our v3 API. For assistance with transitioning, refer to our migration guide.

(information)
(information)

Info

The library does not officially support the V2 API, but you can use V2 with an older version of the library. For more information, see Continue Using V2 in Ruby(link takes you to an external page).


Using SendGrid's Ruby Library

using-sendgrids-ruby-library page anchor
1
# using SendGrid's Ruby Library
2
# https://github.com/sendgrid/sendgrid-ruby
3
require 'sendgrid-ruby'
4
5
sendgrid = SendGrid::Client.new do |c|
6
c.api_key = 'SENDGRID_APIKEY'
7
end
8
9
email = SendGrid::Mail.new do |m|
10
m.to = 'test@sendgrid.com'
11
m.from = 'you@youremail.com'
12
m.subject = 'Sending with SendGrid is Fun'
13
m.html = 'and easy to do anywhere, even with Ruby'
14
end
15
16
sendgrid.send(email)

This example shows how to send email plain text and HTML email using Ruby. The gem Mail(link takes you to an external page) is required.

1
require 'mail'
2
Mail.defaults do
3
delivery_method :smtp, { :address => "smtp.sendgrid.net",
4
:port => 587,
5
:domain => "yourdomain.com",
6
:user_name => "yourusername@domain.com",
7
:api_key => "your_api_key",
8
:authentication => 'plain',
9
:enable_starttls_auto => true }
10
end
11
12
mail = Mail.deliver do
13
to 'yourRecipient@domain.com'
14
from 'Your Name <name@domain.com>'
15
subject 'This is the subject of your email'
16
text_part do
17
body 'Hello world in text'
18
end
19
html_part do
20
content_type 'text/html; charset=UTF-8'
21
body '<b>Hello world in HTML</b>'
22
end
23
end

To install the Mail(link takes you to an external page) gem please note that you need the OpenSSL library installed, then run the following:

gem install mail

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.