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.
We recommend using SendGrid Ruby, our client library, available on GitHub, with full documentation.
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.
1# using SendGrid's Ruby Library2# https://github.com/sendgrid/sendgrid-ruby3require 'sendgrid-ruby'45sendgrid = SendGrid::Client.new do |c|6c.api_key = 'SENDGRID_APIKEY'7end89email = SendGrid::Mail.new do |m|10m.to = 'test@sendgrid.com'11m.from = 'you@youremail.com'12m.subject = 'Sending with SendGrid is Fun'13m.html = 'and easy to do anywhere, even with Ruby'14end1516sendgrid.send(email)
This example shows how to send email plain text and HTML email using Ruby. The gem Mail is required.
1require 'mail'2Mail.defaults do3delivery_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 }10end1112mail = Mail.deliver do13to 'yourRecipient@domain.com'14from 'Your Name <name@domain.com>'15subject 'This is the subject of your email'16text_part do17body 'Hello world in text'18end19html_part do20content_type 'text/html; charset=UTF-8'21body '<b>Hello world in HTML</b>'22end23end
To install the Mail gem please note that you need the OpenSSL library installed, then run the following:
gem install mail