Skip to content

Instantly share code, notes, and snippets.

@sarguru
Created February 11, 2012 13:24
Show Gist options
  • Save sarguru/1799404 to your computer and use it in GitHub Desktop.
Save sarguru/1799404 to your computer and use it in GitHub Desktop.
RT Reminder mail
#!/usr/bin/env 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
# Modifed by Mario A. del Riego <delriego AT fing DOT edu DOT uy>
# Added tainting checks
# 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.1
## Modified by Sarguru Nathan < sarguru AT deeproot DOT in>
## Added an option -b or --difference which can controls the sending of the mail per hour.
## used qmail-sendmail for sending the mail.
##chaaged the mail format.
##fine grained reminder sending to hours. Fixed bug of sending mails to expired reminders within the day.
package RT;
# 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 = 1;
# The mail "From" address.
my $CONFIG_FROM = 'FROM@FROM.COM';
# The mail "To" address if the Reminder *or* Ticket owner is Nobody.
my $CONFIG_TO_NOBODY = 'NOBODY@NOBODY.COM';
# The mail "Subject" line.
my $CONFIG_PRESUBJECT = 'REMINDER: CSD';
# Send mail to REMINDER OWNER
my $ReminderOwner;
# Send mail to TICKET OWNER
my $TicketOwner;
# Only show the mails for debugging
my $debug = 0;
my $interval = undef;
my $help;
my $difference = undef;
##########################################################################
use lib '/usr/local/rt4/lib/';
use Getopt::Long;
use Data::Dumper;
use Date::Calc qw(Today Add_Delta_Days);
use RT::Interface::CLI qw(CleanEnv GetMessageContent loc);
use Date::Parse;
# 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;
sub usage {
print "--reminder|-r Send mail to REMINDER OWNER\n";
print "--ticket|-t Send mail to TICKER 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";
print "--difference|-b <int> Time difference for calculation of mail \n";
exit;
}
if(not GetOptions(
'r|reminder' => \$ReminderOwner,
't|ticket' => \$TicketOwner,
'd|debug' => \$debug,
'i|interval=i' => \$interval,
'h|help' => \$help,
'b|difference=i' => \$difference
)
) {
usage();
}
if ($help) { usage(); }
if (not defined $interval) { $interval = $CONFIG_DUE_DAYS; }
if (not defined $difference) { $difference = 2; }
# 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;
my $duestring = $ticket->Due;
my $isttime = str2time($duestring)+19800;
my $loctime = localtime($isttime);
# print "IST time is $isttime";
my $prtime = time;
# print "Current time is $prtime";
my $difftime = $isttime - $prtime;
#print "diff time $difftime \n";
my $caldiff = $difference*3600;
#print "call dif $caldiff \n";
# Format:
# "Reminder: <subject_reminder> ([RT #<TicketID> <TicketSubject>])
# Terminar en <left_hour> hours"
my $t = RT::Ticket->new($RT::SystemUser);
$t->Load($ticket->RefersTo->First->LocalTarget);
$out = sprintf(
"Reminder: %s ([RT #%s] %s )\n".
" Terminates in %s\n".
"Please visit https://YOUR RT URL/rt4/Ticket/Display.html?id=%s for complete information.\n".
"\n",
$ticket->Subject, $ticket->RefersTo->First->LocalTarget, $t->Subject,
loc($ticket->DueObj->AgeAsString),$ticket->RefersTo->First->LocalTarget);
my $tmp_rcpt_reminder = undef;
$TID = $ticket->RefersTo->First->LocalTarget;
my $ticket_subject = $ticket->Subject;
$CONFIG_SUBJECT = $CONFIG_PRESUBJECT." Ticket"."[#".$TID."] "." ".$ticket_subject." ".$loctime;
if (($difftime < $caldiff) ) {
if ($ReminderOwner) {
# Push reminder to array of distinct e-mail addresses for this ticket.
$tmp_rcpt_reminder = $ticket->OwnerObj->EmailAddress || $CONFIG_TO_NOBODY;
if (not 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 $sendmail = "/var/qmail/bin/sendmail -f $CONFIG_FROM -t";
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?I am using qmail-send
open(MAIL, "| $sendmail") or die("open sendmail: $!");
print(MAIL $mail) or die("print sendmail: $!");
close(MAIL) or die("close sendmail: $!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment