-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEmailer.js
218 lines (200 loc) · 6.66 KB
/
Emailer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
function sendEmail (email, subject, htmlBody) {
Logger.log('Result of sendmail=>%s, %s, %s',email,subject,htmlBody);
Logger.log(shortStringify(
MailApp.sendEmail(
{to:email,
htmlBody:htmlBody,
subject:subject}
)
)
)
}
function checkForSelfApproval (settings) {
// We are checking whether the form allows self approval or what...
if (! settings.allowSelfApproval || settings.allowSelfApproval=='0') {
if (settings.FormUser==settings.Approver) {
Logger.log('Uh oh self approval -- better fix it');
if (settings.ApproverDefault != settings.FormUser) {
settings.Approver = settings.ApproverDefault
}
else {
// fallback
settings.Approver = settings.ApproverBackup
}
}
}
}
// function checkSelfApproval (val, addressSettings, results) {
// if (! addressSettings['PreventSelfApprove']) {return val}
// // Otherwise we *are* requiring that value != self
// if (val != results['FormUser']) {
// return val
// }
// if (results['EmailLookup']['Default']!=results['FormUser']) {
// return results['EmailLookup']['Default']
// }
// if (results['DefaultBackup']!=results['FormUser']) {
// return results['DefaultBackup']
// }
// }
function getEmail (addressSettings, results) {
// Given somewhat complex addressSettings config sheet, get correct
// email for results...
// Email Field => Field that maps directly to email
// ORRRRRR
// Lookup Field => Field that tells us what to look up in the other fields
if (addressSettings['Lookup Field']) {
// If we have a lookup field...
var fieldValue = results[addressSettings['Lookup Field']]
var value = addressSettings[fieldValue]
if (value) {
return checkSelfApproval(value,addressSettings,results)
}
else {
if (addressSettings['Default']) {
return checkSelfApproval(addressSettings['Default'],addressSettings,results)
}
}
}
if (addressSettings['Email Field']) {
return checkSelfApproval(results[addressSettings['Email Field']],addressSettings,results)
}
}
function checkBool (value) {
switch (value) {
case false:
return false;
break;
case 'no':
return false;
break;
case 'No':
return false;
break;
case 'false':
return false;
break
case 'False':
return false;
break
case 0:
return false;
break
case 'untrue':
return false;
break;
default:
return value;
break
}
}
function sendFormResultEmail (results, settings) {
Logger.log('sendFormResultEmail'+shortStringify([results,settings]));
var config = lookupFields(settings, results)
// Add support for conditional email...
if (config.hasOwnProperty('onlyEmailIf')) {
Logger.log('Checking onlyEmailIf field!');
if (! checkBool(config.onlyEmailIf)) {
logNormal('Not sending email: \nresults:%s \nsettings:%s config: %s',results,settings,config);
return 'No Email Sent';
}
}
//
templateFields = getTemplateFields(config, results);
logNormal('config=>%s',config);
if (config.emailFormUser) {
config.To = results.FormUser+','+config.To
}
sendEmailFromTemplate(
//getEmail(config, results),
config.To,
config.Subject,
config.Body,
templateFields,
true
);
return {config:config,fields:templateFields}
}
function sendEmailFromTemplate (email, subj, template, fields, fixWhiteSpace) {
debug = 0;
if (debug) {
msg = '<pre>';
msg += 'Email being sent: here is what we got.\n';
msg += 'Template: '+template+'\n\n';
msg += 'Fields: '+shortStringify(fields)+'\n\n';
msg += 'Result: \nSubject: '+applyTemplate(subj,fields);
msg += 'Result: \nBody: '+applyTemplate(template,fields,fixWhiteSpace);
msg += '</pre>';
emailError(msg, 'No real error :)', {'subject':'Email Debug Info: '+applyTemplate(subj,fields)});
}
sendEmail(email, applyTemplate(subj, fields), applyTemplate(template, fields, fixWhiteSpace));
}
function getTemplateFields (config, results) {
var templateFields = {}
for (var setting in results) {
templateFields[setting] = spreadsheetify(results[setting]);
} // end forEach result
for (var setting in config) {
if (templateFields[setting]) {
logAlways('sendFormResultsEmail - Potential conflict between results[%s]=>%s and config[%s]=>%s; using results',
setting, templateFields[setting], setting, config[setting]
)
}
else {
templateFields[setting] = spreadsheetify(config[setting]);
}
} // end for each setting
return templateFields
}
function applyTemplate (template, fields, fixWhiteSpace) {
if (fixWhiteSpace) {
template = template.replace(/\n/g,'<br>');
}
for (var target in fields) {
var replacement = spreadsheetify(fields[target]);
if (fixWhiteSpace) {
if (typeof replacement == 'string') {
replacement = replacement.replace(/\n/g,'<br>')
}
}
template = template.replace(new RegExp(escapeRegExp('<<'+target+'>>'),'g'),replacement);
template = template.replace(new RegExp(escapeRegExp('{{'+target+'}}'),'g'),replacement);
}
template = template.replace(/{{[^}]*}}/g,''); // remove empty ones
template = template.replace(/<<[^>]*>>/g,''); // remove empty ones
return template
}
function testTemplateEmail () {
template = '<p>This is a <<adj>> paragraph. <<empty>> One day, Mr. <<name>> went on a walk and came upon a <<animal>>.</p><p>When Mr. <<name>> saw the <<animal>>, he <<verb1>>ed!</p><p>This ends my mad lib story. I hope you <<verb2>>ed it.</p>'
fields = {
animal: 'bear',
name: 'Johnson',
verb1: 'regulate',
verb2: 'animate',
adj: 'beautiful'
}
sendEmailFromTemplate('[email protected]','Test Template Message',template, fields);
}
function testEmail () {
sendEmail('[email protected]','This is a test message from Workflows','<b>This is bold</b><br><table><tr><td>This</td><td>is</td></tr><tr><td><i>a</i></td><td>table</td></tr></table><h3>Heading!</h3>');
}
var templateTest
function _initZZZTestEmailer () {
templateTest = Test ( {
metadata : {name:'Template test'},
test : function () {
assertEq(applyTemplate('foo <<bar>>',{bar:'yippee'}),'foo yippee')
assertEq(applyTemplate('foo <<empty>><<bar>>',{bar:'yippee'}),'foo yippee')
assertEq(applyTemplate('foo \n<<bar>>',{bar:'yippee'}),'foo \nyippee')
assertEq(applyTemplate('foo \n{{bar}}',{bar:'yippee'},true),'foo <br>yippee')
assertEq(applyTemplate('foo \n<<bar>>\n<<bar>>\n',{bar:'yippee'},true),'foo <br>yippee<br>yippee<br>')
assertEq(applyTemplate('<<first>>:<<middle>>:<<last>>',{'first':'Thomas','middle':'M','last':'Hinkle'}),
'Thomas:M:Hinkle')
return 'Success!'
}
})
}
function testTemplate () {templateTest.solo()}