The Rails 6 Send Mail Backgtound Job is a small task for everyone to practice more when programming with Ruby on Rails.
We will use gem sidekiq to perform this task.
Support this project 😜 🙏
- Ruby on Rails
We are going to build the web application using:
- Rails 6.0.3.2
- Ruby 2.7.1
Since you already know the call to your Application will be slow, you want it to happen in a background job, so that users can see the confirmation message in their browser immediately without having to wait for the charge to actually happen. To do this, you must create an Active Job class, implement that class to call charge!(), and then add code to the controller to execute this job. The flow looks like the following figure.
$ git clone https://github.com/TanHongIT/send_mail_background_job
$ cd send_mail_background_job
$ bundle install
$ yarn install
You must change the appropriate database configuration
Change configuration at "config/database.yml" with Postgresql.
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
username: send_mail_background_job
password: 1234
host: localhost
# tutorial for ubuntu linux:
# sudo -u postgres psql
# create user "send_mail_background_job" with password '1234';
# create database "send_mail_background_job" owner "send_mail_background_job";
development:
<<: *default
database: send_mail_background_job
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: send_mail_background_job_test
production:
<<: *default
database: send_mail_background_job_production
You must change the username, password and database name accordingly!
$ rails db:migrate
Run console :
$ bundle exec figaro install
Go to config directory and add to file "config/application.yml"
user_mail: 'yourmail@gmail.com'
password: 'your password'
Change your mail in app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: 'your_mail'
layout 'mailer'
end
$ bundle exec sidekiq
Open new terminal and run:
rails s
Now go to at http://localhost:3000/users/new and put your mail end waiting for a response to arrive