Wikia

Request Tracker Wiki

RtReminderMails

Comment1
856pages on
this wiki

This is a small cron script that sends daily emails about reminders that will be due in the next 48h to the owner of the original ticket as well as the owner of the reminder.

Note - it requires the Date::Calc and Getopt::Long perl modules.

 #!/usr/bin/perl
 #
 # This script outputs all reminders that are due within the next e.g. 2 days
 # and mails the result to the corresponding owners. christian <ch AT westend.com>
 #####
 # FING - UdelaR
 # Modified by Mario A. del Riego <delriego AT fing DOT edu DOT uy>
 # Modified by Gary Greene <ggreene AT minervanetworks DOT com>
 # Restructured to allow cleaner execution flow
 # Removed taint check as it does not work currently
 # Fixed an old variable dont defined
 # Changed mail format (ticket id added)
 # Getopt added for parameters: --debug --reminder --ticket --interval and --help
 # File: http://www.fing.edu.uy/~delriego/RT_Reminder.pl
 # v 1.5
 
 package RT;
 
 use strict;
 use warnings;
 use Getopt::Long;
 use Data::Dumper;
 use Date::Calc qw(Today Add_Delta_Days);
 use RT::Interface::CLI qw(CleanEnv GetMessageContent loc);
 
 # Show reminders that are due between today and the day after tomorrow.
 ## Bug fixed by sean <smahan (works for) smwm.com>
 my $CONFIG_DUE_DAYS = 2;
 # The mail "From" address.
 my $CONFIG_FROM = 'rt-helpdesk@minervanetworks.com';
 # The mail "To" address if the Reminder *or* Ticket owner is Nobody.
 my $CONFIG_TO_NOBODY = 'blackhole@minervanetworks.com';
 # The mail "Subject" line.
 my $CONFIG_SUBJECT = 'RT Reminder';
 
 # Send mail to REMINDER OWNER
 my $ReminderOwner;
 # Send mail to TICKER OWNER
 my $TicketOwner;
 # Only show the mails for debugging
 my $debug = 0;
 my $interval = undef;
 
 my $help;
 
 sub usage {
     print "rt-reminders.pl - A script to send alerts for reminders in the RT  system.\n";
    print "Contributed by several members of the RT community.\n";
    print "Under the same license as RT itself.\n";
    print "-------------------------------------------------------------------------\n\n";
    print "--reminder|-r        Send mail to the reminder's owner\n";
    print "--ticket|-t          Send mail to the ticket's owner\n";
    print "--interval|-i <int>  Days left to end time (default < $CONFIG_DUE_DAYS)\n";
    print "--debug|-d           Only show mails to send for debugging\n";
    print "--help|-h            This help!\n\n";
    exit;
 }
 
 sub main {
    unless (defined($ReminderOwner) || defined($TicketOwner)) {
 	usage();
    }
 
    # RT API boilerplate +++++++++++++++++
    # Clean our the environment
    CleanEnv();
    # Load the RT configuration
    RT::LoadConfig();
    # Initialise RT
    RT::Init();
 
    # Load rest of the RT stuff (seems to go after initialization)
    use RT::Date;
    use RT::Queue;
    use RT::Tickets;
    use RT::Action::SendEmail;
    # END RT API boilerplate +++++++++++++
 
    if (! defined($interval)) {
        $interval = $CONFIG_DUE_DAYS;
    }
 
    # Calculate date boundaries.
    my ($dy,$dm,$dd) = Today();
    my $d_now  = sprintf('%04d-%02d-%02d 00:00:00', $dy, $dm, $dd);
    my $d_then = sprintf('%04d-%02d-%02d 23:59:59', Add_Delta_Days($dy, $dm, $dd, $interval));
 
    # Fetch list of matching tickets.
    my $tickets = RT::Tickets->new($RT::SystemUser);
    $tickets->FromSQL(
               'Type = "reminder" AND '.
               '(Status = "new" OR Status = "open") AND '.
               'Due >= "'.$d_now.'" AND '.
               'Due <= "'.$d_then.'"');
    $tickets->OrderBy(FIELD => 'Due', ORDER => 'DESC');
 
    # Format result and group by e-mail address.
    my %rcpts = ();
    while (my $ticket = $tickets->Next) {
        my $out;
 
        # Format:
        # "Reminder: <subject_reminder> ([RT #<TicketID> <TicketSubject>])
        #       Due: <left_hour> hours"
 
        my $t = RT::Ticket->new($RT::SystemUser);
        $t->Load($ticket->RefersTo->First->LocalTarget);
 
        $out = sprintf(
                "Reminder: %s ([RT #%s] %s )\n" .
                "     Due: %s\n\n",
                $ticket->Subject, $ticket->RefersTo->First->LocalTarget, 
                $t->Subject, loc($ticket->DueObj->AgeAsString));
 
        my $tmp_rcpt_reminder = undef;
        if ($ReminderOwner) {
            # Push reminder to array of distinct e-mail addresses for this ticket.
            $tmp_rcpt_reminder = $ticket->OwnerObj->EmailAddress || $CONFIG_TO_NOBODY;
            if (! defined($rcpts{$tmp_rcpt_reminder})) {
                $rcpts{$tmp_rcpt_reminder} = ""
            }
            $rcpts{ $tmp_rcpt_reminder } .= $out;
        }
 
        if ($TicketOwner) {
            # Notify ticket owner or "nobody" if ticket is unowned
            my $tmp_rcpt_ticket   = $t->OwnerObj->EmailAddress || $CONFIG_TO_NOBODY;
            if (defined($CONFIG_TO_NOBODY) && $tmp_rcpt_ticket ne $tmp_rcpt_reminder) {
                $rcpts{$tmp_rcpt_ticket} .= $out;
 	    }
 	}
    }
 
    # Iterate over each of the tickets and send the email
    foreach my $rcpt (keys %rcpts) {
        my $mail = "From: $CONFIG_FROM\n".
                   "To: $rcpt\n".
                   "Subject: $CONFIG_SUBJECT\n\n".
                   $rcpts{$rcpt};
        if ($debug) {
            print $mail . "\n";
        } else {
            # FIXME: Is there no proper RT library for this?
            no warnings;
            open(MAIL, "| $RT::SendmailPath $RT::SendmailBounceArguments $RT::SendmailArguments") or die("open  sendmail: $!");
            use warnings;
            print(MAIL $mail) or die("print sendmail: $!");
            close(MAIL) or die("close sendmail: $!");
        }
    }
 }
 
 GetOptions(
    'r|reminder' => \$ReminderOwner,
    't|ticket' => \$TicketOwner,
    'd|debug' => \$debug,
    'i|interval=i' => \$interval,
    'h|help' => sub { usage() }
 ) or usage();
 
 # while having a main isn't required in Perl, this cleans up and organizes the flow of the code....
 main();

Around Wikia's network

Random Wiki