Ruby

How to Send an Email in Ruby

Ruby is a powerful but also very easy-to-use programming language. It comes packed with features, and it allows external gem installation to extend and improve its functionality.

This tutorial focuses on teaching you how to use Ruby language to email using the Net::SMTP class.

How to Send a Simple Email

Before we can send an email using SMTP lib, we need to import it. You can do this by adding the required clause as:

require 'net/smtp'

Once we have SMTP imported, we need to open a connection to the SMTP server. To do this, we will use the ::start method. This method takes an address as the SMTP server address and the second argument a value as the port for the SMTP protocol.

The ::start will automatically close the connection once it completes.

require 'net/smtp'
# open connection
Net::SMTP.start('localhost', 25) do |smtp|
    # ..
end

The next step is to compose the message, which has the following components:

  1. From – This defines the name and address from which to send the email.
  2. To – This sets the recipient’s address.
  3. Subject – Subject of the Message
  4. Date – Date
  5. Message-Id – Unique message ID

The header components are the first part of the email string. Next, separate them with a new line and add the body of the message.

Finally, close it with the END_OF_MESSAGE block.

Consider the example shown below:

require 'net/smtp'
message = << END_OF_MESSAGE
From: Me <address@example.com>
To: You <recipient@address.com>
Subject: Email Subject Goes Here
Date: Wed, 4 Jul 2021 13:37:43 +0300
Message-Id: 28

This is the body of the message
END_OF_MESSAGE

Once we have the message part composed, we can use the send_message method to send the message as shown below:

require 'net/smtp'
message = << END_OF_MESSAGE
From: Me <address@example.com>
To: You <recipient@address.com>
Subject: Email Subject Goes Here
Date: Wed, 4 Jul 2021 13:37:43 +0300
Message-Id: 28

This is the body of the message
END_OF_MESSAGE

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

If you need to specify server details, such as username and password, do so in the start method as:

Net::SMTP.start('localhost', 25, ‘mail.domain’'username', 'password', :login_method)

In this example, we specify the client’s hostname, username, password, and authentication method. The methods can be plain, login, etc.

To send the email to multiple users, you can specify the addresses in the send_message method as:

require 'net/smtp'
message = << END_OF_MESSAGE
From: Me <address@example.com>
To: You <recipient@address.com>
Subject: Email Subject Goes Here
Date: Wed, 4 Jul 2021 13:37:43 +0300
Message-Id: 28

This is the body of the message
END_OF_MESSAGE

Net::SMTP.start('localhost', 25, 'username', 'password', :login_method) do |smtp|
    smtp.send_message message, '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]'
end

And with that, you can send a basic email using the Ruby Net::SMTP class.

Conclusion

This short tutorial showed you how to send a basic email using the Ruby Net::SMTP class. Consider the documentation to learn how you can expand on the SMTP class.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list