Stackify is now BMC. Read theBlog

3 Ways to Send Emails with Ruby

By: dmytro@mailtrap
  |  May 14, 2024
3 Ways to Send Emails with Ruby

For many developers, Ruby on Rails framework not only allows them to build web applications, websites, and efficient database solutions, but it can help them optimize mailing operations. 

You can easily use Ruby on Rails mailer, an automatic tool to build transactional messages of any kind, and make proper authentication. 

In this article, we review three main ways to work with email sending in RoR, which include some Ruby gems, the Net::SMTP class, and the facilities of the Socket system.

Is Ruby still alive?

Many engineers still choose RoR for their projects. What makes RoR so popular despite the language itself being considered outdated? 

Primarily, it is the ease of reading and decoding the language due to its similarities to the English syntax. Rails Gems simplifies work and saves time by not requiring extended code.

The new versions of the framework are constantly changing but remain effective and safe thanks to a wide community of users who detect negative issues and help to correct deficiencies. 

Finally, testing operations start right from the moment the rails new application_name file is created, which is important for scalable projects.

Rails cannot compete with the popularity of Python or Java, but it is still a wide-spread programming language among developers.

Send emails with Ruby

Generally speaking, the easiest way to set up a painless email delivery is to use the appropriate applications. Using the appropriate applications will help you to not worry if the letters reached the recipient, track efficiency parameters, test the final form of the message, and increase the ultimate profitability of the business and generate website traffic. 

Send emails with Ruby Gems

There are several efficient packages for the implementation of internet mailing among Ruby libraries, including Ruby’s Gmail extent. 

For example, the Ruby Mail system allows you to send and receive regular and multi-level HTML messages with the ability to regulate the features of the SMTP protocol or the delivery method. If you focus on simple messages without any attachments, choose the Pony gem. 

Pony, as a Ruby Gem, helps to send emails in a shortcut code as a one-line command. But, users and tech-savvy Rails enthusiasts will likely choose ActionMailer.

ActionMailer

For an ActionMailer configuration, you need to create the letter itself with different variables. Then, add a mailer template called new-account.html.erb. for HTML letters. Use SMTP as the default sending method, which in its final version looks like this:

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {

  address:                    ‘smtp.yourserver.com’,

  port:                           authentification number,

  domain:                     ‘yourrubyapp.com’,

  user_name:               ‘<username>’,

  password:                  ‘<password>’,

  authentication:           ‘plain’,

  enable_starttls_auto: true 

}

It needs to be called UserMailer.simple_message (‘[email protected] ‘). Deliver_now. in order to deliver your mail.

In general, the message generated with ActionMailer has the following form:

class TestMailer < ActionMailer::Base

  default from: ‘[email protected]

  def simple_message(recipient)

    mail(

      to: recipient,

      subject: ‘Any subject you want’,

      body: ‘Lorem Ipsum’

    )

  end

end

It is worth noting an important opportunity to test a message-to-be-sent on-site of the class, or in case you will choose a Net::SMTP delivery way.

Net::SMTP specifics

A less popular option for sending emails on Rails is the Net::SMTP class due to the need for manual message creation. 

When writing a piece of code, it is important to separate the header and the message text with space and put the connection to the SMTP server. 

The basic format for sending a letter is as follows:

require ‘net/smtp’

message = <<END_OF_MESSAGE

From: YourRubyApp <[email protected]>

To: BestUserEver <[email protected]>

Subject: Any email subject you want 

Date: Tue, 28 Jul 2020 15:00:34 +0800

Lorem Ipsum

END_OF_MESSAGE

Net::SMTP.start(‘your.smtp.server’, authentication number) do |smtp|

  smtp.send_message message, 

  ‘[email protected]’, 

  ‘[email protected]

end

More complex versions, with additional protocol headers and coding, will allow sending complex letters with HTML and attachments.

Send emails with Ruby via Socket 

You can also send emails with RoR using the Socket class interface. However, in this case, the structure of the code becomes even more complicated.  Details on expanding the capabilities of mailing, designing letters and attachments, as well as the incorporation of HTML elements will take much more space.

Conclusion

The Ruby on Rails framework keeps its relevance as the multi-paradigm platform to a large extent due to its correlation with different functionalities for setting up a transactional email campaign. 
Ensure the performance of your Ruby applications with an Application Performance Management tool.  Stackify’s APM tool, Retrace, can help improve the performance of your application with Ruby server monitoring, error/log integration, and APM.  Try your 14 day, free Retrace trial. 

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]