Home » Development » Best Ruby Gems for Email Sending

Best Ruby Gems for Email Sending

Every Rubyist will tell you that the basic way to send email with Ruby is to use the Net::SMTP class. Also, he or she will add that you shouldn’t go this way. Why? Because there are a few notable gems that let you handle email activities simply and effectively. And today we’ll check out the most popular ones including Action Mailer, Ruby Mail, and Pony.


This is the most popular gem if you need to give effect to email delivery on a Ruby on Rails app. It lets you send emails using mailer classes and views. Here’s how a simple email built with ActionMailer may look:

class TestMailer < ActionMailer::Base
  default from: ''
  def simple_message(recipient)
      subject: 'Subject of the message',
      body: 'Body text of the message'

How to create and send an email with ActionMailer?

It’s unlikely that you’ll be OK with a simple message. In most cases, your Ruby app will send transactional emails and they need to be refined. So, check out the following guide on how to create an advanced message and send it using the SMTP server.

  1. Start with creating a mailer model because your app will use it to send emails:

    $ rails generate mailer Notifier

    Also, you need helpers to generate an electronic message. They are defined in the mailer model. You can set up variables in the mailer views, options like :to address, and so on. Here are some helpers you can make use of:

    • attachments['filename.png'] ='path/to/attachment.pdf') – to add attachments
    • attachments.inline['filename.png'] ='path/to/attachment.pdf') – to add an inline attachment
    • headers['X-No-Spam'] = 'True' – to specify a header field
    • headers({'X-No-Spam' => 'True', 'In-Reply-To' => ''}) – to specify multiple headers
    • mail – to specify the email to be sent

    Here is what we’ve got:

    class UserMailer< ApplicationMailer
      def simple_message(recipient)
        attachments["attachment.png"] ="path/to/attachment.png")
          to: “”,
          subject: "Subject of the message",
          content_type: "text/html",
          body: "<html><h2>HTML body text</h2></html>"

    In this mailer class, we’ve got HTML text, as well as an attached PNG file. Now, we can create a corresponding view.

  2. Create a view, a template, to be used with the mailer. You need to create an .erb file in app/views/notifier_mailer/. Name it similarly to the method in the mailer class. In our example, the name is new-account.html.erb. This template will be used for the HTML formatted emails. Also, you can make a text part for the email. For this, create a new-account.txt.erb file in the same directory. The content for both files is up to you.

  3. Now, you can start configuring ActionMailer. SMTP is a default delivery method. You can adjust it using config.action_mailer.smtp_settings. Other delivery methods are sendmail, file (save emails to files), and test (save emails to ActionMailer::Base.deliveries array). We’re interested in SMTP, so let’s configure it as follows:

    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
      address:              '',
      port:                 25,
      domain:               '',
      user_name:            '<username>',
      password:             '<password>',
      authentication:       'login',
      enable_starttls_auto: true 

    We use the port 25 as it’s set by default. Also, we picked Login as the authentication mechanism. If you use Gmail server for sending emails, your ActionMailer configuration will look as follows:

    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
      :address => "",
      :port => 587,
      :user_name => "your mail",
      :password => "your password",
      :authentication => :plain,
      :enable_starttls_auto => true
  4. Eventually, we are ready to send the email. If you want to do this right away, call deliver_now. Accordingly, you need to call deliver_later to defer the delivery for later. That’s how it looks in practice:


    For multiple recipients, you can simply specify a list of email addresses. This can be done for the :to key, :cc key, or :bcc key. The list can be specified in the form of an array or a single string with email addresses separated by commas.


Ruby Mail

Now, let’s check out how to manage all email-related activities using Ruby Mail. And we’ll start with a code example of a simple email:

mail = do
  from    ''
  to      ''
  subject 'Subject of the message'
  body    'Body text'

SMTP is a default mail delivery method with a local host port 25. But you can customize the SMTP settings:

Mail.defaults do
  delivery_method :smtp, address: "localhost", port: 1025

and change the delivery method:

mail.delivery_method :sendmail


How to send an HTML email with attachment in Ruby Mail?

We already know how to send multi-part emails with ActionMailer. Now, let’s see how to do this in Ruby Mail:

mail = do
  from    ''
  to      ''
  subject 'Subject of the message'
  text_part do
    body 'Body text'
  html_part do
    content_type 'text/html; charset=UTF-8'
    body '<h2>HTML text</h2>'

  add_file '/path/to/file.png'

Quite simple, isn’t it? And there is more to come. The next Ruby gem lets you send an email in one command.


It’s not a fairy tale and you really can use one command to send messages. And here is the example:

Pony.mail(:to => '', :from => '', :subject => 'Subject of the message', :body => 'Body text'

If necessary, the script can be cut to having only :to option key. Also, you can specify a delivery method:




And that’s how the SMTP server configuration looks like. We’ll take Gmail SMTP as an example:

Pony.mail(:to => '', :via => :smtp, :via_options => {
  :address        => '',
  :port           => '587',
  :user_name      => 'user',
  :password       => 'pass',
  :authentication => :plain,
  :domain         => ""

Authorization options include Plain, Login, and Cram_MD5. By default, no authorization is set. 

Unfortunately, Pony won’t let you send a message containing the HTML text or attached with a file.

Wrapping Up

The major part of Rubyists opts for ActionMailer. But it doesn’t mean that you should do either. Pick an option that fits best for your project needs. For some reason, even the simplest way provided by Pony gem can be the perfect one. Good luck!


Dipesh Batheja

