У меня есть функция «отправить письмо» в моем приложении, которое отправляет электронное письмо на адреса электронной почты.Как отправить письмо по дате в Rails?
Позже добавлена новая функциональность, когда пользователь проверяет переключатель «Отправить по электронной почте позже», выбирает дату и время и нажимает кнопку отправки электронной почты, чтобы отправить электронное письмо в определенное время. Для этого я добавил поле в базу данных с столбцом «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