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.

Related content

Fake SMTP servers for safe testing that prevent any outbound emails from reaching real users

Test emails safely with no risk of spamming users with real emails

Java email library for SMTP and creating email addresses

MailSlurp Java SDK for sending and receive email and attachments on the JVM.

Ruby Mailer SDK - send and receive emails in Rails, Rspec and more using MailSlurp's official email Gem

Receive email and attachments with Rails, Rspec, and Ruby without SMTP using MailSlurp Ruby Gem.

SMTP email application libraries

How to send email in CSharp, PHP, Ruby, Java, Python, and NodeJS using the SMTP protocol and MailSlurp mailservers.

1111 SMTP Connections failing

1111 SMTP Connections failing

How to Send an Email in Python

Send emails using SMTP mailer in Python. Detailed tutorial for sending mail in Python.

How to send emails in Javascript (NodeJS)

JS SMTP email sending guide using HTML and NodeJS.

PHPMailer tutorial (Sending email with PHP)

PHPMailer tutorial (Sending email with PHP)

How to send an email using Powershell (Windows and cross-platform)

Use Send-MailMessage in Windows Powershell to send emails using an SMTP server or MailSlurp's free email API.

How to send email in R

RLang tutorial for sending emails over SMTP.

SMTP Commands and Response Codes List

A full list of commands and response status codes for SMTP mail servers when sending emails.

Test SMTP servers with Telnet

How to use Telnet to test the SMTP servers?

What Is Amazon Simple Storage Service (SES) - And How Can I Use It?

Amazon SES is an email service among virtual servers, AI apps, and libraries. Ideally, you can integrate your email system with your program hosted on Amazon.

Grab all details about an SMTP Port

What are common SMTP mail server ports and how do you find them?

Which SMTP Port Should I Use?

Picking the right port for SMTP mailservers

Testing authentication using real email addresses in Ruby with Capybara, Cucumber, and Selenium

Cucumber example project using Capybara to test user authentication using real email addresses.

Cucumber, Ruby

Generate test email accounts with Ruby and Cucumber. Test email sign-up, password verification and more.

Avoiding email spam filters

How to send emails that don't trigger a spam warning.

BCC and CC in SMTP emails

What is cc and bcc in email? What is the difference between cc and bcc?

BCC For Email: What It Means And When To Use It

What is BCC email field (blind carbon copy) and how does it differ to CC?

What Is The Best SMTP Port To Use?

MailServers use TCP ports to transmit emails. Picking the right port between 25 vs 2555, 465 vs 587 in SMTP is easy with this guide.

How to configure Mail desktop clients with MailSlurp

How to setup MailSlurp with Outlook, Gmail, Thunderbird, Mail app and more.

Send email with CSharp using SMTP Client and MailSlurp

Create a custom SMTP client and access MailSlurp inboxes from CSharp/DotNET.

DMARC – All Facts that you should know

DMARC – All Facts that you should know

Email size limits

Email size limits

The Fundamentals Of Email Subdomains

How to manage email subdomain custom mailboxes using SMTP/IMAP mailserver settings.

How to send emails in VBA

Send email in Visual basic

A guide to PHP Mailer

A guide to PHP Mailer

SMTP IMAP Settings

How to access MailSlurp email accounts using IMAP and SMTP servers

SMTP Relay for outbound smtp servers

How to use SMTP relay to address spam and security issues that affect outbound mail servers.

SMTP vs HTTP MailSlurp inboxes

The difference between HTTP and SMTP inboxes and domains in MailSlurp.

What are SMTP ports

What are SMTP ports

CSharp send SMTP email

How to use CSharp SMTP client to send email with MailSlurp mail server

Send SMTP email with Java

How to use Java SMTP client to send email with MailSlurp mail server on the JDK

Send emails in NodeJS using SMTP

How to use Javascript SMTP client (or Nodemailer) to send email with MailSlurp mail server

Send SMTP emails with PHP

Use PHPMailer to send emails with SMTP and MailSlurp

Send SMTP email with Python (using SMTPLib)

How to send emails with Python's built-in SMTP client and MailSlurp mail servers.

Ruby send SMTP email

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

Ready to dive in?Start building email applications today.