how to keep sending emails to users every week depending on user date input in rails
Use cron to schedule a repeating rake task. If you're using heroku, you can get cron as an add-on. But first, of course, you need to write the rake task -- for tips:
http://railscasts.com/episodes/66-custom-rake-tasks/
http://jasonseifer.com/2010/04/06/rake-tutorial
Long and the short - rake is a file that allows you to define various tasks and establish dependencies among those tasks. It's perfect for administrative/cleanup tools, or, in your case, something outside the actual execution of your application.
i want system sends email to users every week depending on their date input in rails
You need to write a rake task and call that task through cron.
For example:
task :send_email => :environment do
Dop.where('last_email <= ?', 7.days.ago).each do |dop|
UserMailer.welcome_email(dop).deliver
dop.update_attribute('last_email', Time.now)
end
end
Now set up your machines Cron scheduler to call this Rake task every few minutes to send out emails automatically.
Cron is a Unix scheduler that executes programs in pre-determined intervals.
You need to edit the crontab file using crontab -e
and enter something like this:
*/5 * * * * /bin/bash cd <path_to_your_app> && rake send_email
The */5 * * * *
says that you run it every 5 minutes.
Sending automated mails
This is a design issue, and it may come down to whether it becomes more cumbersome to break the scheduling behavior into multiple classes. You will avoid a case statement, which is an indication that polymorphism could have been used.
If you do, however, choose to use separate classes, this is a good time for the ActiveRecord implementation of the type field. Each subscriber belongs to a schedule. (I'd recommend a schedule has one subscriber.)
The schedules table would have a type field, with the name of the class. You don't have to set the type. Instead, to store the schedule, create the schedule object using the association from the subscriber object, and ActiveRecord will set the type field.
Now, whenever you get a schedule object from the database, it will be the class specified in the type field. You can have class for day of the week, a different one for date of the month, and as many different classes as different date calculation behaviors.
Do store the next due email date. This should be in the schedules table, but you could put it in the subscribers table, if that's more convenient.
Use a method to calculate the next due date. Make sure that method has the same signature in all the classes. So, for the monthly schedule, store the date of the month in the table and do not pass the date of the month to the common method. That way the delivery code can call the method to update the due date, without having to know anything about the schedule algorithm or data.
sending emails based on date
In my view, the UserMailer should only be concerned with the actual creating and shipping of the e-mail. If UserMailer is called, you should already have decided that you want the e-mail to ship.
So in your case, I assume you have a create action in a controller, which instructs the Mailer to send the e-mail. Here you simply encapsulate this action in an if statement.
date = Date.civil(params[:date][:year].to_i, params[:date][:month].to_i, params[:date][:day].to_i)
if date + 7.days == Date.today
UserMailer.welcome_email(@dop).deliver
end
this asumes you use something like this in your form:
<%= select_date Date.today, prefix: :date %>
Rails send mail to a user every day
You should create a rake
task something like:
desc "Send email to users"
task :email_sender => :environment do |_, args|
User.find_each do |user|
UserMailer.daily_mail(user).deliver if <YOUR_LOGIC_TO_CHECK_IF_YOU_NEED_TO_SEND_EMAIL>
end
end
In config/schedule.rb
:
every :day, :at => '12pm' do
rake "email_sender"
end
Sending emails based on intervals using Ruby on Rails
I would use DelayedJob for this ( assuming you are not sending large number of emails emails a day, i.e. 100's of thousands per day etc.)
class Email < ActiveRecord::Base
belongs_to :campaign
after_create :schedule_email_dispatch
def schedule_email_dispatch
send_at(campaign.created_at + self.days.days, :send_email)
end
def send_email
end
end
Run the workers using the rake task:
rake jobs:work
Every time a new Email object is created a delayed job item is added to the queue. At the correct interval the email will be sent by the worker.
@campaign = Compaign.new(...)
@campaign.emails.build(:days => 1)
@campaign.emails.build(:days => 2)
@campaign.save # now the delay
In the example above, two delayed job entries will be created after saving the campaign. They are executed 1 and 2 days after the creation date of the campaign.
This solution ensures emails are sent approximately around the expected schedule times. In a cron job based solution, disptaching happens at the cron intervals. There can be several hours delay between the intended dispatch time and the actual dispatch time.
If you want to use the cron approach do the following:
class Email < ActiveRecord::Base
def self.dispatch_emails
# find the emails due for dispatch
Email.all(:conditions => ["created_at <= DATE_SUB(?, INTERVAL days DAY)",
Time.now]).each do |email|
email.send_email
end
end
end
In this solution, most of the processing is done by the DB.
Add email.rake
file in lib/tasks
directory:
task :dispatch_emails => :environment do
Email.dispatch_emails
end
Configure the cron to execute rake dispatch_emails
at regular intervals( in your case < 24 hours)
Most reliable way to deliver emails from a users email address in rails?
I believe SendGrid support customising the From address. Heroku provide them as an add-on and they advertise "Complete control over the From: address." on their add-ons page
Related Topics
Issue with Precision of Ruby Math Operations
Error Occurs When Trying to Install Homebrew on a MAC for Ruby on Rails
Rails "Template Is Missing" Error
Scope That Has Three Levels Deep Joins
Rexml::Document.New How to Give Encode Parameters on This Line
Require Self Made Gem in Jruby Fails After Update to Jruby-1.7.13
Ruby: Sorting 2 Arrays Using Values from One of Them
Ruby: How to Remove Items from Array a If It's Not in Array B
How to Require Activerecord in Irb
Rails Cors Issue with Ajax API Endpoint Request
Rails Console Is Adding Nil Instead of Values
Bug in Implemented Tagging System
Rails 3 Translations Within Models in Production
Devise 'Find_First_By_Auth_Conditions' Method Explanation
Open Firefox Browser with Ruby Automation Script
Gracefully Shutting Down Sidekiq Processes
Rspec Error 'Report_Activate_Error': Could Not Find Rubygem Rspec-Core (>=0) (Gem:Loaderror)