如何在Rails中根据日期时间发送电子邮件?

时间:2016-11-01 16:05:38

标签: ruby-on-rails email delayed-job

我的应用程序中有“发送电子邮件”功能,可以将电子邮件发送到电子邮件地址。

稍后会添加一项新功能,当用户选中“稍后发送电子邮件”单选按钮时,选择日期和时间并单击“发送电子邮件”按钮,以便在特定时间发送电子邮件。 为此,我在数据库中添加了一个字段“send_reports_at”,添加到控制器和所需的操作中。

日期时间存储为:“2016-11-01 16:15”。

如何根据我的应用程序中的日期时间添加发送电子邮件的条件(我是新来的延迟工作和所有)? 请帮忙。

这是我的观点:

<%= f.input :additional_emails, :label => false, :input_html => {:id => 'sponge_contacts', :placeholder => 'Separate emails by comma', :class => 'deliver_page_email', :type => 'text', :rows => 2, :style=> 'width:300px; border-color:#ccc; font-size:13px; padding:10px 0 0 10px; width: 295px; height: 110px; resize: none;'} %>
<input type="radio" id="send_email_0" name="send_email" value="now" checked> Send email now<br>
<input type="radio" id="send_email_1" name="send_email" value="later"> Send email later<br>

    <%= f.input :send_reports_at,:label => false, :input_html => {:placeholder => 'Click here to select date and times', :class => 'deliver_page_email', :type => 'text', :rows => 2} %>
    <% content_for :javascript do %>
        <script type="text/javascript">
          $( function() {
    $( "#report_send_reports_at" ).datepicker();
  } );

            $(document).ready(function() {
                $("#report_send_reports_at").hide();

                $("#send_email_0, #send_email_1").bind('change click',function(){
                    toggleResult();
                });

            });
            function toggleResult(){
                result = $("[name='send_email']:checked").val();
                if(result == "later"){
                    $("#report_send_reports_at").show();
                }else{
                    $("#report_send_reports_at").hide();
                }
            }

        </script>
    <% end %>
<% end %>

<%= link_to "Send Now", '#', :class => "pink_button _round_5", :id => 'send_now_button' %>

这是我的控制器方法:

def send_report_email
    send_to_agents = params.has_key?("send_to_agents") && params["send_to_agents"] == "true"
    if @report.photos.empty?
      Screenshot.delay.new(@user.id, @report.id, Rails.application.config.custom.indicator_screenshot_bucket)
    else
      Screenshot.delay.new(@user.id, @report.id, "photo_screenshots")
    end
     Screenshot.delay.new(@user.id, @report.id, Rails.application.config.custom.report_screenshot_bucket)
    if @report.update_attributes(params[:report])
      set_photo_position(false)
      @report.save   
      if send_to_agents
        @report.update_attribute(:duplicable, true)
        @good_emails, @bad_emails, @unsubscribed_emails = @user.account.users.map(&:email), [], []
      else
        @good_emails, @bad_emails, @unsubscribed_emails = filter_emails(@report.additional_emails)
        @user.delay.add_new_contacts(@good_emails)
      end
      @good_emails.each do |email|
        send_to_agents == true ? ReportMailer.delay.additional_emails(email, @user, @report, "A new report is available: #{@report.title}") : ReportMailer.delay.additional_emails(email, @user, @report)
      end
      ReportMailer.delay.report_sent(@user, @report, @good_emails, @bad_emails, @unsubscribed_emails)
    end

    respond_to do |format|
      format.json { render :json => { :success => true, :report_id => @report.id, :redirect => user_reports_url(current_user), :notice => 'Report was successfully sent!' } }
    end

  end

1 个答案:

答案 0 :(得分:1)

您可以编写一个函数(例如ReportMailer的类方法),该函数发送计划时间与当前时间匹配或小于当前时间的所有电子邮件,然后将cron作业设置为使用{{3}根据需要经常运行该功能。