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
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'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.
I want send an SMS each 5 minutes to my users. At the moment, my application sends an SMS during the creation of an account.
# users_controller.rb
def create
#user = User.new(user_params)
if #user.save
#user.send_activation_email
#user.send_daily_sms
flash[:info] = "Veuillez contrôler votre boîte mail pour activer votre compte."
redirect_to root_url
else
render 'new'
end
end
# user.rb
def send_daily_sms
# put your own credentials here
account_sid = '**********************'
auth_token = '**********************'
# set up a client to talk to the Twilio REST API
#client = Twilio::REST::Client.new account_sid, auth_token
#client.account.messages.create({
:from => '**********',
:to => '***********',
:body => 'Salut',
})
end
I already have scheduled mails working in my project by doing this :
# schedule.rb
every :day, :at => '12pm' do
rake "email_sender_daily"
end
# My task
task :email_sender_daily => :environment do |_, args|
User.find_each do |user|
UserMailer.daily_mail(user).deliver_now if user.daily == true
end
end
# My UserMailer
def daily_mail(user)
#user = user
mail to: user.email, subject: "Mail journalier"
end
I'm showing you this because, with the UserMailer, I know how to access it from an other file. Here, I'd like to do the exactly the same for SMS, but how can I access the method that is in my Model ? If not, where can I put this method to be able to access it from my rake task ?
Twilio developer evangelist here.
It looks to me like you have all the parts you need. If send_daily_sms is a method in your User class then all you require is a rake task like so:
task :sms_sender_daily => :environment do |_, args|
User.find_each do |user|
user.send_daily_sms if user.daily == true
end
end
And then your schedule.rb would look like:
every :day, :at => '12pm' do
rake "email_sender_daily"
rake "sms_sender_daily"
end
I would warn that sending sms messages to all your users via one method that calls the API over and over again is somewhat fragile. If one message fails to send because of a timeout or some other error then the task will throw an error and not be able to complete sending all the messages.
I'd suggest sending both emails and sms messages by workers using a background queue, like Rails's ActiveJob. If you are on the latest Rails 4.2 then you can use a gem called Textris that works much like ActionMailer and then you could define a UserTexter class like this:
class UserTexter < Textris::Base
default :from => YOUR_NUMBER
def daily_sms(user)
#user = user
text :to => #user.phone_number
end
end
Then your tasks could look like this:
task :email_sender_daily => :environment do |_, args|
User.find_each do |user|
UserMailer.daily_mail(user).deliver_later if user.daily == true
end
end
task :sms_sender_daily => :environment do |_, args|
User.find_each do |user|
UserTexter.daily_sms(user).deliver_later if user.daily == true
end
end
Check out the Textris documentation for more on how to use the gem.
Let me know if this helps at all!
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