#!/usr/bin/perl -w -I/opt/eprints3/perl_lib =pod =head1 NAME B - Send email message to users. =head1 SYNOPSIS B I I [B] =head1 DESCRIPTION This script sends out email messages to users. =head1 ARGUMENTS =over 8 =item B The ID of the eprint repository to use. =item B The type of user "admin", "editor", "user", etc. These are found in the cfg/namedsets/user file. =back =head1 OPTIONS =over 8 =item B<--help> Print a brief help message and exit. =item B<--man> Print the full manual page and then exit. =item B<--quiet> Be vewwy vewwy quiet. This option will supress all output unless an error occurs. =item B<--verbose> Explain in detail what is going on. May be repeated for greater effect. =item B<--dry-run> Run the script without sending any emails. =item B<--version> Output version information and exit. =back =head1 NOTES This script uses two phrases that must be defined. "email_users/notification_subject" for the subject line of the message. "email_users/notification_body" for the body of the message. =cut use EPrints; use strict; use Getopt::Long; use Pod::Usage; use Data::Dumper::Simple; my $version = 0; my $verbose = 0; my $quiet = 0; my $help = 0; my $man = 0; my $dryrun = 0; Getopt::Long::Configure("permute"); GetOptions( 'help|?' => \$help, 'man' => \$man, 'version' => \$version, 'verbose+' => \$verbose, 'dry-run' => \$dryrun, 'silent' => \$quiet, 'quiet' => \$quiet ) || pod2usage( 2 ); EPrints::Utils::cmd_version( "email_users" ) if $version; pod2usage( 1 ) if $help; pod2usage( -exitstatus => 0, -verbose => 2 ) if $man; pod2usage( 2 ) if( scalar @ARGV != 2 ); my $noise = 1; $noise = 0 if( $quiet ); $noise = 1+$verbose if( $verbose ); # Set STDOUT to auto flush (without needing a \n) $|=1; my $repoid = $ARGV[0]; my $user_type = $ARGV[1]; my $session = new EPrints::Session( 1 , $repoid , $noise ); if( !defined $session ) { print STDERR "Failed to load repository: $repoid\n"; exit 1; } my $ds = $session->get_repository->get_dataset( "user" ); my $searchexp = EPrints::Search->new( satisfy_all => 1, session => $session, dataset => $ds, ); $searchexp->add_field( $ds->get_field( "usertype" ), $user_type ); my $results = $searchexp->perform_search; my $count = $results->count; printf STDERR "Total number of type '%s': %5d\n",$user_type,$count; my $sent=0; my $skip=0; if ($dryrun) { print STDERR "DRY RUN: ON\n"; } sub fn { my( $session, $dataset, $user ) = @_; my( $subject, $message ); $message = $session->html_phrase("email_users/notification_body"); my $email = $user->get_value('email'); my $userid = $user->get_value('userid'); my $name = $user->get_value('name'); print Dumper($name) if $verbose >=2; if (defined $name) { if ($email ne "") { printf STDERR "Sending to userid: %5d - Email: %s",$userid,$email if $verbose >=1; if (!($dryrun)) { print STDERR " (Sent).\n" if $verbose >=1; $user->mail("email_users/notification_subject", $message); } else { print STDERR " (Dry Run - No email sent).\n" if $verbose >=1; } $sent++; } else { printf STDERR "Skipping userid: %5d - Email address is blank.\n"; $skip++; } } else { printf STDERR "Skipping userid: %5d - User record incomplete - No email sent.\n",$userid if $verbose >=1; $skip++; } } $results->map( \&fn ); printf STDERR "Number of Emails sent: %5d.\n",$sent; printf STDERR "Number of Emails skipped: %5d.\n",$skip; $results->dispose; $session->terminate(); exit;