2018-08-23 08:33:18 +00:00
#!/usr/bin/perl
2018-08-23 08:53:02 +00:00
# Debug level 0: Send email
# Debug level 1: Send email to myself
# Debug level 2: Print names and email addresses
2018-08-23 08:33:18 +00:00
use strict ;
use warnings ;
use Text::CSV ;
use Email::MIME::CreateHTML ;
use Email::Sender::Simple qw( sendmail ) ;
use Email::Sender::Transport::SMTP ;
use Email::Sender::Transport::SMTPS ;
use Email::Sender::Transport::SMTP::TLS ;
2018-10-11 12:02:09 +00:00
use Term::ReadKey ;
2018-08-23 08:33:18 +00:00
use File::Basename ;
use Try::Tiny ;
use Data::Dumper ;
2019-10-15 19:19:14 +00:00
my $ map_dir = '/home/tim/Documents/Projects/2019/Gun Run/Maps/' ;
2018-10-11 19:02:47 +00:00
my $ email_csv = "$map_dir/GIS Base Map/marshals_by_position.csv" ;
my $ pdf_dir = "$map_dir/GIS Base Map/Marshals/" ;
2018-10-12 21:37:01 +00:00
my $ debug = 2 ;
2018-08-23 08:33:18 +00:00
my @ emails ;
2018-10-13 09:29:34 +00:00
if ( $ debug == 2 ) {
print "WARNING: Printing names. Not sending emails!\n" ;
} elsif ( $ debug == 1 ) {
print "WARNING: Sending emails to a TEST address.\n" ;
} elsif ( $ debug == 0 ) {
print "Sending emails to...\n" ;
} else {
print "ERROR: Invalid debug value\n" ;
}
2018-10-11 12:02:09 +00:00
my $ password ;
if ( - f "$map_dir/password_store" ) {
open ( my $ fh , '<:encoding(UTF-8)' , "$map_dir/password_store" ) or die "Could not open password store: $!" ;
while ( my $ row = <$fh> ) {
chomp ( $ password = $ row ) ;
}
close ( $ fh )
} else {
print "Please type your email password: " ;
#chomp($password = <STDIN>);
ReadMode ( 'noecho' ) ;
chomp ( $ password = ReadLine ( 0 ) ) ;
print "\n" ;
ReadMode ( 'normal' ) ;
}
2018-08-23 08:33:18 +00:00
my $ csv = Text::CSV - > new ( { binary = > 1 } ) or die "Cannot use CSV: " . Text::CSV - > error_diag ( ) ;
open my $ ifh , "<" , $ email_csv or die "Could not read $email_csv: $!" ;
$ csv - > column_names ( $ csv - > getline ( $ ifh ) ) ;
while ( my $ row = $ csv - > getline ( $ ifh ) ) {
my $ person = {
"position" = > $ row - > [ 0 ] ,
2019-10-15 19:19:14 +00:00
"club" = > $ row - > [ 1 ] ,
"first_name" = > $ row - > [ 2 ] ,
"name" = > $ row - > [ 4 ] ,
2018-08-23 08:53:02 +00:00
"email" = > $ row - > [ 6 ] ,
2018-08-23 08:33:18 +00:00
} ;
$ person - > { "name" } =~ s/ +$//g if $ person - > { "name" } ;
$ person - > { "name" } =~ s/ +/ /g if $ person - > { "name" } ;
2018-08-23 08:53:02 +00:00
$ person - > { "first_name" } =~ s/ +$//g if $ person - > { "first_name" } ;
$ person - > { "first_name" } =~ s/ +/ /g if $ person - > { "first_name" } ;
2018-08-23 08:33:18 +00:00
$ person - > { "email" } =~ s/,//g if $ person - > { "email" } ;
push @ emails , $ person ;
}
$ csv - > eof or $ csv - > error_diag ( ) ;
close $ ifh ;
#@emails = sort { $a->{"position"} cmp $b->{"position"} } @emails;
if ( $ ARGV [ 0 ] ) {
my $ pos = $ ARGV [ 0 ] ;
@ emails = grep { $ _ - > { "position" } =~ /^$pos$/ } @ emails ;
}
2018-08-23 08:53:02 +00:00
#print Dumper(@emails);
2018-08-23 08:33:18 +00:00
foreach my $ person ( @ emails ) {
next unless $$ person { "name" } and $$ person { "email" } ;
2019-10-15 19:19:14 +00:00
next unless $$ person { "club" } =~ /^(AAC)$/ ;
2018-10-11 12:02:09 +00:00
next if $$ person { "position" } =~ /\D/ ;
2018-08-23 08:53:02 +00:00
my $ salutation = $$ person { "first_name" } ;
my $ name = $$ person { "name" } ;
2018-08-23 08:33:18 +00:00
my $ email = $$ person { "email" } ;
2018-10-11 08:28:05 +00:00
my $ club = $$ person { "club" } ;
2018-08-23 08:33:18 +00:00
next unless $ email =~ /@/ ;
my $ position = $$ person { "position" } ;
2018-10-11 12:02:09 +00:00
my $ position_time ;
2019-10-15 19:19:14 +00:00
if ( $ position =~ /^(10\d|110)$/ ) {
2018-10-11 12:02:09 +00:00
$ position_time = "4:45AM" ;
} else {
2019-10-25 07:40:29 +00:00
$ position_time = "5:45AM" ;
2018-10-11 12:02:09 +00:00
}
2018-08-23 08:33:18 +00:00
2018-10-11 08:28:05 +00:00
# if ($position =~ /^(26|27)$/i) {
# warn "NOTE: Skipped position $position";
# next;
# }
2018-08-23 08:33:18 +00:00
foreach my $ m ( split ( /[, ]+/ , $ email ) ) {
2018-08-23 08:53:02 +00:00
$ m = "$name <$m>" ;
2018-10-11 08:28:05 +00:00
printf "%-4s %-15s %15s\n" , $$ person { "position" } , $ salutation , $ m if ( $ debug >= 0 ) ;
2018-08-23 08:53:02 +00:00
$ m = 'Timothy Allen <tim@allen.org.za>' if ( $ debug == 1 ) ;
2018-10-11 08:28:05 +00:00
my $ pdf = $ pdf_dir . $ club . '_marshal_map_' . $ position . '.pdf' ;
my $ jpg = $ pdf_dir . $ club . '_marshal_map_' . $ position . '.jpg' ;
2019-10-25 07:40:29 +00:00
my $ parking = $ map_dir . '/Gun Run 2019 - P9 Marshal Parking.pdf' ;
my $ directions = $ map_dir . '/Gun Run 2019 - P9 Parking Access.pdf' ;
2018-08-23 08:53:02 +00:00
my $ pdf_filename = basename ( $ pdf ) ;
my $ jpg_filename = basename ( $ jpg ) ;
die "No PDF at $pdf_filename" unless - e $ pdf ;
2018-10-11 08:28:05 +00:00
die "No JPG at $jpg_filename" unless - e $ jpg ;
2018-08-23 08:53:02 +00:00
2018-08-23 08:33:18 +00:00
my $ html = qq(
<html> <head>
</head>
<body>
<p>
Dear $ salutation
</p>
<p>
2019-10-25 07:40:29 +00:00
I believe that many marshals didn 't receive their maps, so I' m re - sending to everyone . If you received your map before , please accept my apologies , along with the attached voucher for parking in P9 ! I 've also attached a map that shows you how to get to P9: turn off Helen Suzman by the Virgin Active, turn right at the T-junction, and pass through the gate at the end of the road, onto the field in Green Point Park. Please note that you will need to <b>print</b> the map (security won' t accept an image on your phone ) .
2018-08-23 08:33:18 +00:00
</p>
<p>
2019-10-25 07:40:29 +00:00
Also attached to this email is a map that gives your marshalling position . <b> You are in position $ position on the map . </b> It ' s important to be in place by <b> $ position_time </b> on the day , as the police and traffic officials will be conducting spot checks at that point , and if any marshal is not in position , they may call off the race or delay it .
2018-08-23 08:33:18 +00:00
</p>
<p>
2019-10-15 19:19:14 +00:00
If you have any trouble opening the attachment , the map is available online as a < a href = "http://node.org.za/GunRun2019/$club/$pdf_filename" > PDF document </a> and an < a href = "http://node.org.za/GunRun2019/$club/$jpg_filename" > image </a> .
2018-08-23 08:33:18 +00:00
</p>
<p>
2019-10-15 19:19:14 +00:00
In addition , there is a complete < a href = "https://node.org.za/GunRun2019/Gun%20Run%202019%20-%20Routes.pdf" > route map </a> and a map of < a href = "https://node.org.za/GunRun2019/Gun%20Run%202019%20-%20Marshal%20Plan.pdf" > all marshal positions </a> . All < a href = "http://node.org.za/GunRun2019/" > other maps </a> ( including cones and fencing ; road closures ; and directions to the start ) are available as well .
2018-08-23 08:33:18 +00:00
</p>
<p>
2019-10-15 19:19:14 +00:00
You 'll notice the map has several phone numbers on it—if in doubt, please call the Venue Operations Centre (VOC) in case of any emergency, as you' ll get a faster response than by calling your captain or myself .
2018-08-23 08:53:02 +00:00
</p>
<p>
It can get quite cold in the mornings at this time of year ; please be sure to bring something warm ( wear it <i> under </i> your bib , please ! ) , as well as sunscreen and a hat . You may also wish to bring water or coffee and something to eat , and even something to sit on . Please do encourage the runners as they run past you !
2018-08-23 08:33:18 +00:00
</p>
<p>
Regards ,
</p>
<p>
Tim
</p>
</body>
</html>
) ;
2018-08-23 08:53:02 +00:00
2018-08-23 08:33:18 +00:00
my $ plain_text = qq( Dear $salutation
2019-10-25 07:40:29 +00:00
I believe that many marshals didn 't receive their maps, so I' m re - sending to everyone . If you received your map before , please accept my apologies , along with the attached voucher for parking in P9 ! I 've also attached a map that shows you how to get to P9: turn off Helen Suzman by the Virgin Active, turn right at the T-junction, and pass through the gate at the end of the road, onto the field in Green Point Park. Please note that you will need to print the map (security won' t accept an image on your phone ) .
2018-08-23 08:33:18 +00:00
2018-10-11 12:02:09 +00:00
Attached to this email is a map that gives your marshalling position . You are in position $ position . It ' s important to be in place by $ position_time on the day , as the police and traffic officials will be conducting spot checks at that point , and if any marshal is not in position , they may call off the race or delay it .
2018-08-23 08:33:18 +00:00
2019-10-15 19:19:14 +00:00
If you have any trouble opening the attachment , the map is available online as a PDF document at <http://node.org.za/GunRun2019/$club/$pdf_filename> and as an image at <http://node.org.za/GunRun2019/$club/$jpg_filename> .
2018-08-23 08:53:02 +00:00
2019-10-15 19:19:14 +00:00
In addition , there is a complete route map at <https://node.org.za/GunRun2019/Gun%20Run%202019%20-%20Routes.pdf> and a map of all marshal positions at <https://node.org.za/GunRun2019/Gun%20Run%202019%20-%20Marshal%20Plan.pdf> . All other maps ( including cones and fencing ; road closures ; and directions to the start ) are available as well at <https://node.org.za/GunRun2019/> .
2018-08-23 08:53:02 +00:00
2019-10-15 19:19:14 +00:00
You 'll notice the map has several phone numbers on it -- if in doubt, please call the Venue Operations Centre (VOC) in case of any emergency, as you' ll get a faster response than by calling your captain or myself .
2018-08-23 08:33:18 +00:00
It can get quite cold in the mornings at this time of year ; please be sure to bring something warm ( wear it under your bib , please ! ) , as well as sunscreen and a hat . You may also wish to bring water or coffee and something to eat , and even something to sit on . Please do encourage the runners as they run past you !
Regards ,
Tim
) ;
2018-08-23 08:53:02 +00:00
$ pdf_filename =~ s/^.*?_// ;
2019-10-25 07:40:29 +00:00
my % objects = (
"$pdf_filename" = > $ pdf ,
"Gun_Run_2019_-_P9_Marshal_Parking.pdf" = > $ parking ,
"Gun_Run_2019_-_P9_Parking_Access.pdf" = > $ directions ,
) ;
2018-08-23 08:53:02 +00:00
2018-08-23 08:33:18 +00:00
my $ mail = Email::MIME - > create_html (
header = > [
From = > 'Timothy Allen <tim@allen.org.za>' ,
2019-10-25 07:40:29 +00:00
Subject = > 'Your Gun Run 2019 marshalling position (with parking voucher)' ,
2018-08-23 08:33:18 +00:00
To = > $ m ,
] ,
body = > $ html ,
text_body = > $ plain_text ,
objects = > \ % objects ,
) ;
my $ g_transport = Email::Sender::Transport::SMTP::TLS - > new (
host = > 'smtp.gmail.com' ,
username = > 'trallen@gmail.com' ,
2018-10-11 08:28:05 +00:00
password = > $ password ,
2018-08-23 08:33:18 +00:00
) ;
my $ t_transport = Email::Sender::Transport::SMTP::TLS - > new (
host = > 'mail.treehouse.org.za' ,
username = > 'tim' ,
2018-10-11 08:28:05 +00:00
password = > $ password ,
2018-08-23 08:33:18 +00:00
) ;
try {
2019-10-15 19:19:14 +00:00
# sendmail($mail, { transport => $g_transport }) if ($debug <= 1);
sendmail ( $ mail , { transport = > $ t_transport } ) if ( $ debug <= 1 ) ;
2018-08-23 08:33:18 +00:00
} catch {
warn "\n\nError sending email for position $position\n\n" ;
warn $ _ ;
} ;
}
2018-10-11 08:28:05 +00:00
sleep 2 if ( $ debug < 1 ) ;
2018-08-23 08:53:02 +00:00
last if ( $ debug == 1 ) ;
2018-08-23 08:33:18 +00:00
}
2018-10-11 08:28:05 +00:00
# vim: set expandtab shiftwidth=4 softtabstop=4 :