【问题标题】:How to send email based on date time in Rails?如何在 Rails 中根据日期时间发送电子邮件?
【发布时间】:2017-03-14 19:39:19
【问题描述】:

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

稍后添加了一个新功能,当用户选中“稍后发送电子邮件”单选按钮时,选择日期和时间并单击发送电子邮件按钮,以便在特定时间发送电子邮件。 为此,我在数据库中添加了一个字段,其中包含“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

【问题讨论】:

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


    【解决方案1】:

    您可以编写一个函数(例如 ReportMailer 的类方法)发送所有预定时间匹配或小于当前时间的电子邮件,然后设置一个 cron 作业以使用 rails runner 来运行它尽可能频繁地发挥作用。

    【讨论】:

      猜你喜欢
      • 2015-08-28
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 2013-10-04
      • 2015-09-20
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多