Sign up
email

How to Send Email Using Ruby

Angela Stringfellow

Last updated on

Created in 1995, Ruby is still one of the most popular programming languages in the world, and it continues to be used by thousands of developers to build websites, applications, and database solutions.

Programming languages allow many of the functions and capabilities of an application to be automated, and one of the most common capabilities is to send emails regularly. The need for automation is pronounced when the application has to send transactional emails on a frequent basis.

The programming language used to build the application should also be capable of sending emails automatically. Ruby on Rails can be used to send and validate emails, but it can also be used to automate the process, and in this article, we’ll discuss how to achieve that goal.

Sending Emails With Ruby

Sending emails with Ruby can be done in many ways. The three main ways to send emails are with Ruby Gems, ActionMailer, and Net::SMTP, and in the following sections, we will discuss each of these in turn.

Sending Email with Net::SMTP

SMTP or Simple Mail Transfer Protocol is a popular method of sending emails and Net::SMTP is a class that can be used to send emails in a Ruby application. The process required to send emails is as follows.

First, import the required libraries with the command given below.

require 'net/smtp'

The next step is to create variables required to send emails. The key parameters required are the sender's email address, password, and recipient's email address.

FROM_EMAIL = "mail@example.com"
PASSWORD = "strong-email-password"
TO_EMAIL = "recepient@email.com"

Once the variables have been declared, create an instance ‌of the SMTP client with the command in the following format. Here, smtp.address refers to the address from which the email is to be sent, and 587 is the most commonly used port for this task. Apply necessary changes where required.

smtp = Net::SMTP.new 'smtp.address', 587

The next step is composing the email to be sent, which should use the following format. You can, of course, change the message as appropriate.

message = <<END_OF_MESSAGE
From: John Doe <mail@example.com>
To: Jane Doe <recepient@email.com>
Subject: Sending email with Ruby 
Hello.
This is an email ‌sent with Ruby.
END_OF_MESSAGE

Once the email content is composed, initiate the SMTP client and send the email. After the required emails are sent, close the connection. You can use the following code to perform these tasks.

smtp.start('received-from-goes-here', FROM_EMAIL, PASSWORD, :plain)
smtp.send_message(message, FROM_EMAIL, TO_EMAIL)
smtp.finish()

You can compile all the code shown above as a Ruby file and then run it from the terminal as and when required. This script can also be modified to automate emails.

Sending Email with Ruby Gems

Ruby Gems is the package manager for Ruby. Ruby Gems has many packages that can be used to send emails, and these can be browsed at the Ruby Gems website. For this article, we will discuss the Ruby Gem called Pony and how to send email using the package.

This package can be used to send email with class sendmail or with SMTP. The default method is sending email with sendmail. The simplest format to send emails is:

Pony.mail(:to => 'mail@email.com', :cc => 'manager@example.com', :from => 'support@example.com', :subject => 'Hello', :body => 'Can you help me with this?', :via => :smtp)

You’ll need to provide the receiver's email address, sender's email address, message, and the delivery method. This method can be appropriate to the requirements of the application and can also be automated. Beyond Pony, there are other Ruby Gems that can also be used to send emails which you can find at the Ruby Gems website.

Sending Email with ActionMailer

ActionMailer is one of the most popular Ruby Gems used to send emails, as it helps to create classes that can be regularly invoked to send emails. The new classes created inherit from the class ActionMailer::Base.

For example, a class to send simple emails can be created with the following code snippet.

class ExampleMailer < ActionMailer::Base
  default from: 'mail@example.com'

  def simple_message(recipient)
    mail(
      to: recipient,
      subject: 'Sending email with Ruby',
      body: 'Hello. This email is sent with Ruby.'
    )
  end
end

The base structure can be extended with many functionalities that suit your requirements, and you can find a detailed guide to achieve this at RailsGuides.

Ruby offers many avenues to send emails from your application, and the three major methods discussed above are the most popular. Depending on your own specific requirements, there may be other options that are better suited to your needs. Choose the most appropriate method, implement it into your application and fully automate the email process.

If your notification strategy includes more than just emails, it’s convenient to have a solution that can manage various types of notifications and messages. MagicBell is a complete notification system you can implement in under an hour – without breaking a sweat – complete with a notification inbox for your product.

With MagicBell, you can notify users wherever they are with push, email, and in-app notifications and how they prefer, all while syncing your notifications across devices to ensure that your users don’t receive the same notifications twice.

Related articles: