Saturday 18 November 2017

Send email using active jobs in rails with sendgrid credentials


step 1. rails g scaffold user name:string email:string

step 2. rails generate mailer UserMailer

step 3. put method in user mailer
   
      def welcome_email(user)
        @user = user
        mail(to: @user.email, subject: 'Welcome to My Awesome Site')
      end
step 4. need to set SMTP setting in development.rb and production.rb file
    I am sending email using sendgrid credentials.
   
    config.action_mailer.delivery_method = :smtp
      config.action_mailer.smtp_settings = {
      :user_name => 'sendgrid username',
      :password => 'sendgrid password',
      :domain => 'localhost',
      :address => 'smtp.sendgrid.net',
      :port => 587,
      :authentication => :plain,
      :enable_starttls_auto => true
     }
step 5. rails g job user

step 6. copy and paste this line or you can define according to your class mailer and method name.
   
    class UserJob < ApplicationJob
      queue_as :default

      def perform(user)
          user_email = user
        ProductMailer.welcome_email(user_email).deliver_now
      end
    end
step 7. copy and paste below line in you controller method or model method where you want to use.
   
    UserJob.set(wait: 2.minutes).perform_later(current_user)

No comments:

Post a Comment

Revert last commit or second last and more....

 Git revert commit_id -m 1 this command willl revert last commit  Git revert commit_id -m 2 this command will revert second commit with same...