add ability to customize message before sending
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SendSubmitterInvitationEmailJob < ApplicationJob
|
||||
def perform(submitter)
|
||||
SubmitterMailer.invitation_email(submitter).deliver_now!
|
||||
def perform(params = {})
|
||||
submitter = Submitter.find(params['submitter_id'])
|
||||
|
||||
SubmitterMailer.invitation_email(submitter, subject: params['subject'], body: params['body']).deliver_now!
|
||||
|
||||
SubmissionEvent.create!(submitter:, event_type: 'send_email')
|
||||
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class SubmitterMailer < ApplicationMailer
|
||||
DEFAULT_MESSAGE = %(You have been invited to submit the "%<name>s" form:)
|
||||
|
||||
def invitation_email(submitter, message: '')
|
||||
def invitation_email(submitter, body: nil, subject: nil)
|
||||
@current_account = submitter.submission.template.account
|
||||
@submitter = submitter
|
||||
@message = message.presence || format(DEFAULT_MESSAGE, name: submitter.submission.template.name)
|
||||
@body = body.presence
|
||||
|
||||
@email_config = @current_account.account_configs.find_by(key: AccountConfig::SUBMITTER_INVITATION_EMAIL_KEY)
|
||||
|
||||
subject =
|
||||
if @email_config
|
||||
ReplaceEmailVariables.call(@email_config.value['subject'], submitter:)
|
||||
if @email_config || subject.present?
|
||||
ReplaceEmailVariables.call(subject.presence || @email_config.value['subject'], submitter:)
|
||||
else
|
||||
'You have been invited to submit a form'
|
||||
end
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
<div class="form-control">
|
||||
<% is_smtp_configured = Accounts.can_send_emails?(current_account) %>
|
||||
<div class="flex justify-between">
|
||||
<div class="flex justify-between items-center">
|
||||
<%= f.label :send_email, for: uuid = SecureRandom.uuid, class: 'flex items-center cursor-pointer' do %>
|
||||
<%= f.check_box :send_email, id: uuid, class: 'base-checkbox', disabled: !is_smtp_configured, checked: is_smtp_configured %>
|
||||
<span class="label">Send emails</span>
|
||||
<% end %>
|
||||
<%= render 'email_stats' %>
|
||||
<div>
|
||||
<label>
|
||||
<%= f.check_box :is_custom_message, onchange: "[this.form.querySelector('#message_field').classList.toggle('hidden', !event.currentTarget.checked)]", checked: false, class: 'hidden peer' %>
|
||||
<span class="link peer-checked:hidden">Edit message</span>
|
||||
</label>
|
||||
<%= render 'email_stats' %>
|
||||
</div>
|
||||
</div>
|
||||
<% unless is_smtp_configured %>
|
||||
<div class="alert my-4">
|
||||
@@ -21,3 +27,20 @@
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<% config = AccountConfigs.find_or_initialize_for_key(current_account, AccountConfig::SUBMITTER_INVITATION_EMAIL_KEY) %>
|
||||
<div id="message_field" class="card card-compact bg-base-200 hidden">
|
||||
<div class="card-body">
|
||||
<div class="form-control space-y-2">
|
||||
<div class="form-control">
|
||||
<%= f.label :subject, class: 'label' %>
|
||||
<%= f.text_field :subject, value: config.value['subject'], required: true, class: '!text-sm base-input w-full' %>
|
||||
</div>
|
||||
<div class="form-control">
|
||||
<%= f.label :message, 'Body', class: 'label' %>
|
||||
<autoresize-textarea>
|
||||
<%= f.text_area :body, value: config.value['body'], required: true, class: 'base-textarea w-full', rows: 10 %>
|
||||
</autoresize-textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
<% if @email_config %>
|
||||
<%= auto_link(simple_format(h(ReplaceEmailVariables.call(@email_config.value['body'], submitter: @submitter)))) %>
|
||||
<% if @email_config || @body.present? %>
|
||||
<%= auto_link(simple_format(h(ReplaceEmailVariables.call(@body.presence || @email_config.value['body'], submitter: @submitter)))) %>
|
||||
<% else %>
|
||||
<p>Hi there,</p>
|
||||
<%= simple_format(@message) %>
|
||||
<p>You have been invited to submit the "<%= @submitter.submission.template.name %>" form</p>
|
||||
<p><%= link_to 'Submit Form', submit_form_url(slug: @submitter.slug, t: SubmissionEvents.build_tracking_param(@submitter, 'click_email')) %></p>
|
||||
<p>Please contact us by replying to this email if you didn't request this.</p>
|
||||
<p>
|
||||
|
||||
+9
-1
@@ -35,7 +35,15 @@ module Submitters
|
||||
submitters.each do |submitter|
|
||||
next if submitter.email.blank?
|
||||
|
||||
SendSubmitterInvitationEmailJob.perform_later(submitter)
|
||||
enqueue_invitation_email(submitter, params)
|
||||
end
|
||||
end
|
||||
|
||||
def enqueue_invitation_email(submitter, params)
|
||||
subject, body = params.values_at(:subject, :body) if params[:is_custom_message] == '1'
|
||||
|
||||
SendSubmitterInvitationEmailJob.perform_later('submitter_id' => submitter.id,
|
||||
'body' => body,
|
||||
'subject' => subject)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user