Home » Ruby programming

Sending email using Ruby

In this tutorial, we are going to learn how to send email using Ruby programming languages with example?
Submitted by Hrithik Chandra Prasad, on September 08, 2019

Ruby sending email

Sending emails and routing email among mail servers are handled by Simple Mail Transfer Protocol commonly known as SMTP. Net::SMTP class is a predefined class in Ruby’s library which is purposefully defined for Simple Mail Transfer Protocol.

Net::SMTP has two methods namely new and start. The parameter specification goes as follows,


It consumes two parameters, the first one is a server which is by default 'localhost' and the port number which is by default 25.


It consumes six parameters namely server, port, domain, account, password, and authtype. The default value of server is localhost, the port is 25, the domain is ENV["HOSTNAME"], account and password is nil and authtype is cram_md5.

sendmail is a method of an SMTP object which takes three parameters namely The source, The sender and the recipient.

Now, let us understand a very simple way to send one email using Ruby code. refer the example given below,

require 'net/smtp'

message = <<MESSAGE_END
From: Includehelp <[email protected]>
To: Hrithik sinha <[email protected]>
Subject: Urgent Information

Hello there, Greeting from Includehelp.
We hope, you are doing good. Have a great day.
Read articles at Includhelp for best programming guidance.

Net::SMTP.start('localhost') do |smtp|
  smtp.send_message message, '[email protected]', '[email protected]'

The code given above is a very basic way to send email using Ruby. We have integrated a very simple email message. It is required to take care of the format of headers properly. We all know that an email requires three things namely a From, To and Subject which should be identical from the body of an email with the help of a blank line.

For sending an email, we are supposed to use Net::SMTP class which allows us to connect with the SMTP server on our machine and then make use of send_message method with the message specified, the sender’s address and the receiver’s address as parameters.

If you want to send an HTML file using Ruby script, that file will be treated as a normal document even after including HTML tags in the documents. Net::SMTP class provides you a feature to send an HTML message with the help of an email. Refer the following code for instance.

require 'net/smtp'

message = <<MESSAGE_END
From: Hrihik Sinha <[email protected]>
To: Saksham Sharma <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: An exemplary email

hey bro,

This is an example. Read articles at Includehelp.com for better understanding of concepts.

<h1>Write heading here</h1>
<p>This is a paragraph</p>

Net::SMTP.start('localhost') do |smtp|
   smtp.send_message message, '[email protected]', '[email protected]'

You are supposed to specify Mime version, the type of content and character set if you want to email an HTML text using Ruby script.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.