Discover Our Blog

Bulk Mailing Using Sidekiq

Bulk Mailing Using Sidekiq


Sometimes the rails application requires sending the bulk mails using action mailers on the single attempt. The user can be able to send them seamlessly. But, this task is not possible in synchronous way. This will affect whole application performance and will take a lot of time to load the respective pages.

To overcome this problem rails community have enormous kinds of solutions. Among them  sidekiq is the best choice to overcome this kind of problems.

The sidekiq is a full-featured background processing framework for Ruby. It aims to be simple to integrate with any modern Rails application and much higher performance than other existing solutions.

The Redisis an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperlog logs and geospatial indexes with radius queries.

The Action Mailer is a service provided by rails to send the mails on respective emails ids. To send an email we must have to invoke deliver method of actionmailer.

To send mails asynchronously we have to use the sidekiq.

Integration Setup:

Add sidekiq into your rails application by including it into your Gemfile and run the bundle command.


gem ‘sidekiq’

To run sidekiq, run the following command:

$ bundle exec sidekiq -d -L log/sidekiq.log -C config/sidekiq.yml(default development mode)

$ bundle exec sidekiq -d -L log/sidekiq.log -C config/sidekiq.yml -e production (production mode)

Sending Delayed Emails:

                Sidekiq provides asynchronous emails sending provision with Action Mailer. That will be performed by adding below three methods to the ActionMailer module.


The .delay method will call the action mailer method and add this mail into to DelayedMailer queue for processing.

                                For ex.,   NewsMailer.delay.newsmailer(@email)


Here, user can be proviode inerval time before sending emails asynchronously.

                                For ex.,   NewsMailer.delay_for(


Here, user can set the timestamp for sidekiq. Sidekiq will wait until the provided time to send the email.

For ex.,   NewsMailer.delay_until(

Sending Bulk delayed Emails:

The below example illustrated the way to send bulk emails.


Here., ApplicationMailer is a class have new_article_mail(article) method. We used .delay method to                           set the delay for sending mail asynchronously.

# Send an email to all the users if new project has been posted in website.

		def new_article_mail(article)
			@article = article
			all_users("New article added in GXP")
		def all_users(subject){ |user|
		       @user = user
		       send_mail(, subject).deliver

		def send_mail(email, subject)
		    mail to: email, subject: subject

Here, new_article_mail method has one more method all_users in which we are calling send_mail method for sending an email to the individual user.


Leave a comment: