I've created a Rake Task for a Mailer to later scheduled wth Crontab and automate with Gem Whenever for send a email to a users. For a while is a just a test to know if task is working.
My Rake Task and Mailer is the following.
require 'rake'
desc 'send digest email'
task send_warn_course: :environment do
MailCourseWarnMailer.course_available(user).deliver!
end
# And my Mailer is:
class MailCourseWarnMailer < ActionMailer::Base
def course_available(user)
#user = user
mail(to: #user.email, subject: "Curso disponível")
end
end
I passed the parameters to task but this is not working. When I run rake send_warn_course I get the error:
rake send_warn_course
rake aborted!
NameError: undefined local variable or method `user' for main:Object
Anyone knows what is happening? What did I miss here?
As the error suggests you don't have User defined.
You need to input email ID or ID of the user and need to find the user from the database and then pass it to the function.
Something like the following:
require 'rake'
desc 'send digest email'
task :send_warn_course, [:user_email] => :environment do |t, args|
user = User.find_by_email args[:user_email]
MailCourseWarnMailer.course_available(user).deliver!
end
# And my Mailer is:
class MailCourseWarnMailer < ActionMailer::Base
def course_available(user)
#user = user
mail(to: #user.email, subject: "Curso disponível")
end
end
Then, you can run the task like:
rake send_warn_course['user#email.com']
As it is a scheduler task you might not want to pass a variable with each task and instead would like to perform some database queries to find the relevant users. But this was just to demonstrate the fix of the error.
Related
I am setting up a rake task, so I can run it through heroku scheduler when it is in production.
The email sends when I run TaskMailer.task_overdue.deliver_later elsewhere, and I can see from what is printed on the console that the rake task is reaching the right places.
Can anyone help me with why action::mailer is not sending the email when called through a rake task
scheduler.rake:
task :overdue_meeting => :environment do
MeetingsController.send_reminder
end
meetings_controller.rb:
class MeetingsController < ApplicationController
def self.send_reminder
puts "you made it to the send reminder method"
TaskMailer.task_overdue.deliver_later
puts "Now sending you over to the task mailer"
end
end
task_mailer.rb
class TaskMailer < ApplicationMailer
def task_overdue
mail(
from: "info#mysite.com",
to: "redacted#gmail.com",
subject: "All sent from rake"
)
end
end
task_overdue.html.erb
<h1>Meeting overdue email</h1>
<p>When the method is written, this will enclude all overdue meetings</p>
When I run rake overdue_meeting on the console
you made it to the send reminder method
Now sending you over to the task mailer
I have that Mailer method and a Rake task to later scheduled with Cron Job and automate with Gem Whenever for send a email to a users:
# Mailer:
class MailCourseWarnMailer < ActionMailer::Base
def course_available(user)
#user = user
mail(to: #user.email, subject: "Curso disponível") # ... email sending logic goes here
end
end
# Task
require 'rake'
desc 'send digest email'
task :send_warn_course, [:user_email] => :environment do |t, args|
user = MailCourseWarn.find_by_email args[:user_email]
MailCourseWarnMailer.course_available(user).deliver!
end
# Model
class MailCourseWarn < ActiveRecord::Base
belongs_to :course
validates :name, :email, presence: true
end
I am currently run the task like this: rake send_warn_course['user#email.com'] but I need that this be automatic, in a way that when run rake send_warn_course the Rake Task send to all of users in my DB.
Any ideas How can I do that? Thanks.
Find the user(s) that need the email and iterate over them. Surely you're indicating which users need the email in the database somehow, so you'll just want to query all those user records, iterate over them, and then send the email.
task :send_warn_course, [:user_email] => :environment do |t, args|
MailCourseWarn.where(needs_warned: true).each do |user|
MailCourseWarnMailer.course_available(user).deliver!
end
end
I have setup a Task that check for all the followups that are outstanding by a date. I have now send up a task that will run and check for outstanding follow ups for the day and send out an email reminder. All working i think but i can't get the values to show in the Email itself it keep giving me a NilClass error.
rake aborted!
undefined method `company_name' for nil:NilClass
This task i am running through rake at the moment as it will be running through Cron (Whenever gem) which all is working.
Thanks in Advance Code is Below
lib/tasks/daily.rake
namespace :notifications do
desc "Sends notifications"
task :send => :environment do
Followup.where(:closed => false, :quotefdate => (8640.hours.ago..Time.now)).each do |u|
FollowupMailer.followup_confirmation(#followup).deliver
end
end
end
followup_mailer.rb
class FollowupMailer < ActionMailer::Base
default :from => "from#email.com"
def followup_confirmation(followup)
#followup = followup
mail(:to => 'my#email.com', :subject => "Follow up Required")
end
end
followup_confirmation.text.erb
Good Day
Please action this follow up.
<%= #followup.company_name %>
Kind Regards
Mangement
The error source is located in this rake task:
namespace :notifications do
desc "Sends notifications"
task :send => :environment do
Followup.where(:closed => false, :quotefdate => (8640.hours.ago..Time.now)).each do |u|
FollowupMailer.followup_confirmation(#followup).deliver
end
end
end
You're trying to use #followup instance variable, which is unset. Instead, you should use u passed into block:
namespace :notifications do
desc "Sends notifications"
task :send => :environment do
Followup.where(:closed => false, :quotefdate => (8640.hours.ago..Time.now)).each do |u|
FollowupMailer.followup_confirmation(u).deliver # use u variable here
end
end
end
I have a rake task that is meant to call a Mailer and email certain users that meet a given condition. But, when I call the rake task from the console using rake nagging_email:send
I get the following 'ArgumentError: no method name given' and the task does not run. The full console error log can be seen here: https://gist.github.com/srt32/6433024
I have a mailer set up as follows:
class WorkoutMailer < ActionMailer::Base
def nagging_email(user)
#user = user
subject = "What have you done today?"
#url = 'http://frozen-taiga-7141.herokuapp.com/members/sign_in'
mail to: #user.email,
subject: subject.to_s
end
end
and then a rake task as follows that gets all the users that meet a given condition (being lazy) and then calls the Mailer given that user as a param:
namespace :nagging_email do
desc "send nagging email to lazy users"
task :send => :environment do
daily_nag
end
def daily_nag
users = User.all
users.each do |user|
unless last_workout(user) == Date.today
WorkoutMailer.nagging_email(user).deliver
end
end
end
def last_workout(user)
user = user
last_workout = user.workouts.order("date DESC").limit(1)
last_workout_date = last_workout.date
return last_workout_date
end
end
Any help trying to figure out how run this rake task would be appreciated. Thanks.
You should run rake from terminal, not rails console.
If you for some reason want to do it from rails console, you should load tasks like that
require 'rake'
MyRailsApp::Application.load_tasks
Rake::Task['my_task'].invoke
I have recently started seeing the following error message among my rspec output:
undefined method [] for nil:NilClass
Note this does not make the test fail, it just puts this error message to the buffer. I've managed to track down that it is happening due to the mailer. However, I can't seem to find anywhere it might be produced, or any way to see a backtrace.
Excuse the large paste, but I wanted to make sure to include all the relevant code:
## app/models/invite.rb
class Invite < ActiveRecord::Base
include Token
belongs_to :account
after_create :send_email
def send_email
InviteMailer.invite(self).deliver
end
end
## app/mailers/invite_mailer.rb
class InviteMailer < ActionMailer::Base
def invite(invite)
#invite = invite
#account = invite.account
mail to: invite.email, subject: "You're invited to help manage #{#account.name}"
end
end
## spec/models/invite_spec.rb
require 'spec_helper'
describe Invite do
let(:account){create(:account)}
it 'mails the user' do
account.invites.create(email: 'my#email.com')
end
end
## config/environments/test.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :test
## Test Results
› be rspec spec/models/invite_spec.rb
undefined method `[]' for nil:NilClass
undefined method `[]' for nil:NilClass
.
Finished in 0.79803 seconds
1 example, 0 failures
Randomized with seed 46468
Thanks for any help, this one has been driving me crazy.
A suggestion would be to change the let in your invite_spec.rb
let!(:account){create(:account)}
let bang creates it instantly rather than lazily. Anytime I get a nil:NillClass error I check the let. Its easy to overlook.