2017-01-12 18:31:50 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use autodie qw(open close);
|
|
|
|
use DBI;
|
|
|
|
|
|
|
|
use Date::Manip::DM5;
|
|
|
|
use Supporters;
|
2018-01-24 19:47:47 +00:00
|
|
|
use Encode qw(encode decode);
|
|
|
|
use Email::MIME::RFC2047::Encoder;
|
|
|
|
use utf8;
|
|
|
|
|
|
|
|
my $encoder = Email::MIME::RFC2047::Encoder->new();
|
2017-01-12 18:31:50 +00:00
|
|
|
|
|
|
|
my $TODAY = UnixDate(ParseDate("today"), '%Y-%m-%d');
|
|
|
|
|
|
|
|
if (@ARGV < 5) {
|
|
|
|
print STDERR "usage: $0 <SUPPORTERS_SQLITE_DB_FILE> <FROM_ADDRESS> <EMAIL_CONTENTS_FILE> <MONTHLY_SEARCH_REGEX> <ANNUAL_SEARCH_REGEX> <VERBOSE> <LEDGER_CMD_LINE> <LEDGER_COMMAND_LINE>\n";
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
my($SUPPORTERS_SQLITE_DB_FILE, $FROM_ADDDRESS, $EMAIL_CONTENTS_FILE, $MONTHLY_SEARCH_REGEX, $ANNUAL_SEARCH_REGEX, $VERBOSE,
|
|
|
|
@LEDGER_CMD_LINE) = @ARGV;
|
|
|
|
$VERBOSE = 0 if not defined $VERBOSE;
|
|
|
|
|
|
|
|
my $dbh = DBI->connect("dbi:SQLite:dbname=$SUPPORTERS_SQLITE_DB_FILE", "", "",
|
|
|
|
{ RaiseError => 1, sqlite_unicode => 1 })
|
|
|
|
or die $DBI::errstr;
|
|
|
|
|
|
|
|
my $sp = new Supporters($dbh, \@LEDGER_CMD_LINE, { monthly => $MONTHLY_SEARCH_REGEX, annual => $ANNUAL_SEARCH_REGEX});
|
|
|
|
|
|
|
|
open(EMAIL, "<", $EMAIL_CONTENTS_FILE);
|
|
|
|
my(@emailLines) = <EMAIL>;
|
|
|
|
close EMAIL;
|
|
|
|
|
|
|
|
my(@supporterIds) = $sp->findDonor({});
|
|
|
|
foreach my $id (@supporterIds) {
|
2019-12-10 17:30:32 +00:00
|
|
|
my $donorType = $self->getType($id);
|
2017-01-12 18:31:50 +00:00
|
|
|
my $expiresOn = $sp->supporterExpirationDate($id);
|
|
|
|
my $isLapsed = ( (not defined $expiresOn) or $expiresOn lt $TODAY);
|
2019-12-10 17:30:32 +00:00
|
|
|
|
|
|
|
my $amount = $sp->donorTotalGaveInPeriod(donorId => $id);
|
|
|
|
my $lastGaveDate = $sp->donorLastGave($id);
|
|
|
|
my $firstGaveDate = $sp->donorFirstGave($id);
|
|
|
|
my $nineMonthsSinceFirstGave = UnixDate(DateCalc(ParseDate($firstGaveDate), "+ 9 months"), '%Y-%m-%d');
|
2017-01-18 03:34:15 +00:00
|
|
|
|
2018-01-24 19:47:47 +00:00
|
|
|
|
|
|
|
# invalid email address
|
|
|
|
my @invalidEmailAddresses = qw//;
|
|
|
|
next if ($id ~~ @invalidEmailAddresses);
|
2017-01-18 03:34:15 +00:00
|
|
|
next unless $sp->emailOk($id);
|
2018-01-24 19:47:47 +00:00
|
|
|
my %emails;
|
|
|
|
my $email = $sp->getPreferredEmailAddress($id);
|
|
|
|
if (defined $email) {
|
|
|
|
$emails{$email} = {};
|
2017-08-01 16:21:34 +00:00
|
|
|
} else {
|
2018-01-24 19:47:47 +00:00
|
|
|
%emails = $sp->getEmailAddresses($id);
|
2017-08-01 16:21:34 +00:00
|
|
|
}
|
2018-01-24 19:47:47 +00:00
|
|
|
my(@emails) = keys(%emails);
|
|
|
|
|
2017-08-01 16:21:34 +00:00
|
|
|
my $fullEmailLine = "";
|
|
|
|
my $emailTo = join(' ', @emails);
|
2018-01-24 19:47:47 +00:00
|
|
|
my $displayName = $sp->getDisplayName($id);
|
2017-08-01 16:21:34 +00:00
|
|
|
foreach my $email (@emails) {
|
|
|
|
$fullEmailLine .= ", " if ($fullEmailLine ne "");
|
|
|
|
my $line = "";
|
|
|
|
if (defined $displayName) {
|
2018-01-24 19:47:47 +00:00
|
|
|
$line .= $encoder->encode_phrase($displayName) . " ";
|
2017-08-01 16:21:34 +00:00
|
|
|
}
|
|
|
|
$line .= "<$email>";
|
2018-01-24 19:47:47 +00:00
|
|
|
$fullEmailLine .= $line;
|
2017-01-12 18:31:50 +00:00
|
|
|
}
|
|
|
|
open(SENDMAIL, "|-", "/usr/lib/sendmail -f \"$FROM_ADDDRESS\" -oi -oem -- \'$emailTo\'");
|
|
|
|
|
2017-08-01 16:21:34 +00:00
|
|
|
|
|
|
|
print SENDMAIL "To: $fullEmailLine\n";
|
2017-01-12 18:31:50 +00:00
|
|
|
print SENDMAIL @emailLines;
|
|
|
|
close SENDMAIL;
|
|
|
|
|
2017-08-01 16:21:34 +00:00
|
|
|
print STDERR "Emailed $emailTo with $id who expires on $expiresOn\n" if ($VERBOSE);
|
2017-01-12 18:31:50 +00:00
|
|
|
}
|
|
|
|
###############################################################################
|
|
|
|
#
|
|
|
|
# Local variables:
|
|
|
|
# compile-command: "perl -c send-mass-email.plx"
|
|
|
|
# End:
|
|
|
|
|