Ruby on Rails

How to Send Mail in the Future with Rails

This post was originally posted on Max's blog, Maxgrok.

This post assumes you already have setup Sidekiq, Sidetiq, Rails 5.1.2, and are familiar with the command line.

Let's run rails generate mailer InsertMailerName in the command line in the root directory of your Rails app.

This will create the following output in your terminal (minus the 'identical' files, it will just say created then the file name):

# app/mailers/insert_name_mailer.rb
class InsertNameMailer < ActionMailer::Base
  default from: "[email protected]"
  def welcome_email
    mail(to: "[email protected]", subject: "Your Future Email")

Create a Mailer View by making a new file called app/views/insert_name_mailer/welcome_email.html.haml (or just .html if you do not have a .haml linter setup).

Inside that welcome_email.html.haml file, place the following code:

<!DOCTYPE html>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
    <h1>Welcome to!</h1>
      You have successfully signed up to<br>
      To login to the site, just follow this link:<a href=""></a>.
    <p>Thanks for joining and have a great day!</p>

Go to app/workers/name_of_worker_worker.rb

You will see the following:

class NameOfWorkerWorker
  include Sidekiq::Worker
  def perform(*args)
    # Do something

Let's add the mailer in the perform method.

class NameOfWorkerWorker
  include Sidekiq::Worker
  def perform(*args)
    InsertNameMailer.welcome_email.deliver # calls the mailer we made's method and delivers the email immediately

Now, whenever the worker runs, it will send a welcome email.

Let's test this out.

Go to rails c test to load your test environment.

Inside the rails console, run

If successful, you will see a load of text followed by an output that is green and looks like this:

=> #<Mail::Message:LongNumberHere>, Multipart: true, Headers: <Date: TodaysDateHere>, <From: "[email protected]">, <To: "[email protected]">, <Message-ID: <[email protected]'sName>>,<Subject: "Your Future Email">, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==mimepart_(randonstringoflettersandnumbers)"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>

Now, let's add the scheduling to the Sidekiq Worker with one line thanks to Sidetiq!

class NameOfWorkerWorker
  include Sidekiq::Worker
  include Sidetiq::Schedulable # new line includes the Schedulable module from Sidetiq
  recurrence { daily } # makes the worker run daily
  def perform(*args)
    InsertNameMailer.welcome_email.deliver # calls the mailer we made's method and delivers the email immediately

Congratulations your Sidekiq Worker now sends email daily!

Where ever you call your Sidekiq Worker, you will send an email daily in the future.


Ruby on Rails Guides: ActionMailer

Getting Started Sidekiq

Header photo by Kirsty TG on Unsplash

Author image

About Max Goodman

You've successfully subscribed to SmartLogic Blog
Great! Next, complete checkout for full access to SmartLogic Blog
Welcome back! You've successfully signed in.
Unable to sign you in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.