0

I am making an application in which user will get a mail after every minute but the problem it is not getting any mail.I have installed the whenever gem and ran the bundle install command.After that i ran wheneverize . command and schedule.rb file generated and given below are the files.

UsersController

class UsersController < ApplicationController
# GET /users
# GET /users.json


def index 
@users = User.all

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @users }
end
end

# GET /users/1
# GET /users/1.json
def show
@user = User.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @user }
end
end

# GET /users/new
# GET /users/new.json
def new
@user = User.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @user }
end
end

# GET /users/1/edit
def edit
@user = User.find(params[:id])
end

# POST /users
# POST /users.json
def create
@user = User.new(params[:user])

respond_to do |format|
  if @user.save


    format.html { redirect_to(@user, :notice => 'User was successfully created.') }
    format.json { render :json => @user, :status => :created, :location => @user }
  else
    format.html { render :action => "new" }
    format.json { render :json => @user.errors, :status => :unprocessable_entity }
  end
end
end


# PUT /users/1
# PUT /users/1.json
def update
@user = User.find(params[:id])

respond_to do |format|

    format.html { redirect_to @user, notice: 'User was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
@user = User.find(params[:id])
respond_to do |format|
  if @user.destroy
    # Tell the UserMailer to send a welcome Email after save


    format.html { redirect_to(@user, :notice => 'User was successfully created.') }
    format.json { render :json => @user, :status => :created, :location => @user }
  else
    format.html { render :action => "new" }
    format.json { render :json => @user.errors, :status => :unprocessable_entity }
  end
end
end

end

Schedule.rb

every 1.minute do
runner "User.send_email_to_user"
end

And this is Model user.rb

class User < ActiveRecord::Base
attr_accessible :email, :login, :name, :Notification

def self.send_email_to_user
UserMailer.welcome_email(@user).deliver
end
end

And this is User_mailer.rb

class UserMailer < ActionMailer::Base
default from: "anasjmh@gmail.com"

def welcome_email(user)
@user = user
@url  = "http://gmail.com/login"
mail(:to => user.email, :subject => "Welcome to My Awesome Site")
end
end

Can anyone tell me where is the problem????? Any help would be appreciated...

Thanks in advance.

Mohd Anas
  • 634
  • 1
  • 9
  • 22

1 Answers1

0

does the UserMailer itself work? I have something in mind, that you need to call the deliver method after the mail method, the mail method only prepares the message:

mail(:to => user.email, :subject => "Welcome to My Awesome Site").deliver

And have you configured the action_mailer properly? You can config it to display errors, e.g. in development.rb:

config.action_mailer.default_url_options = { :host => 'yourdomain.com' }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
23tux
  • 14,104
  • 15
  • 88
  • 187
  • yes UserMailer works fine...you mean that remove the deliver method from the user model and place it in the mailer class..right??Do you think this is the problem of enviornment????? – Mohd Anas Nov 21 '12 at 09:48
  • Maybe it could be the environment, what settings do you have? I'm not sure where to put the ``.deliver`` method, but it has to be called ;) – 23tux Nov 21 '12 at 10:14
  • config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp config.action_mailer.default_url_options = { :host => 'www.gmail.com' } config.action_mailer.perform_deliveries = true config.active_support.deprecation = :log config.action_mailer.smtp_settings = { :address => 'smtp.gmail.com', :port => 25, :domain => 'www.gmail.com', :authentication => :login, :user_name => '****8', :password => '*******' } – Mohd Anas Nov 21 '12 at 10:18
  • are the mails delivered? Because I'm also using gmail for sending mails, and I have different settings. If its a general problem, I can give you my configuration. And please, use the code formatters ``your code`` – 23tux Nov 21 '12 at 10:20
  • if i use simple mailer notification application mails are delieverd but when i use whenever gem to send mails after a particular time mails are not delievered... – Mohd Anas Nov 21 '12 at 10:36
  • could you write out some logs out of the crone job? Maybe it throws errors and you don't notice them – 23tux Nov 21 '12 at 10:37
  • yes ...when i ran whenever command it displays * * * * * /bin/bash -l -c 'cd /root/Desktop/Rails/email2 && script/rails runner -e development '\''User.send_email_to_user'\''' ## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated. ## [message] Run `whenever --help' for more options. – Mohd Anas Nov 21 '12 at 10:58