Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

v2 API Ruby Code Example


(information)

Info

We recommend using SendGrid Ruby, our client library, available on GitHub(link takes you to an external page), with full documentation.

(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

_16
# using SendGrid's Ruby Library
_16
# https://github.com/sendgrid/sendgrid-ruby
_16
require 'sendgrid-ruby'
_16
_16
sendgrid = SendGrid::Client.new do |c|
_16
c.api_key = 'SENDGRID_APIKEY'
_16
end
_16
_16
email = SendGrid::Mail.new do |m|
_16
m.to = 'test@sendgrid.com'
_16
m.from = 'you@youremail.com'
_16
m.subject = 'Sending with SendGrid is Fun'
_16
m.html = 'and easy to do anywhere, even with Ruby'
_16
end
_16
_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.


_23
require 'mail'
_23
Mail.defaults do
_23
delivery_method :smtp, { :address => "smtp.sendgrid.net",
_23
:port => 587,
_23
:domain => "yourdomain.com",
_23
:user_name => "yourusername@domain.com",
_23
:api_key => "your_api_key",
_23
:authentication => 'plain',
_23
:enable_starttls_auto => true }
_23
end
_23
_23
mail = Mail.deliver do
_23
to 'yourRecipient@domain.com'
_23
from 'Your Name <name@domain.com>'
_23
subject 'This is the subject of your email'
_23
text_part do
_23
body 'Hello world in text'
_23
end
_23
html_part do
_23
content_type 'text/html; charset=UTF-8'
_23
body '<b>Hello world in HTML</b>'
_23
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:


_10
gem install mail


Rate this page: