【问题标题】:Rails send an automatic reminder email 3 days after field updateRails 在字段更新后 3 天发送自动提醒电子邮件
【发布时间】:2016-08-08 17:31:11
【问题描述】:

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

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
    

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

1) 授予访问权限后,标记用户在 3 天内查看。

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

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

我从来没有做过这样的事情,所以我很难找到解决方案。非常感谢您的建议!

【问题讨论】:

    标签: ruby-on-rails ruby email cron


    【解决方案1】:

    我已经成功使用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>
    

    【讨论】:

      【解决方案2】:

      您可以使用 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
      

      【讨论】:

        【解决方案3】:

        whenever 是一个很好的宝石。但它需要unix 平台。除此之外,无论何时都不适用于heroku。 另一个不错的选择是Clockwork Gem

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

        您甚至可以使用此 gem 来安排您的后台作业。

        【讨论】:

        猜你喜欢
        • 2018-01-15
        • 2014-09-27
        • 2014-02-12
        • 2012-08-14
        • 1970-01-01
        • 2018-12-09
        • 2019-09-12
        • 1970-01-01
        • 2017-12-01
        相关资源
        最近更新 更多