Rails在现场更新后3天发送自动提醒电子邮件

时间:2016-04-16 20:15:10

标签: ruby-on-rails ruby email cron

我有一个应用程序,用户和项目之间的关联。当用户注册时,他们会收到快速调查。如果调查表明它们与我们的网站很匹配,则会被授予访问权限并发送电子邮件,要求他们填写他们的个人资料和项目信息。这是处理该操作的控制器。

class Admin::Users::GrantAccessesController < ApplicationController
  def create
    @user = User.find(params[:id])
    if @user.update_attribute(:access_granted, true)
      if params[:email] == "true"
        @user.send_access_granted_email
      end
      redirect_to admin_users_path
    else
     redirect_to admin_users_path
    end
  end

我想设置一个命令,如果他们尚未填写他们的项目,他们会在获得访问权后3天自动向某人发送提醒。我只想给他们发一封提醒。

我有一些想法,我希望每个人都能给我最好的建议。

  1. 使用gem时设置一个cron作业(有问题,因为我只想发送一封电子邮件而不是无限的电子邮件,直到他们更新他们的项目。我也不知道如何将if语句添加到cron作业)

    every 1.day, :at => '4:30 am' do
      runner "Task.send_email" (if access granted and project not updated/created)
    end
    
  2. 使用UserMailer发送电子邮件(我不知道如果没有项目,如果在授予访问权限后3天触发电子邮件将被发送)

       # Sends project_reminder e-mail
       def send_project_reminder_email
        UserMailer.project_reminder(self).deliver_now
       end
    
  3. 如果更新了granted_access,请在用户模型上添加以下内容以执行操作。

    def update
     @model = Model.find(params[:id])
     detect_changes
    
     if @model.update_attributes(params[:model])
      send email if attr_changed?
     end
    end  
    
    private
    
    def detect_changes
      @changed = []
      @changed << :attr if @model.attr != params[:model][:attr]
    end
    
    def attr_changed?
      @changed.include :attr
    end
    

    这些解决方案似乎都没有真正解决整个问题。我认为步骤是

  4. 1)授予访问权限后,标记用户在3天内进行检查。

    2)3天后检查项目是否已更新。

    3)如果更新什么都不做,如果没有更新则发送电子邮件。

    我从未做过这样的事情,所以我很难在解决方案中解决问题。非常感谢你的建议!

3 个答案:

答案 0 :(得分:2)

我已成功使用whenever这样:

在config / schedule.rb中(安排你的任务运行):

every 1.day, :at => "9am" do
  rake "notifications:send"
end

在lib / tasks / notifications.rake中(您在UserMailer中为每个需要发送的电子邮件调用您的方法的实际任务):

namespace :notifications do
  desc "Sends daily notifications to admins and users"
  task :send => :environment do
    User.all.each do |user|
      # This is where to decide whether to send to any given individual user
      UserMailer.daily_stakeholder_events_to_user(user).deliver
    end
  end
end

在app / mailers / user_mailer.rb中(现在发送一封电子邮件):

  def daily_stakeholder_events_to_user(user)
    user.email = "email@example.com"
    subject = "Daily Email Alert"
    @user = user
    @events = []

    email_with_name = "#{@user.full_name} <#{@user.email}>"
    puts "Emailing #{subject} to #{email_with_name}"
    mail(:to => email_with_name, :subject => subject)
  end

最后,在app / views / user_mailer / daily_stakeholder_events_to_user.html.erb(您的电子邮件正文的HTML)中:

<p>Hello <%= @user.full_name %>!</p>
<p>You have <%= @events.count %> <%= "event".pluralize(@events.count) %>.<p>

答案 1 :(得分:0)

您可以使用Ruby的库中的Time/day方法,添加发送电子邮件的日期,记录日期,如果电子邮件发送72小时,则让方法发送电子邮件

require 'date'

def three_days_later
    File.open('email_log.txt', 'r').each_line do |check|
      # find the email address
      if check.include?(04/17/16) 
        #If the email includes the same day, don't send
        puts "Hasn't been 72 hours.."
      else 
        #resend email
    end
  end
end

然后从您的电子邮件方式:

class Admin::Users::GrantAccessesController < ApplicationController
  def create
    @user = User.find(params[:id])
    if @user.update_attribute(:access_granted, true)
      if params[:email] == "true"
        @user.send_access_granted_email
      end
      redirect_to admin_users_path
      #log the data for the time information
    else
     redirect_to admin_users_path
    end
  end

答案 2 :(得分:0)

whenever是一个很好的宝石。但它需要unix平台。除此之外,只要不与heroku一起使用。 另一个不错的选择是Clockwork Gem

require 'clockwork'

module Clockwork
  every(10.seconds, 'frequent.job')
end

您甚至可以使用此gem安排后台工作。

相关问题