| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | ############################################# | ||
| 4 | ## ## | ||
| 5 | ## notify_expiring_members.pl ## | ||
| 6 | ## ## | ||
| 7 | ## Perl script to send email to Members of ## | ||
| 8 | ## a Drupal website when their Membership ## | ||
| 9 | ## is getting close to expiration. ## | ||
| 10 | ## ## | ||
| 11 | ## See notify_expiring_members.README ## | ||
| 12 | ## ## | ||
| 13 | ############################################# | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | |||
| 18 | use Cwd qw/ realpath /; | ||
| 19 | use Config::Tiny; | ||
| 20 | use POSIX qw/ strftime /; | ||
| 21 | use DBI; | ||
| 22 | use DBD::mysql; | ||
| 23 | use MIME::Lite::TT; | ||
| 24 | use Carp; | ||
| 25 | |||
| 26 | my $path = realpath($0); | ||
| 27 | $path =~ s/\/notify_expiring_members\.pl$//x; | ||
| 28 | |||
| 29 | my $conf = Config::Tiny->new; | ||
| 30 | $conf = Config::Tiny->read( "$path/notify_expiring_members.config" ) | ||
| 31 | or croak("Died: Config:" . Config::Tiny->errstr); | ||
| 32 | |||
| 33 | my $DEBUG = $conf->{'program'}->{'debug'}; | ||
| 34 | |||
| 35 | print("\nInitializing at " . strftime("%F %T", localtime $^T) . "\n"); | ||
| 36 | $DEBUG > 0 && print("Running in DEBUG mode.\n"); | ||
| 37 | |||
| 38 | my $dsn = "dbi:mysql:" . join( ';', $conf->{'database'}->{'database'}, | ||
| 39 | $conf->{'database'}->{'host'}, | ||
| 40 | $conf->{'database'}->{'port'} ); | ||
| 41 | |||
| 42 | my $dbh = DBI->connect( $dsn, | ||
| 43 | $conf->{'database'}->{'user'}, | ||
| 44 | $conf->{'database'}->{'password'}, | ||
| 45 | { RaiseError => 1 } ); | ||
| 46 | |||
| 47 | ## nested subselect because a user may have a Membership Term that is about | ||
| 48 | ## to expire but already have another active one ready to begin | ||
| 49 | |||
| 50 | my $sql = <<"end_of_sql"; | ||
| 51 | |||
| 52 | SELECT | ||
| 53 | active.uid AS uid, | ||
| 54 | active.user AS user, | ||
| 55 | active.email AS email, | ||
| 56 | active.name AS name, | ||
| 57 | DATE_FORMAT(active.end_date, '%W, %M %D, %Y') AS end_date | ||
| 58 | FROM | ||
| 59 | ( | ||
| 60 | SELECT | ||
| 61 | users.uid AS uid, | ||
| 62 | users.name AS user, | ||
| 63 | users.mail AS email, | ||
| 64 | field_data_field_first_name.field_first_name_value AS name, | ||
| 65 | MAX(membership_entity_term.end) AS end_date | ||
| 66 | FROM | ||
| 67 | membership_entity_term | ||
| 68 | INNER JOIN | ||
| 69 | membership_entity | ||
| 70 | ON | ||
| 71 | membership_entity.mid = membership_entity_term.mid | ||
| 72 | LEFT JOIN | ||
| 73 | users | ||
| 74 | ON | ||
| 75 | users.uid = membership_entity.uid | ||
| 76 | LEFT JOIN | ||
| 77 | field_data_field_first_name | ||
| 78 | ON | ||
| 79 | field_data_field_first_name.entity_id = membership_entity.uid | ||
| 80 | WHERE | ||
| 81 | membership_entity_term.status IN ('0', '1') | ||
| 82 | GROUP BY users.uid, membership_entity.type | ||
| 83 | ) | ||
| 84 | AS active | ||
| 85 | WHERE | ||
| 86 | DATEDIFF(end_date, CURRENT_DATE) = ? | ||
| 87 | |||
| 88 | end_of_sql | ||
| 89 | |||
| 90 | my $query = $dbh->prepare($sql); | ||
| 91 | |||
| 92 | foreach my $num_days($DEBUG > 2 ? (1..31) : (sort {$a <=> $b} keys %{ $conf->{'notifications'} })) { | ||
| 93 | |||
| 94 | print(" ", "-" x 62, "\n"); | ||
| 95 | print(" Processing for $num_days ...\n"); | ||
| 96 | |||
| 97 | $query->execute($num_days); | ||
| 98 | while (my $row = $query->fetchrow_hashref) { | ||
| 99 | |||
| 100 | print " $row->{'uid'} $row->{'user'} $row->{'end_date'}\n"; | ||
| 101 | |||
| 102 | my %template_options = (); | ||
| 103 | my %template_params = ( | ||
| 104 | 'name' => $row->{'name'}, | ||
| 105 | 'organization' => $conf->{'organization'}->{'long_name'}, | ||
| 106 | 'date' => $row->{'end_date'}, | ||
| 107 | 'website' => $conf->{'organization'}->{'website'}, | ||
| 108 | 'user' => $row->{'user'}, | ||
| 109 | 'signature' => $conf->{'email'}->{'signature'}, | ||
| 110 | ); | ||
| 111 | |||
| 112 | my $msg = MIME::Lite::TT->new( | ||
| 113 | 'From' => $conf->{'email'}->{'sender'}, | ||
| 114 | 'To' => $row->{'email'}, | ||
| 115 | 'CC' => $conf->{'email'}->{'copy_to'}, | ||
| 116 | 'Subject' => "$row->{'user'}, your $conf->{'organization'}->{'short_name'} Membership " . | ||
| 117 | ($num_days < 1 ? "expired " : "expires in ") . | ||
| 118 | abs($num_days) . " " . (abs($num_days) == 1 ? "day" : "days") . | ||
| 119 | ($num_days < 1 ? " ago" : ""), | ||
| 120 | 'Template' => join('/', $path, $conf->{'notifications'}->{ $DEBUG > 2 ? 7 : $num_days }), | ||
| 121 | 'TmplOptions' => \%template_options, | ||
| 122 | 'TmplParams' => \%template_params, | ||
| 123 | ) or croak($!); | ||
| 124 | |||
| 125 | print(" Sending email ..."); | ||
| 126 | $DEBUG > 1 && $msg->print(); | ||
| 127 | |||
| 128 | unless ($DEBUG) { $msg->send() or croak($!); } | ||
| 129 | |||
| 130 | print(" email sent." . ($DEBUG > 0 ? " (Not really. We're debugging.)\n" : "\n")); | ||
| 131 | } | ||
| 132 | print(" Finished.\n"); | ||
| 133 | } | ||
| 134 | print(" ", "-" x 62, "\n"); | ||
| 135 | print("Finished at " . strftime("%F %T", localtime $^T) . ".\n"); | ||
| 136 | print("#" x 64, "\n"); | ||
| 137 | |||
| 138 | exit 1; | ||
| 139 | |||
| 140 | =pod | ||
| 141 | |||
| 142 | =head1 SYNOPSIS | ||
| 143 | |||
| 144 | This is a Perl program to automate sending reminder notifications via email to | ||
| 145 | Members of a Drupal website whose Membership is soon to expire. It works with | ||
| 146 | the Membership Entity Drupal contributed modules, which do not, as of 12/3/2014, | ||
| 147 | expose expiration dates to work with Drupal Rules. | ||
| 148 | |||
| 149 | The program uses an external configuration file for setting options and the Perl | ||
| 150 | Template Toolkit to keep the content of the notification emails out of the | ||
| 151 | program code. You will have to edit the configuration file, and may edit the | ||
| 152 | templates to customize your reminder notifications, but you don't have to edit | ||
| 153 | any code or even know anything about it to use this program. | ||
| 154 | |||
| 155 | To repeat: you will have to copy notify_expiring_members.config.sample to | ||
| 156 | notify_expiring_members.config and then edit it. But you can use the provided | ||
| 157 | template files out of the box if you wish. | ||
| 158 | |||
| 159 | =head1 CONTENTS | ||
| 160 | |||
| 161 | This program should have been distributed with the following files: | ||
| 162 | |||
| 163 | notify_expiring_member.pl <- the script, must be executable | ||
| 164 | notify_expiring_members.config.sample <- sample config file | ||
| 165 | initial.notification <- sample notification template | ||
| 166 | final.notification <- sample notification template | ||
| 167 | notify_expiring_members.README <- documentation; this file | ||
| 168 | |||
| 169 | =head1 INSTALLATION | ||
| 170 | |||
| 171 | Copy the files in this distribution to a directory where you have permission to | ||
| 172 | execute files. If you download and unpack the .tar.gz archive, it will create | ||
| 173 | create a directory called notify_expiring_members in which all the files will be | ||
| 174 | placed. | ||
| 175 | |||
| 176 | Copy the file notify_expiring_members.config.sample to | ||
| 177 | notify_expiring_members.config and then configure for your system. | ||
| 178 | |||
| 179 | Set a daily cron job to run the script. | ||
| 180 | |||
| 181 | =head2 Dependencies | ||
| 182 | |||
| 183 | This program depends on the following Perl modules: | ||
| 184 | |||
| 185 | L<DBI> | ||
| 186 | |||
| 187 | L<DBD::mysql> | ||
| 188 | |||
| 189 | L<Config::Tiny> | ||
| 190 | |||
| 191 | L<MIME::Lite::TT> | ||
| 192 | |||
| 193 | The modules listed above have other dependencies, including L<MIME::Lite> and | ||
| 194 | L<Template::Toolkit>, but these are installed on most shared hosting servers. | ||
| 195 | L<MIME::Lite::TT> and L<Config::Tiny> do not require compilation so you should be | ||
| 196 | able to install them in your local Perl lib directory even without root access. | ||
| 197 | |||
| 198 | =head1 USAGE | ||
| 199 | |||
| 200 | =head2 Configuration | ||
| 201 | |||
| 202 | This program stores all its configuration information in the file | ||
| 203 | notify_expiring_members.config . The file is in the .ini format, like so: | ||
| 204 | |||
| 205 | B<[section]> | ||
| 206 | |||
| 207 | conf_option = value | ||
| 208 | |||
| 209 | Note that no comments are allowed in the config file. | ||
| 210 | |||
| 211 | The following are the configuration options: | ||
| 212 | |||
| 213 | ---------------------------------------------------- | ||
| 214 | |||
| 215 | B<[program]> | ||
| 216 | |||
| 217 | debug = switch to output debugging data at runtime | ||
| 218 | |||
| 219 | B<[database]> | ||
| 220 | |||
| 221 | database = your database name | ||
| 222 | |||
| 223 | host = the host your database runs on | ||
| 224 | (leave blank to use localhost) | ||
| 225 | |||
| 226 | port = the port your database listens on | ||
| 227 | (leave blank to use 3306) | ||
| 228 | |||
| 229 | user = the database user | ||
| 230 | (leave blank if MySQL uses your system username) | ||
| 231 | |||
| 232 | password = the database password for the database user | ||
| 233 | (leave blank if none) | ||
| 234 | |||
| 235 | B<[organization]> | ||
| 236 | |||
| 237 | short_name = short name for your organization used in email Subject | ||
| 238 | |||
| 239 | long_name = long name for your organization used in email body | ||
| 240 | |||
| 241 | website = URL of the website to which the Member has a Membership | ||
| 242 | |||
| 243 | B<[email]> | ||
| 244 | |||
| 245 | sender = the email address from which to send notifications | ||
| 246 | |||
| 247 | signature = a one-line signature appended to email body | ||
| 248 | |||
| 249 | copy_to = a comma-separated list of email addresses to cc | ||
| 250 | |||
| 251 | B<[notifications]> | ||
| 252 | |||
| 253 | n = /path/to/template (where the email sent to Members with | ||
| 254 | n days remaining will be created with the named | ||
| 255 | template) | ||
| 256 | |||
| 257 | ------------------------------- | ||
| 258 | |||
| 259 | B<A note about [notifications]> | ||
| 260 | |||
| 261 | You can send reminder notifications on any number of days before Membership | ||
| 262 | expiration. For each number of days before expiration for which you want the | ||
| 263 | program to search for soon-to-expire Members, you must include a configuration | ||
| 264 | option in the [notifications] section. The option name should be the number of | ||
| 265 | days and the value should be the path to the template file to be used. You can | ||
| 266 | use a negative number for the number of days if you want to send a notification | ||
| 267 | after expiration has occurred. | ||
| 268 | |||
| 269 | You can create a different notification template for each number of days on | ||
| 270 | which notification is to be sent, or you can use the same template for more than | ||
| 271 | one notification. For example, my config file contains this: | ||
| 272 | |||
| 273 | B<[notifications]> | ||
| 274 | |||
| 275 | 31 = initial.notification | ||
| 276 | |||
| 277 | 14 = initial.notification | ||
| 278 | |||
| 279 | 7 = initial.notification | ||
| 280 | |||
| 281 | 1 = final.notification | ||
| 282 | |||
| 283 | -2 = expired.notification | ||
| 284 | |||
| 285 | =head2 Templates | ||
| 286 | |||
| 287 | The values for the options in the [notifications] section of the config file | ||
| 288 | are the paths to the template files. You can name the files anything and put | ||
| 289 | them anywhere so long as the path is valid and the file is readable. | ||
| 290 | |||
| 291 | The template is used by the program to build the notification email. It | ||
| 292 | contains tokens so that you don't have to edit the script code. The following | ||
| 293 | tokens are available for use in the templates: | ||
| 294 | |||
| 295 | [% name %] The first name of the Member | ||
| 296 | (from the database) | ||
| 297 | |||
| 298 | [% organization %] The name of the organization to which the Member belongs | ||
| 299 | (from the config file) | ||
| 300 | |||
| 301 | [% date %] The date on which the Membership will expire | ||
| 302 | (from the database) | ||
| 303 | |||
| 304 | [% website %] The URL of the website to which the Member belongs | ||
| 305 | (from the config file) | ||
| 306 | |||
| 307 | [% user %] The user name of the Member on the Drupal site | ||
| 308 | (from the database) | ||
| 309 | |||
| 310 | [% signature %] The signature of the sender of the notification | ||
| 311 | (from the config file) | ||
| 312 | |||
| 313 | =head2 Logging/Error checking | ||
| 314 | |||
| 315 | The program prints a report as it goes, so if you direct the output of | ||
| 316 | the program to a file, it will effectively serve as a log. If you are | ||
| 317 | running it from cron, do something like this: | ||
| 318 | |||
| 319 | 32 3 * * * /path/to/notify_expiring_members.pl >> /path/to/your.log | ||
| 320 | |||
| 321 | The output of the program that will be logged looks like this: | ||
| 322 | |||
| 323 | ################################################################ | ||
| 324 | |||
| 325 | Initializing at 2015-06-07 03:32:01 | ||
| 326 | -------------------------------------------------------------- | ||
| 327 | Processing for 1 ... | ||
| 328 | 12855 rastus Monday, June 8th, 2015 | ||
| 329 | Sending email ... email sent. | ||
| 330 | 11678 user007 Monday, June 8th, 2015 | ||
| 331 | Sending email ... email sent. | ||
| 332 | Finished. | ||
| 333 | -------------------------------------------------------------- | ||
| 334 | Processing for 7 ... | ||
| 335 | Finished. | ||
| 336 | -------------------------------------------------------------- | ||
| 337 | Processing for 14 ... | ||
| 338 | 13067 smcleod Sunday, June 21st, 2015 | ||
| 339 | Sending email ... email sent. | ||
| 340 | Finished. | ||
| 341 | -------------------------------------------------------------- | ||
| 342 | Processing for 31 ... | ||
| 343 | Finished. | ||
| 344 | -------------------------------------------------------------- | ||
| 345 | Finished at 2015-06-07 03:32:01. | ||
| 346 | ################################################################ | ||
| 347 | |||
| 348 | Of course you can always include yourself on the list of copy_to email | ||
| 349 | addresses in the configuration file, so that you see all the emails | ||
| 350 | that the program sends. | ||
| 351 | |||
| 352 | =head1 SUPPORT | ||
| 353 | |||
| 354 | At the moment you can get support for this program at | ||
| 355 | http://tinyurl.com/wbs-nem (the author's blog) or at the Drupal.org | ||
| 356 | thread https://www.drupal.org/node/2385155 | ||
| 357 | |||
| 358 | =head1 AUTHOR | ||
| 359 | |||
| 360 | Nick Tonkin <nick@websitebackendsolutions.com> | ||
| 361 | |||
| 362 | =head1 COPYRIGHT | ||
| 363 | |||
| 364 | Copyright 2014-2015 and All Rights Reserved Nick Tonkin. You may use this | ||
| 365 | software freely and may redistribute it as long as the AUTHOR and COPYRIGHT | ||
| 366 | sections of this file are not removed or changed. | ||
| 367 |