0
votes

I am trying to send welcome mail when user registers but everything seems fine the same code i have used in different project as well but the mail is not sent :

my application logs :

User Load (1.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 14], ["LIMIT", 1]] [ActiveJob] [ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd] Performing ActionMailer::DeliveryJob from Async(mailers) with arguments: "UserMailer", "user_mailer", "deliver_now",

> [ActiveJob] [ActionMailer::DeliveryJob]

[3a0b9366-b50f-4934-8ca6-89303d1749bd] Rendering user_mailer/user_mailer.html.erb within layouts/mailer [ActiveJob] [ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd]
Rendered user_mailer/user_mailer.html.erb within layouts/mailer (8.0ms) [ActiveJob] [ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd] UserMailer#user_mailer: processed outbound mail in 79.8ms [ActiveJob] [ActionMailer::DeliveryJob] [3a0b9366-b50f-4934-8ca6-89303d1749bd] Performed ActionMailer::DeliveryJob from Async(mailers) in 81.07ms

and my environment.rb file :

require_relative 'application'

# Initialize the Rails application.
Rails.application.initialize!
Rails.application.configure do

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

  if Rails.env.development? || Rails.env.test?
    config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  elsif Rails.env.production?
    config.action_mailer.default_url_options = {:host => ENV['HOST']}
  end

  config.action_mailer.smtp_settings = {   
    :openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,      
    :enable_starttls_auto => true,  #this is the important stuff!

    :address        => 'smtp.gmail.com',

    :port           => 587,
    :authentication => :plain,
    :user_name      => '[email protected]',
    :password       => '********'

   } 
end

my development.rb file :

Rails.application.configure do

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports.
  config.consider_all_requests_local = true

  # Enable/disable caching. By default caching is disabled.
  if Rails.root.join('tmp/caching-dev.txt').exist?
    config.action_controller.perform_caching = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => 'public, max-age=172800'
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.perform_caching = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Suppress logger output for asset requests.
  config.assets.quiet = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # Use an evented file watcher to asynchronously detect changes in source code,
  # routes, locales, etc. This feature depends on the listen gem.
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker


  config.action_mailer.delivery_method = :smtp
end

my user_mailer.rb :

class UserMailer < ApplicationMailer
    default from: '[email protected]'

  def user_mailer(user)
    @user = user
    if Rails.env.development? || Rails.env.test?
        @url  = 'http://localhost:3000/login'       
    elsif Rails.env.production?
        @url  = 'http://client-development-env.fr4s6529gr.us-west-2.elasticbeanstalk.com/login'
    end
    mail(to: @user.email, subject: 'Welcome to Lokavidya')
  end
end

My user_mailer.html.erb :

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1><%= Welcome to Lokavidya %> <%= @user.name %></h1>
    <p>
      <%= Welcome To Lokavidya %> <%= @user.email %>.<br>
    </p>
    <h3>
      <% if Rails.env.development? || Rails.env.test? %>
        <%#= verify_users_url(@user.confirmation_link) %>
        <a href="http://localhost:3000/users/verify?link=<%= @user.confirmation_link%>">

      <% elsif Rails.env.production? %>
        <a href="http://client-development-env.fr4s6529gr.us-west-2.elasticbeanstalk.com/users/verify?link=<%= @user.confirmation_link%>">
      <% end %>

      <%= "Click The Above Link to Verify Your Account" %>
      </a>
    </h3>
    <p><%= "Thanks For Joining Lokavidya" %></p>
  </body>
</html>

and my user controller method :

def create
    @user = User.new(user_params)
    @user.confirmation_link = confirmation_link!
    @user.uuid = unique_id!
    if @user.password == @user.password_confirmation
      if @user.save
        UserMailer.user_mailer(@user).deliver_later
        msg  = {:status => 200, :uuid => @user.uuid, :message => "User created Succesfully" }
        return render :json => msg
      else
        render json: @user.errors, status: :unprocessable_entity
      end
    end
  end

Please tell me what i am doing wrong here I am really stuck at it.

1

1 Answers

1
votes

changed below line:

UserMailer.user_mailer(@user).deliver_later

to

UserMailer.user_mailer(@user).deliver

and check the if mail send or not.

if you want to send mails in background then you have to add Gem delayed_job or sidekiq to your Gemfile.

Smtp setting:

config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
      :address => "smtp.gmail.com",
      :port => 587,
      :domain => "gmail.com",
      :user_name => "[email protected]",
      :password => "xxxxxx",
      :authentication => "plain",
      :enable_starttls_auto => true
  }