From db43a3ed71e9c6c5def1470022d8be377220d3e0 Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Thu, 31 Dec 2015 02:16:30 -0800 Subject: [PATCH] First draft of renewal-notices script. --- scripts/send-renewal-notices.plx | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 scripts/send-renewal-notices.plx diff --git a/scripts/send-renewal-notices.plx b/scripts/send-renewal-notices.plx new file mode 100644 index 0000000..a9ace92 --- /dev/null +++ b/scripts/send-renewal-notices.plx @@ -0,0 +1,61 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use autodie qw(open close); + +use DBI; +use Encode qw(encode decode); +use Date::Manip::DM5; +use Supporters; + +my $TODAY = UnixDate(ParseDate("today"), '%Y-%m-%d'); + +if (@ARGV < 6) { + print STDERR "usage: $0 \n"; + exit 1; +} + +my($SUPPORTERS_SQLITE_DB_FILE, $REQUEST_NAME, $BCC_ADDRESS, $EMAIL_TEMPLATE, $MONTHLY_SEARCH_REGEX, $ANNUAL_SEARCH_REGEX, + @LEDGER_CMN_LINE) = @ARGV; + + +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_CMN_LINE, { monthly => $MONTHLY_SEARCH_REGEX, annual => $ANNUAL_SEARCH_REGEX}); + +my(@supporterIds) = $sp->findDonor({}); + +foreach my $supporterId (@supporterIds) { + my $expiresOn = $sp->supporterExpirationDate($supporterId); + next unless ( (not defined $expiresOn) or $expiresOn lt $TODAY); + + my %emails; + my $email = $sp->getPreferredEmailAddress($supporterId); + if (defined $email) { + $emails{$email} = {}; + } else { + %emails = $sp->getEmailAddresses($supporterId); + } + my $lastDonateDate = $sp->donorLastGave($supporterId); + + open(MESSAGE, "<", $EMAIL_TEMPLATE); + my @message; + while (my $line = ) { + $line =~ s/FIXME_LAST_DONATE_DATE/$lastDonateDate/g; + push(@message, $line); + } + close MESSAGE; + open(SENDMAIL, "|/usr/lib/sendmail -f \"$FROM_ADDRESS\" -oi -oem -- $emailTo $FROM_ADDRESS") or + die "unable to run sendmail: $!"; + + print "To: ", join(', ', keys %emails), "\n"; + print SENDMAIL @message; + + close SENDMAIL; + sleep 1; + $sp->addRequest({donorId => $supporterId, requestType => $REQUEST_NAME}); +}