ruby-on-rails 如何在Rails中基于日期时间发送电子邮件?

utugiqy6  于 2023-04-13  发布在  Ruby
关注(0)|答案(2)|浏览(140)

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

nzkunb0c1#

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

vwhgwdsa

vwhgwdsa2#

使用deliver_later方法和wait_until选项,如下所示

.deliver_later(wait_until: your_stored_datetime)

相关问题