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 ;
use File::Basename ;
use Try::Tiny ;
use Data::Dumper ;
2018-08-23 08:53:02 +00:00
my $ email_csv = '/home/tim/Documents/Projects/2017/Gun Run/maps/GIS base files and routes/marshals_by_position.csv' ;
my $ pdf_dir = '/home/tim/Documents/Projects/2017/Gun Run/maps/GIS base files and routes/marshals/' ;
my $ debug = 1 ;
2018-08-23 08:33:18 +00:00
my @ emails ;
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 ] ,
2018-08-23 08:53:02 +00:00
"first_name" = > $ row - > [ 1 ] ,
"name" = > $ row - > [ 3 ] ,
"club" = > $ row - > [ 4 ] ,
"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" } ;
next unless $$ person { "club" } =~ /^AAC$/ ;
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" } ;
next unless $ email =~ /@/ ;
my $ position = $$ person { "position" } ;
#next if ($position =~ /^(68|306|302|74)$/i);
foreach my $ m ( split ( /[, ]+/ , $ email ) ) {
2018-08-23 08:53:02 +00:00
$ m = "$name <$m>" ;
printf "%-4s %-15s %15s\n" , $$ person { "position" } , $ salutation , $ m if ( $ debug <= 0 ) ;
$ m = 'Timothy Allen <tim@allen.org.za>' if ( $ debug == 1 ) ;
my $ pdf = $ pdf_dir . $$ person { "club" } . '_marshal_map_' . $ position . '.pdf' ;
my $ jpg = $ pdf_dir . $$ person { "club" } . '_marshal_map_' . $ position . '.jpg' ;
my $ pdf_filename = basename ( $ pdf ) ;
my $ jpg_filename = basename ( $ jpg ) ;
die "No PDF at $pdf_filename" unless - e $ pdf ;
2018-08-23 08:33:18 +00:00
my $ html = qq(
<html> <head>
</head>
<body>
<p>
Dear $ salutation
</p>
<p>
Thank you for helping out at this year ' s Gun Run !
</p>
<p>
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> 6 AM </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 .
</p>
<p>
2018-08-23 08:53:02 +00:00
If you have any trouble opening the attachment , the map is available online as a < a href = "http://node.org.za/GunRun2017/AAC/$pdf_filename" > PDF document </a> and a < a href = "http://node.org.za/GunRun2017/AAC/$jpg_filename" > JPEG image </a> .
2018-08-23 08:33:18 +00:00
</p>
<p>
2018-08-23 08:53:02 +00:00
In addition , there is a complete < a href = "http://node.org.za/GunRun2017/Gun%20Run%202017%20-%20Routes.pdf" > route map </a> and a map of < a href = "http://node.org.za/GunRun2017/Gun%20Run%202017%20-%20Marshal%20overview.pdf" > all marshal positions </a> .
2018-08-23 08:33:18 +00:00
</p>
<p>
2018-08-23 08:53:02 +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 , Roger , or myself .
</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
Thank you for helping out at this year ' s Gun Run !
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 6 AM 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:53:02 +00:00
If you have any trouble opening the attachment , the map is available online as a PDF document at <http://node.org.za/GunRun2017/AAC/$pdf_filename> and as a JPG image at <http://node.org.za/GunRun2017/AAC/$jpg_filename> .
In addition , there is a complete route map at <http://node.org.za/GunRun2017/Gun%20Run%202017%20-%20Routes.pdf> and a map of all marshal positions at <http://node.org.za/GunRun2017/Gun%20Run%202017%20-%20Marshal%20overview.pdf> .
2018-08-23 08:33:18 +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 , Roger , or myself .
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/^.*?_// ;
my % objects = ( "$pdf_filename" = > $ pdf ) ;
2018-08-23 08:33:18 +00:00
my $ mail = Email::MIME - > create_html (
header = > [
From = > 'Timothy Allen <tim@allen.org.za>' ,
2018-08-23 08:53:02 +00:00
Subject = > 'Your Gun Run 2017 marshalling position' ,
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' ,
password = > '' ,
) ;
my $ t_transport = Email::Sender::Transport::SMTP::TLS - > new (
host = > 'mail.treehouse.org.za' ,
username = > 'tim' ,
password = > '' ,
) ;
try {
2018-08-23 08:53:02 +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-08-23 08:53:02 +00:00
sleep 2 if ( $ debug < 1 ) ;
last if ( $ debug == 1 ) ;
2018-08-23 08:33:18 +00:00
}
# vim: paste