Skip to content

Commit

Permalink
send receipt to user
Browse files Browse the repository at this point in the history
  • Loading branch information
hjelmevold committed Dec 10, 2018
1 parent 79a785d commit 2f194ee
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
33 changes: 30 additions & 3 deletions src/main/resources/site/content-types/form/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@
</items>
</field-set>
<field-set name="email-settings">
<label>E-mail with form data</label>
<label>Send data by e-mail</label>
<items>
<input type="TextLine" name="emailSubscribers">
<label>Send to e-mail address</label>
<label>To</label>
<occurrences minimum="0" maximum="0"/>
<help-text>Optional. E-mail addresses specified in these fields will receive all the raw form response data in an e-mail, and with uploaded files as attachments.</help-text>
<config>
Expand All @@ -183,7 +183,7 @@
<help-text>If one of the inputs for the form specified in the "Input fields" list above is of type "E-mail", that input may be used as the FROM address in the e-mail that is sent with the form data. That way, the e-mail subscribers may reply to the user e-mail directly. Overrides the default "E-mail sent FROM" specified further below.</help-text>
</input-->
<input type="TextLine" name="emailSubject">
<label>E-mail subject</label>
<label>Subject</label>
<occurrences minimum="0" maximum="1"/>
<help-text>Is the form heading by default, but a custom e-mail subject may be specified here. If neither are filled out, the display name is used.</help-text>
</input>
Expand Down Expand Up @@ -230,6 +230,33 @@
<label>Receipt message</label>
<occurrences minimum="0" maximum="1"/>
</input-->
<option-set name="receipt">
<label>E-mail receipt back to user</label>
<expanded>false</expanded>
<occurrences minimum="1" maximum="1"/>
<help-text>For this to work, one of the Input Fields created above MUST be of type "e-mail" and be "required".</help-text>
<options minimum="1" maximum="1">
<option name="disabled">
<label>No, do not send receipt</label>
<default>true</default>
</option>
<option name="enabled">
<label>Yes, send receipt</label>
<help-text>For this to work, one of the Input Fields created above MUST be of type "e-mail" and be "required".</help-text>
<items>
<input name="message" type="HtmlArea">
<label>Receipt message</label>
<occurrences minimum="1" maximum="1"/>
<help-text>For this to work, one of the Input Fields created above MUST be of type "e-mail" and be "required".</help-text>
</input>
<input name="includeFormData" type="checkbox">
<label>Include summary of submitted form data</label>
<occurrences minimum="0" maximum="1"/>
</input>
</items>
</option>
</options>
</option-set>
</items>
</field-set>
<field-set name="advanced">
Expand Down
39 changes: 19 additions & 20 deletions src/main/resources/site/lib/form-builder/form-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,8 @@ var saveFile = function(file, folder, siteConfig) {
};

var sendEmailToRecipients = function(formData, siteConfig, formConfig, request, emailAttachments) {
var systemEmailFrom = formConfig.emailFrom || siteConfig.emailFrom;
var userEmailFrom = null;
var userReceiptEmailTo = null;
var systemEmailAddr = formConfig.emailFrom || siteConfig.emailFrom;
var userEmailAddr = null;

// Arrays that will later be populated with values and concatenated in the e-mail body
var labels = [];
Expand All @@ -275,18 +274,15 @@ var sendEmailToRecipients = function(formData, siteConfig, formConfig, request,
var checkbox = CheckboxInputMapper.map(inputConfig);
value = (formData[checkbox.name]) ? 'Yes' : 'No';
}
// Sets the e-mail address for the TO field
// Currently DISABLED (needs boolean sendReceipt in config)
if (inputConfig.input && inputConfig.input._selected && inputConfig.input._selected === 'email') {
userReceiptEmailTo = value.trim();
}
labels.push(label);
values.push(value);

// Set e-mail sender address to be the one given in the form data, if provided
if (formConfig.emailFromInput && _id === formConfig.emailFromInput && value) {
// TODO: verify logic!!! should perhaps have been userEmailFrom? Or was that specifically omitted due to variable scope?
formConfig.emailFrom = value;
// Set user e-mail address to be the value from the first e-mail input field in the form data
if (inputConfig.input && inputConfig.input._selected && inputConfig.input._selected === 'email' && value && value.trim()) {
// Only set once, for the first input of type 'email' with a supplied value. Disregard any following email input values
if (!userEmailAddr) {
userEmailAddr = value.trim();
}
}
});

Expand Down Expand Up @@ -323,15 +319,18 @@ var sendEmailToRecipients = function(formData, siteConfig, formConfig, request,
var subject = (formConfig.emailSubject ? formConfig.emailSubject : formData._formContentDisplayName) + (subjectFromInput.length > 0 ? ' # ' + subjectFromInput : '');

// Send e-mail receipt to user
// Currently DISABLED due to missing boolean in form config
if (isSet(formConfig.sendReceipt) && formConfig.sendReceipt && userEmailTo.length > 0) {
if (isSet(formConfig.receipt) && formConfig.receipt && formConfig.receipt._selected === 'enabled' && userEmailAddr && userEmailAddr.length > 0) {
var emailBody = portal.processHtml({ value: formConfig.receipt.enabled.message });
// Append form data to message HTML
if (formConfig.receipt.enabled.includeFormData) {
emailBody += '<code>' + formDataBeautified + '</code>';
}

mail.send({
from: systemEmailFrom || 'noreply@example.com', // TODO: get from site config, or form
to: userReceiptEmailTo,
from: systemEmailAddr || 'noreply@example.com',
to: userEmailAddr,
subject: subject,
body: (isSet(formConfig.includeFormData) && formConfig.includeFormData)
? '<code>' + portal.processHtml({ value: formConfig.receiptMessage }) + '<br/>' + formDataBeautified + '</code>'
: '<code>' + portal.processHtml({ value: formConfig.receiptMessage }) + '</code>',
body: emailBody,
attachments: [], // a waste of time and resources to e-mail the uploaded attachments back to the user
contentType: 'text/html; charset="UTF-8"'
});
Expand All @@ -340,7 +339,7 @@ var sendEmailToRecipients = function(formData, siteConfig, formConfig, request,
// Send e-mail to subscribers
if (formConfig.emailSubscribers) {
return mail.send({
from: userEmailFrom || systemEmailFrom || 'noreply@example.com',
from: userEmailAddr || systemEmailAddr || 'noreply@example.com',
to: util.forceArray(formConfig.emailSubscribers),
subject: subject,
body: '<code>' + formDataBeautified + '</code>',
Expand Down

0 comments on commit 2f194ee

Please sign in to comment.