From 35b47468b49506948a753dbf17628659edf21501 Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Thu, 12 Jan 2017 10:31:50 -0800 Subject: [PATCH] send-mass-email script: wrote this a while ago. This script has never been committed before. I wrote it a while ago though. --- scripts/send-mass-email.plx | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 scripts/send-mass-email.plx diff --git a/scripts/send-mass-email.plx b/scripts/send-mass-email.plx new file mode 100644 index 0000000..aa1ba91 --- /dev/null +++ b/scripts/send-mass-email.plx @@ -0,0 +1,59 @@ +#!/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 < 5) { + print STDERR "usage: $0 \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) = ; +close EMAIL; + +my(@supporterIds) = $sp->findDonor({}); +foreach my $id (@supporterIds) { + my $expiresOn = $sp->supporterExpirationDate($id); + my $isLapsed = ( (not defined $expiresOn) or $expiresOn lt $TODAY); + # next if $isLapsed; + next unless $sp->emailOk($donorId); + my $emailTo = $sp->getPreferredEmailAddress($id); + if (not defined $emailTo) { + my(@addr) = $sp->getEmailAddresses($id); + $emailTo = $addr[0]; + } + + open(SENDMAIL, "|-", "/usr/lib/sendmail -f \"$FROM_ADDDRESS\" -oi -oem -- \'$emailTo\'"); + + print SENDMAIL "To: $emailTo\n"; + print SENDMAIL @emailLines; + close SENDMAIL; + + print STDERR "Emailed $emailTo with $id\n" if ($VERBOSE); +} +############################################################################### +# +# Local variables: +# compile-command: "perl -c send-mass-email.plx" +# End: +