-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyptemail.pl
253 lines (192 loc) · 5.88 KB
/
yptemail.pl
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env perl
# send emails, slowly, via blat
use Getopt::Std;
getopts('snD');
Usage() unless ($#ARGV == 1);
print '*' x 50 . "\nStarting " . scalar(localtime()) . "\n";
$toemail = $ARGV[0];
$skip = $ARGV[1];
# these fields are required
@emailRequired = (
'Member_ID',
'First_Name',
'Last_Name',
'sn_email',
'ypt_email',
'sb_email',
'Y01_Expires'
);
print scalar(localtime) . " loading email list from $toemail\n";
open(INPUT, '<', $toemail) or die $!;
# Member_ID First_Name Last_Name sn_email ypt_email sb_email Y01_Expires continue
# 0 1 2 3 4 5 6 7
while (<INPUT>)
{
chomp; s/\r|\n//g;
next if (/^\s*$/); # skip blank lines
@f = split(/\t/);
if ($. == 1)
{
# map field names to offsets
$i = 0;
for $f (@f)
{
$emailFields{$f} = $i++;
}
die "Improper email file format $_\nRequired: " . join(', ', @emailRequired) . "\n" unless (Contains(\@f, \@emailRequired));
next;
}
if ($f[$emailFields{'Member_ID'}] !~ /\d+/)
{
print "Invalid email entry $_\n";
$errors{"Invalid email entry"}++;
next;
}
if (exists $emails{$f[$emailFields{'Member_ID'}]})
{
print "Duplicate email entry for $_\n\tprev: $emails{$f[$emailFields{'Member_ID'}]}\n";
$errors{"Duplicate email entry"}++;
}
$emails{$f[$emailFields{'Member_ID'}]} = $_;
}
close(INPUT);
# these fields are required
@skipRequired = (
'BSAID',
);
print scalar(localtime) . " loading email list from $skip\n";
open(INPUT, '<', $skip) or die $!;
# BSAID Notes
# 0 1
while (<INPUT>)
{
chomp;
next if (/^\s*$/); # skip blank lines
@f = split(/\t/);
if ($. == 1)
{
# map field names to offsets
$i = 0;
for $f (@f)
{
$skipFields{$f} = $i++;
}
die "Improper skip file format $_\nRequired: " . join(', ', @skipRequired) . "\n" unless (Contains(\@f, \@skipRequired));
next;
}
if ($f[$skipFields{'BSAID'}] !~ /\d+/)
{
print "Invalid skip entry $_\n";
$errors{"Invalid skip entry"}++;
next;
}
if (exists $skips{$f[$skipFields{'BSAID'}]})
{
print "Duplicate skip entry for $_\n\tprev: $skips{$f[$skipFields{'BSAID'}]}\n";
$errors{"Duplicate skip entry"}++;
}
$skips{$f[$skipFields{'BSAID'}]} = $_;
}
close(INPUT);
if ($opt_s)
{
for (sort keys %emails)
{
$bsaid = $_;
next if (exists($skips{$bsaid}));
print "$emails{$bsaid}\n";
$outputCount++;
}
}
else
{
$emailTemplate = do { local $/; <DATA> };
$loopCount = scalar(values %emails);
for (values %emails)
{
@e = split(/\t/);
$bsaid = $e[$emailFields{'Member_ID'}];
$fname = $e[$emailFields{'First_Name'}];
$lname = $e[$emailFields{'Last_Name'}];
$ypt = $e[$emailFields{'Y01_Expires'}];
%emailList = ();
foreach (qw(sn_email ypt_email sb_email))
{
$emailList{lc($e[$emailFields{$_}])} = 1 if ($e[$emailFields{$_}] =~ /.+\@.+\..+/);
}
$email = join(',', keys(%emailList));
if (exists $skips{$bsaid})
{
print scalar(localtime) . " SKIP $bsaid, $fname $lname, $email\n";
$errors{"Skip entry"}++;
next;
}
print scalar(localtime) . " processing $bsaid, $fname $lname, $email\n";
# substitute values in the email body
$emailBody = $emailTemplate;
# name, ypt, link
$emailBody =~ s/\[name\]/$fname/ig;
$emailBody =~ s/\[ypt\]/$ypt/ig;
open(OUTPUT, '>body.txt') or die $!;
print OUTPUT $emailBody;
close(OUTPUT);
$from = '[email protected]';
$subject = "BSA YPT expired for merit badge counselor";
if ($opt_n)
{
print "COMMAND: blat body.txt -to $email -subject \"$subject\" -replyto $from -from $from -sender $from\n";
print "body.txt:\n";
$emailBody =~ s/\n/\n\t/g;
print "\t$emailBody\n";
}
else
{
print `blat body.txt -to $email -subject "$subject" -replyto $from -from $from -sender $from`;
}
unlink('body.txt');
$outputCount++;
sleep(10) if (--$loopCount && !$opt_n); # seconds
}
}
print '=' x 20 . "\nTotal records: $outputCount\nError counts\n";
for (sort keys %errors)
{
print "\t$_: $errors{$_}\n";
}
print "Finished " . scalar(localtime()) . "\n\n";
# usage Contains (\@haystack, \@needle)
# think of it as @haystack contains @needle
sub Contains
{
my ($haystack, $needle) = @_;
my %haystackHash = map {$_ => 1} @{$haystack};
for (@{$needle})
{
return 0 if (!exists($haystackHash{$_}));
}
return 1;
}
sub Usage
{
die <<EOS;
Usage: $0 [-nD] email.tsv skip.tsv
email.tsv = tab seperated list of people to send to
skip.tsv = tab separated list of those to be skipped
options:
-s : just do the skip list processing and output the remaining records
-n : don't actually send, just show what would be done
-D : turn on debug messages
EOS
}
__DATA__
[name],
Thank you for renewing your merit badge counselor registration. One of the requirements for a merit badge counselor is to maintain current BSA Youth Protection Training (YPT).
According to our records, your YPT will expire before the end of June of this year. In order to renew as a merit badge counselor, you will need to complete YPT before the end of this month.
Your YPT expires [ypt].
Visit https://www.utahscouts.org/ypt to take the training.
If you have recently completed YPT, or you believe your YPT date is in error, please email a copy of your YPT completion certificate to Annette Sholly at [email protected]
Sincerely,
James Brown
Merit Badge Counselor Coordinator
Council Advancement Committee
Crossroads of the West Council