New plugin: email customers appointment reminders

Reminders for redmine_qbo_calendar appointments, to the customer's email in
QuickBooks, sent from your own mailbox through Google Workspace's SMTP relay
(or Gmail SMTP with an app password, or Redmine's email settings).

- Schedule: N days before at a set time, only within sending hours, not within
  N hours of the appointment; rescheduling sends one for the new time
- Plain text + HTML from an editable template with placeholders and sections,
  calendar (.ics) attachment, Message-ID on the sending domain
- Each reminder claimed in the database before sending (no duplicates across
  processes); failures retried 3 times
- Appointment page: status and Send now / Send again; customer page: turn
  reminders off; settings: test email, recent reminders, send due now
- Runs on page loads every 10 minutes, or from cron via
  rake redmine:appointment_reminders:send
- README covers Google Workspace setup and SPF/DKIM/DMARC

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-31 22:30:12 -04:00
commit 7c09b58708
28 files changed
+1482

No files matched your search

@@ -0,0 +1,75 @@
#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Sending a reminder from an appointment, turning reminders on or off for a customer, and (administrators)
# sending a test email or running the reminders from the settings page
class AppointmentRemindersController < ApplicationController
before_action :authorize_global, only: [:send_now, :opt_out, :opt_in]
before_action :require_admin, only: [:test, :run]
def send_now
appointment = CustomerAppointment.find(params[:id])
reminder = RedmineQboReminders::Sender.new.send_now(appointment, user: User.current)
if reminder.nil?
flash[:error] = l(:error_reminder_no_email)
elsif reminder.sent?
flash[:notice] = l(:notice_reminder_sent, email: reminder.email)
else
flash[:error] = l(:error_reminder_failed, error: reminder.error.to_s)
end
redirect_to customer_appointment_path(appointment)
end
def opt_out
customer = Customer.find(params[:customer_id])
ReminderOptOut.find_or_create_by!(customer_id: customer.id) { |o| o.user = User.current }
flash[:notice] = l(:notice_reminders_off, customer: customer.name)
redirect_back_or_default customer_path(customer)
end
def opt_in
customer = Customer.find(params[:customer_id])
ReminderOptOut.where(customer_id: customer.id).delete_all
flash[:notice] = l(:notice_reminders_on, customer: customer.name)
redirect_back_or_default customer_path(customer)
end
# A sample reminder to the address given (or the administrator's own), with the settings as they are on
# the settings page, saved or not (its Send test email button submits the form here)
def test
settings = RedmineQboReminders::Settings.current
if params[:settings].respond_to?(:to_unsafe_h)
settings = RedmineQboReminders::Settings.new(Setting.plugin_redmine_qbo_reminders.to_h.merge(params[:settings].to_unsafe_h))
end
email = params[:email].to_s.strip.presence || User.current.mail
starts_at = settings.zone.now.beginning_of_day + 1.day + 9.hours
message = RedmineQboReminders::Message.new(
settings: settings, starts_at: starts_at, uid: "test-#{Time.now.to_i}@#{settings.mail_domain}",
values: { 'customer' => 'Jane Doe', 'first_name' => 'Jane', 'vehicle' => '2022 Jeep Gladiator',
'subject' => 'Sample appointment' }.merge(RedmineQboReminders::Message.when_values(starts_at))
)
AppointmentReminderMailer.reminder(message, email).deliver_now
flash[:notice] = l(:notice_reminder_test_sent, email: email)
rescue StandardError => e
flash[:error] = l(:error_reminder_failed, error: "#{e.class}: #{e.message}")
ensure
redirect_to plugin_settings_path(:redmine_qbo_reminders)
end
def run
if RedmineQboReminders::Settings.current.enabled?
flash[:notice] = l(:notice_reminders_run, result: RedmineQboReminders::Sender.run.to_s)
else
flash[:error] = l(:error_reminders_disabled)
end
redirect_to plugin_settings_path(:redmine_qbo_reminders)
end
end
+18
View File
@@ -0,0 +1,18 @@
#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Sends the reminders that are due (see RedmineQboReminders::Sender)
class AppointmentRemindersJob < ActiveJob::Base
queue_as :default
def perform
RedmineQboReminders::Sender.run
end
end
@@ -0,0 +1,36 @@
#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'securerandom'
# The reminder email: plain text and HTML, with the appointment as a calendar file. Sent through the SMTP
# server in the plugin's settings (e.g. Google Workspace's SMTP relay), or through Redmine's own email
# settings. Not Redmine's Mailer, which would add Redmine's From address, headers and footer.
class AppointmentReminderMailer < ActionMailer::Base
layout false
# So a refused message is recorded as failed (and retried), not as sent
self.raise_delivery_errors = true
# message: a RedmineQboReminders::Message
def reminder(message, email, settings: message.settings)
if (ics = message.ics)
attachments['appointment.ics'] = { mime_type: 'text/calendar; method=PUBLISH; charset=UTF-8', content: ics }
end
# A Message-ID on the sending domain, rather than the server's host name
headers['Message-ID'] = "<reminder.#{Time.now.to_i}.#{SecureRandom.hex(6)}@#{settings.mail_domain}>"
options = { to: email, from: settings.from, subject: message.subject }
options[:reply_to] = settings.reply_to if settings.reply_to
mail(options.merge(settings.delivery_options)) do |format|
format.text { render plain: message.text }
format.html { render html: message.html.html_safe }
end
end
end
+42
View File
@@ -0,0 +1,42 @@
#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# A reminder emailed (or being emailed, or that failed) for an appointment at a given time
class AppointmentReminder < ActiveRecord::Base
STATUSES = %w[pending sent failed].freeze
# Tries before a failing reminder is given up on (it can still be sent from the appointment page)
MAX_ATTEMPTS = 3
# A "pending" row older than this was left by a run that died mid-send, and may be tried again
STALE_AFTER = 30 * 60
belongs_to :customer_appointment
belongs_to :sent_by, class_name: 'User', optional: true
validates :status, inclusion: { in: STATUSES }
scope :recent, -> { order(created_at: :desc) }
def sent?
status == 'sent'
end
def failed?
status == 'failed'
end
def stale?
status == 'pending' && updated_at && updated_at < Time.now - STALE_AFTER
end
# Worth another try on schedule
def retry?
(failed? || stale?) && attempts < MAX_ATTEMPTS
end
end
+21
View File
@@ -0,0 +1,21 @@
#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# A customer who doesn't get appointment reminders
class ReminderOptOut < ActiveRecord::Base
belongs_to :customer
belongs_to :user, optional: true
validates :customer_id, presence: true, uniqueness: true
def self.opted_out?(customer)
customer.present? && exists?(customer_id: customer.id)
end
end
@@ -0,0 +1,40 @@
<%# The reminder's status on an appointment's page (redmine_qbo_calendar's show_customer_appointment hook) %>
<% appointment = local_assigns[:appointment] %>
<% if appointment %>
<% rs = RedmineQboReminders::ReminderStatus.for(appointment) %>
<% can_send = User.current.allowed_to?(:send_appointment_reminders, nil, global: true) %>
<div class="box appointment-reminder appointment-reminder-<%= rs.state %>">
<i class="fa-regular fa-envelope"></i>
<strong><%= l(:label_appointment_reminder) %>:</strong>
<% text = case rs.state
when :sent then l(:text_reminder_sent, time: format_time(rs.reminder.sent_at), email: rs.email)
when :sending then l(:text_reminder_sending, email: rs.email)
when :failed then l(:text_reminder_failed_retry, email: rs.email)
when :given_up then l(:text_reminder_failed, email: rs.email)
when :scheduled then l(:text_reminder_scheduled, time: format_time(rs.at), email: rs.email)
when :off then l(:text_reminder_off)
when :opted_out then l(:text_reminder_opted_out)
when :status then l(:text_reminder_status, status: appointment.status.to_s.humanize)
when :no_customer then l(:text_reminder_no_customer)
when :no_email then l(:text_reminder_no_email)
else l(:text_reminder_too_late)
end %>
<%= text %>
<% if rs.sent? && rs.reminder.sent_by %>(<%= rs.reminder.sent_by %>)<% end %>
<% if [:failed, :given_up].include?(rs.state) %><em><%= rs.reminder.error %></em><% end %>
<% if can_send && appointment.customer && [:sent, :scheduled, :failed, :given_up, :too_late].include?(rs.state) %>
<%= button_to (rs.sent? ? l(:button_send_reminder_again) : l(:button_send_reminder_now)),
send_appointment_reminder_path(appointment), method: :post, form_class: 'appointment-reminder-send',
data: (rs.sent? ? { confirm: l(:text_reminder_send_again_confirm) } : {}) %>
<% end %>
</div>
<style>
.appointment-reminder { padding: 8px 12px; }
.appointment-reminder em { color: #b71c1c; }
.appointment-reminder-sent { border-left: 4px solid #5aa05a; }
.appointment-reminder-scheduled { border-left: 4px solid #628db6; }
.appointment-reminder-failed, .appointment-reminder-given_up, .appointment-reminder-no_email { border-left: 4px solid #e8a33d; }
form.appointment-reminder-send { display: inline; margin-left: 8px; }
</style>
<% end %>
@@ -0,0 +1,19 @@
<%# Turning appointment reminders on or off for a customer, on the customer page (redmine_qbo's show_customer_view_right hook) %>
<% customer = local_assigns[:customer] %>
<% if customer && User.current.allowed_to?(:view_customer_appointments, nil, global: true) %>
<% off = ReminderOptOut.opted_out?(customer) %>
<div class="customer-reminders">
<i class="fa-regular fa-envelope"></i>
<strong><%= l(:label_appointment_reminders) %>:</strong>
<%= off ? l(:text_customer_reminders_off) : l(:text_customer_reminders_on) %>
<% if User.current.allowed_to?(:send_appointment_reminders, nil, global: true) %>
<%= button_to (off ? l(:button_reminders_on) : l(:button_reminders_off)),
(off ? customer_reminders_on_path(customer) : customer_reminders_off_path(customer)),
method: :post, form_class: 'customer-reminders-toggle' %>
<% end %>
</div>
<style>
.customer-reminders { margin: 10px 0; }
form.customer-reminders-toggle { display: inline; margin-left: 8px; }
</style>
<% end %>
@@ -0,0 +1,200 @@
<%# Settings for redmine_qbo_reminders (Administration » Plugins » Configure) %>
<% s = RedmineQboReminders::Settings.new(settings) %>
<% field = ->(key) { "settings[#{key}]" } %>
<fieldset class="box tabular">
<legend><%= l(:label_reminders_sending) %></legend>
<p>
<label><%= l(:label_reminders_enabled) %></label>
<%= hidden_field_tag field.call('enabled'), '0', id: nil %>
<%= check_box_tag field.call('enabled'), '1', s.enabled?, id: 'reminders_enabled' %>
<em class="info"><%= l(:text_reminders_enabled) %></em>
</p>
<p>
<label for="reminders_from_name"><%= l(:label_reminders_from_name) %></label>
<%= text_field_tag field.call('from_name'), s['from_name'], id: 'reminders_from_name', size: 40, placeholder: s['shop_name'] %>
</p>
<p>
<label for="reminders_from_address"><%= l(:label_reminders_from_address) %></label>
<%= email_field_tag field.call('from_address'), s['from_address'], id: 'reminders_from_address', size: 40,
placeholder: Setting.mail_from %>
<em class="info"><%= l(:text_reminders_from_address) %></em>
</p>
<p>
<label for="reminders_reply_to"><%= l(:label_reminders_reply_to) %></label>
<%= email_field_tag field.call('reply_to'), s['reply_to'], id: 'reminders_reply_to', size: 40 %>
<em class="info"><%= l(:text_reminders_reply_to) %></em>
</p>
<p>
<label><%= l(:label_reminders_delivery) %></label>
<label class="inline"><%= radio_button_tag field.call('delivery'), 'smtp', s['delivery'] == 'smtp', id: 'reminders_delivery_smtp' %> <%= l(:label_reminders_delivery_smtp) %></label><br />
<label class="inline"><%= radio_button_tag field.call('delivery'), 'redmine', s['delivery'] == 'redmine', id: 'reminders_delivery_redmine' %> <%= l(:label_reminders_delivery_redmine) %></label>
</p>
<div id="reminders-smtp">
<p>
<label for="reminders_smtp_address"><%= l(:label_reminders_smtp_address) %></label>
<%= text_field_tag field.call('smtp_address'), s['smtp_address'], id: 'reminders_smtp_address', size: 30 %>
<%= l(:label_reminders_smtp_port) %>
<%= number_field_tag field.call('smtp_port'), s['smtp_port'], id: 'reminders_smtp_port', min: 1, max: 65535, style: 'width: 6em' %>
<em class="info"><%= l(:text_reminders_smtp_address) %></em>
</p>
<p>
<label for="reminders_smtp_domain"><%= l(:label_reminders_smtp_domain) %></label>
<%= text_field_tag field.call('smtp_domain'), s['smtp_domain'], id: 'reminders_smtp_domain', size: 30, placeholder: s.mail_domain %>
<em class="info"><%= l(:text_reminders_smtp_domain) %></em>
</p>
<p>
<label for="reminders_smtp_user_name"><%= l(:label_reminders_smtp_user_name) %></label>
<%= text_field_tag field.call('smtp_user_name'), s['smtp_user_name'], id: 'reminders_smtp_user_name', size: 30, autocomplete: 'off' %>
</p>
<p>
<label for="reminders_smtp_password"><%= l(:label_reminders_smtp_password) %></label>
<%= password_field_tag field.call('smtp_password'), s['smtp_password'], id: 'reminders_smtp_password', size: 30, autocomplete: 'new-password' %>
<em class="info"><%= l(:text_reminders_smtp_auth) %></em>
</p>
</div>
</fieldset>
<fieldset class="box tabular">
<legend><%= l(:label_reminders_schedule) %></legend>
<p>
<label for="reminders_days_before"><%= l(:label_reminders_send) %></label>
<%= number_field_tag field.call('days_before'), s['days_before'], id: 'reminders_days_before', min: 0, max: 14, style: 'width: 4em' %>
<%= l(:label_reminders_days_before_at) %>
<%= time_field_tag field.call('send_at'), s['send_at'], id: 'reminders_send_at' %>
<em class="info"><%= l(:text_reminders_send) %></em>
</p>
<p>
<label for="reminders_window_start"><%= l(:label_reminders_window) %></label>
<%= time_field_tag field.call('window_start'), s['window_start'], id: 'reminders_window_start' %>
&ndash;
<%= time_field_tag field.call('window_end'), s['window_end'], id: 'reminders_window_end' %>
<em class="info"><%= l(:text_reminders_window) %></em>
</p>
<p>
<label for="reminders_min_lead_hours"><%= l(:label_reminders_min_lead) %></label>
<%= number_field_tag field.call('min_lead_hours'), s['min_lead_hours'], id: 'reminders_min_lead_hours', min: 0, max: 48, step: 0.5, style: 'width: 5em' %>
<%= l(:label_reminders_hours) %>
</p>
<p>
<label for="reminders_time_zone"><%= l(:label_reminders_time_zone) %></label>
<%= select_tag field.call('time_zone'), options_for_select(ActiveSupport::TimeZone.all.map { |z| [z.to_s, z.name] }, s.zone.name),
id: 'reminders_time_zone' %>
<em class="info"><%= l(:text_reminders_time_zone) %></em>
</p>
<p>
<label><%= l(:label_reminders_statuses) %></label>
<%= hidden_field_tag "#{field.call('statuses')}[]", '', id: nil %>
<% CustomerAppointment::STATUSES.each do |status| %>
<label class="inline" style="margin-right: 10px">
<%= check_box_tag "#{field.call('statuses')}[]", status, s.statuses.include?(status), id: "reminders_status_#{status.parameterize}" %>
<%= status.humanize %>
</label>
<% end %>
</p>
<p>
<label><%= l(:label_reminders_check_on_page_loads) %></label>
<%= hidden_field_tag field.call('check_on_page_loads'), '0', id: nil %>
<%= check_box_tag field.call('check_on_page_loads'), '1', s.check_on_page_loads?, id: 'reminders_check_on_page_loads' %>
<em class="info"><%= l(:text_reminders_check_on_page_loads) %></em>
</p>
</fieldset>
<fieldset class="box tabular">
<legend><%= l(:label_reminders_message) %></legend>
<p>
<label for="reminders_shop_name"><%= l(:label_reminders_shop_name) %></label>
<%= text_field_tag field.call('shop_name'), s['shop_name'], id: 'reminders_shop_name', size: 40 %>
</p>
<p>
<label for="reminders_shop_phone"><%= l(:label_reminders_shop_phone) %></label>
<%= text_field_tag field.call('shop_phone'), s['shop_phone'], id: 'reminders_shop_phone', size: 20 %>
</p>
<p>
<label for="reminders_shop_address"><%= l(:label_reminders_shop_address) %></label>
<%= text_field_tag field.call('shop_address'), s['shop_address'], id: 'reminders_shop_address', size: 60 %>
</p>
<p>
<label for="reminders_subject"><%= l(:field_subject) %></label>
<%= text_field_tag field.call('subject'), s['subject'], id: 'reminders_subject', size: 70 %>
</p>
<p>
<label for="reminders_body"><%= l(:label_reminders_body) %></label>
<%= text_area_tag field.call('body'), s['body'], id: 'reminders_body', rows: 14, cols: 70, class: 'reminders-body' %>
<em class="info">
<%= l(:text_reminders_placeholders) %>
<% RedmineQboReminders::Template::PLACEHOLDERS.each do |name| %><code>{{<%= name %>}}</code> <% end %><br />
<%= l(:text_reminders_sections) %>
</em>
</p>
<p>
<label><%= l(:label_reminders_attach_calendar) %></label>
<%= hidden_field_tag field.call('attach_calendar'), '0', id: nil %>
<%= check_box_tag field.call('attach_calendar'), '1', s.attach_calendar?, id: 'reminders_attach_calendar' %>
<em class="info"><%= l(:text_reminders_attach_calendar) %></em>
</p>
<p>
<label for="reminders_test_email"><%= l(:label_reminders_test) %></label>
<%= email_field_tag 'email', User.current.mail, id: 'reminders_test_email', size: 30 %>
<%= button_tag l(:button_reminders_send_test), type: 'submit', formaction: test_appointment_reminder_path,
formmethod: 'post', name: nil, id: 'reminders-send-test' %>
<em class="info"><%= l(:text_reminders_test) %></em>
</p>
</fieldset>
<fieldset class="box">
<legend><%= l(:label_reminders_recent) %></legend>
<% recent = AppointmentReminder.recent.includes(customer_appointment: :customer).limit(15) %>
<% if recent.empty? %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% else %>
<table class="list reminders-recent">
<thead><tr>
<th><%= l(:label_appointment_reminder_appointment) %></th>
<th><%= l(:label_reminders_customer) %></th>
<th><%= l(:field_mail) %></th>
<th><%= l(:field_status) %></th>
<th><%= l(:label_reminders_sent_at) %></th>
</tr></thead>
<tbody>
<% recent.each do |r| %>
<% appointment = r.customer_appointment %>
<tr>
<td class="appointment">
<% if appointment %><%= link_to "#{format_time(r.appointment_date)} – #{appointment.subject}", customer_appointment_path(appointment) %><% end %>
</td>
<td><%= appointment&.customer&.name %></td>
<td><%= r.email %></td>
<td class="status reminder-<%= r.status %>">
<%= l("label_reminder_status_#{r.status}") %>
<% if r.error.present? %><br /><em><%= r.error.truncate(140) %></em><% end %>
</td>
<td><%= format_time(r.sent_at) if r.sent_at %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<p>
<%= button_tag l(:button_reminders_run), type: 'submit', formaction: run_appointment_reminders_path, formmethod: 'post',
name: nil, id: 'reminders-run' %>
<em class="info"><%= l(:text_reminders_run) %></em>
</p>
</fieldset>
<style>
textarea.reminders-body { font-family: monospace; width: 95%; }
table.reminders-recent td { text-align: left; }
table.reminders-recent td.status em { color: #b71c1c; font-size: 0.9em; }
table.reminders-recent .reminder-sent { color: #108000; }
table.reminders-recent .reminder-failed { color: #b71c1c; }
</style>
<%= javascript_tag do %>
$(function() {
// The SMTP server settings only matter when sending through it
var toggle = function() { $('#reminders-smtp').toggle($('#reminders_delivery_smtp').is(':checked')); };
$('input[name="settings[delivery]"]').on('change', toggle);
toggle();
});
<% end %>