We recommend using SendGrid Ruby, our client library, available on Github, with full documentation.

Using SendGrid’s Ruby Library

1
2
3
4
5
6
7
8
9
10
11
12
13
# using SendGrid's Ruby Library - https://github.com/sendgrid/sendgrid-ruby
require 'sendgrid-ruby'

client = SendGrid::Client.new(api_user: api_user, api_key: api_key)

email = SendGrid::Mail.new do |m|
  m.to      = 'test@sendgrid.com'
  m.from    = 'you@youremail.com'
  m.subject = 'Sending with SendGrid is Fun'
  m.html    = 'and easy to do anywhere, even with Ruby'
end

client.send(email)

This example shows how to send email plain text and HTML email using Ruby. The gem Mail is required.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
require 'mail'
Mail.defaults do
  delivery_method :smtp, { :address   => "smtp.sendgrid.net",
                           :port      => 587,
                           :domain    => "yourdomain.com",
                           :user_name => "yourusername@domain.com",
                           :password  => "yourPassword",
                           :authentication => 'plain',
                           :enable_starttls_auto => true }
end

mail = Mail.deliver do
  to 'yourRecipient@domain.com'
  from 'Your Name <name@domain.com>'
  subject 'This is the subject of your email'
  text_part do
    body 'Hello world in text'
  end
  html_part do
    content_type 'text/html; charset=UTF-8'
    body '<b>Hello world in HTML</b>'
  end
end

To install the Mail gem please note that you need the OpenSSL library installed, then run the following:

1
gem install mail