Ruby send SMTP email

How to use Ruby SMTP client to send email with the MailSlurp mail servers

send and read email in ruby Ruby has the ability to send emails directly using SMTP (simple mail transfer protocol). This post will show you how to configure the inbuilt library net/smtp to send email with ruby SMTP.

What is SMTP?

SMTP is a protocol that was developed to allow computers to send electronic mail to one another. The protocol is widely supported and included in Ruby with the net/smtp package. For SMTP to work you need to communicate with an SMTP server that is set up to handle emails for a particular email domain (such as @gmail.com).

Import NET/SMTP

Sending email with SMTP in Ruby is easy. In a new ruby file add:

require 'net/smtp'

Then to send an email start a process and perform sending inside the do block:

Net::SMTP.start('email.host', 25, 'greeting.your.domain') do |smtp|
  smtp.send_message 'Hello', 'from@me.com', 'to@you.com'
end

Notice the host and port given. This is the SMTP server that we wish to email. You must know the SMTP server location of the email provider you wish to contact. Typically, this can be found by running dig on a domain for MX records. For an @gmail account for instance the MailServer can be found running on port 465 of the host smtp.gmail.com. You can use ruby to send email with SMTP to gmail accounts by using the host and port with net/smtp.

Testing a MailServer with TELNET

To test that an SMTP server is functioning on the desired port and host use telnet to call a server:

telnet mx.mailslurp.com 2525

If the server is working you should see a response like so:

Trying 34.212.234.28...
Connected to mx.mailslurp.com.
Escape character is '^]'.
220 localhost ESMTP Service Ready

Using your own SMTP server

If you have an email host like Gmail or MailSlurp you can use the SMTP server host they provide to connect to a server and send emails. For MailSlurp you can use the get_imap_smtp_access function to get access credentials:

require 'mailslurp_client'
MailSlurpClient.configure do |config|
  config.api_key['x-api-key'] = API_KEY
end
smtp_access = inbox_controller.get_imap_smtp_access()

# configure NET SMTP with plain auth
Net::SMTP.start(smtp_access.smtp_server_host, smtp_access.smtp_server_port, 'greeting.your.domain',
                smtp_access.smtp_username, smtp_access.smtp_password, :plain) do |smtp|
  # send email
  smtp.send_message message, inbox1.email_address, inbox2.email_address
end

Testing SMTP servers with Ruby and RSpec

Use ruby to send SMTP emails and use RSpec and MailSlurp to test your processes. MailSlurp gives you unlimited disposable mailboxes that you can connect to with SMTP in ruby. You can then use the MailSlurp API or Ruby SDK to wait for emails to arrive and validate their contents. Here is a full example:

# frozen_string_literal: true

require 'mailslurp_client'
require 'base64'
require 'rspec'
require 'net/smtp'

# read mailslurp api key from environment variables
API_KEY = ENV['API_KEY']
PATH_TO_ATTACHMENT = ENV['PATH_TO_ATTACHMENT']

describe 'use MailSlurp ruby sdk to create email addresses then send and receive email' do
  before(:all) do
    # expect upload file for later tests
    expect(PATH_TO_ATTACHMENT).to be_truthy

    # provide a mailslurp api key
    expect(API_KEY).to be_truthy

    # configure mailslurp client globally with API key (or pass each controller a client instance)
    MailSlurpClient.configure do |config|
      config.api_key['x-api-key'] = API_KEY
    end
  end

  it 'can send email using SMTP' do
    inbox_controller = MailSlurpClient::InboxControllerApi.new

    # create two inboxes
    inbox1 = inbox_controller.create_inbox_with_options({ inboxType: 'SMTP_INBOX' })
    inbox2 = inbox_controller.create_inbox

    expect(inbox1.email_address).to include('@mailslurp.mx')

    # get smtp access for inbox
    smtp_access = inbox_controller.get_imap_smtp_access({ inbox_id: inbox1.id })

    # compose email
    message = <<~MESSAGE_END
      From: #{inbox1.email_address}
      To: #{inbox2.email_address}
      Subject: Test smtp email

      This is a test
    MESSAGE_END

    Net::SMTP.start(smtp_access.smtp_server_host, smtp_access.smtp_server_port, 'greeting.your.domain',
                    smtp_access.smtp_username, smtp_access.smtp_password, :plain) do |smtp|
      # send email
      smtp.send_message message, inbox1.email_address, inbox2.email_address
    end

    # now confirm email was sent
    wait_for_controller = MailSlurpClient::WaitForControllerApi.new
    email = wait_for_controller.wait_for_latest_email({ inbox_id: inbox2.id })
    expect(email.subject).to include("Test smtp email")
  end
en

Further reading

For more tips on sending email with Ruby see the MailSlurp Ruby email library.

Ready to dive in?Start building email applications today.