Skip to content

Instantly share code, notes, and snippets.

@JuarezLustosa
Forked from maxivak/00.md
Created November 19, 2018 15:56
Show Gist options
  • Select an option

  • Save JuarezLustosa/7e39f62fd9cd72d035c8f2fa427b2ee0 to your computer and use it in GitHub Desktop.

Select an option

Save JuarezLustosa/7e39f62fd9cd72d035c8f2fa427b2ee0 to your computer and use it in GitHub Desktop.
Sending emails with ActionMailer and Sidekiq

Sending emails with ActionMailer and Sidekiq

Send email asynchroniously using Sidekiq.

ActionMailer

Create your mailer us usual:

# app/mailers/users_mailer.rb

class UsersMailer < ActionMailer::Base

def welcome_email(user_id)
    @user = User.find(user_id)

    mail(   :to      => @user.email,
            :subject => "Welcome"
    ) do |format|
      format.text
      format.html
    end
  end
end

Send email:

user = User.find(1)

mail = UsersMailer.welcome_email(user.id)
#mail.deliver_now
mail.deliver_later

    

Sidekiq

Gemfile:

gem 'sidekiq'

Install Redis

Redis provides data storage for Sidekiq. It holds all the job data along with runtime and historical data

Configure Sidekiq

Run Sidekiq

bundle exec sidekiq --environment development -C config/sidekiq.yml 

God + Sidekiq

Use god for monitoring and running sidekiq automatically: https://gist.github.com/maxivak/05847dc7f558d5ef282e

Sidekiq and Devise

Read this: https://github.com/mperham/sidekiq/wiki/Devise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment