Advertisement
Guest User

duedatereminderbyemail

a guest
Jul 14th, 2016
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.49 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  ####################### RT Email Notification Script ####################
  3.  ####
  4.  #### Author: Daniely Yoav / Qball Technologies Ltd.
  5.  #### Email: yoavd@qballtech.net
  6.  #### Date: 05/06/05
  7.  ####
  8.  #### Modified by: Tomas Borland Valenta
  9.  #### Email: tomas at trustica dot cz
  10.  #### Date: 2007/03/12
  11.  ####
  12.  #### Modified by: Tim Schaller
  13.  #### Email: tim-rt@torzo.com
  14.  #### Date: 2008/06/17
  15.  ####
  16.  #### Modified by: Vaclav Vobornik
  17.  #### Email: vaclav dot vobornik at commerzbank dot com
  18.  #### Date: 2008/07/04
  19.  ####
  20.  #### Purpose: Send Email Notification on all open/new tickets in RT that have their due date expired
  21.  ####
  22.  #### Version: 3.1
  23.  ####
  24.  #### Changes from 3 ( Vaclav Vobornik )
  25.  #### - Added Cc and Bcc possibilities.
  26.  ####
  27.  #### Changes from 2 ( Tim Schaller )
  28.  #### - Added multiple command line options.
  29.  #### - Adaptive subject line.
  30.  #### - Sending Admin CC emails optional.
  31.  ####
  32.  #### Changes from 1.2 ( Tomas Borland Valenta )
  33.  #### - rewriten mail subsystem
  34.  #### - code cleanup
  35.  #### - adopted for RT 3.6.x
  36.  #### - used some global RT config variables
  37.  ####
  38.  #### ======================================================================
  39.  ####
  40.  #### Command line options
  41.  ####          -d          : turns dubugging on
  42.  ####          -A          : Send to AdminCC ( made default not to )
  43.  ####          -a <n>      : Send reminders to tickets due in <n> or less days.
  44.  ####                      : Includes overdue tickets.
  45.  ####          -q <queue>  : Only send reminder for tickets in <queue>
  46.  ####          -o <owner>  : Only send reminders for tickets owned by <owner>
  47.  ####          -c <email>  : Email in Cc
  48.  ####          -b <email>  : Email in Bcc
  49.  ####
  50.  #### ======================================================================
  51.  ####
  52.  ####  Usage: Invoke via cron every working day at 8 morning
  53.  ####  0 8 * * 1-5 /path/to/script/remind_email_due.pl -A
  54.  ####
  55.  ####  Usage: Invoke via cron every working day at 8 morning
  56.  ####       : and send notices to everyone in the SysAdmin Queue
  57.  ####  0 8 * * 1-5 /path/to/script/remind_email_due.pl -q SysAdmin
  58.  ####
  59.  ####  Usage: Invoke via cron every working day at 8 morning
  60.  ####       : and send notices to everyone in the SysAdmin Queue
  61.  ####       : Remind them about tickets due in less then 5 days.
  62.  ####  0 8 * * 1-5 /path/to/script/remind_email_due.pl -a 5 -q SysAdmin
  63.  ####
  64.  ####  Usage: Invoke via cron every working day at 8 morning
  65.  ####       : and send notices to everyone in the SysAdmin Queue.
  66.  ####       : Send copies to all AdminCC on the tickest.
  67.  ####  0 8 * * 1-5 /path/to/script/remind_email_due.pl -A -q SysAdmin
  68.  
  69.  ### External libraries ###
  70.  use strict;
  71.  use Getopt::Std;
  72.  
  73.  use lib ("/opt/rt3/lib");  # Change this to your RT lib path!
  74.  package RT;
  75.  use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
  76.  use RT::Date;
  77.  use RT::Queue;
  78.  use RT::Queues;
  79.  use RT::Tickets;
  80.  
  81.  ################## Init ##################
  82.  # Clean our environment
  83.  CleanEnv();
  84.  # Load the RT configuration
  85.  RT::LoadConfig();
  86.  RT::Init();
  87.  # Set config variables
  88.  my $debug=0;
  89.  my $from_address = $RT::CorrespondAddress; #From: address used in reports
  90.  my $rt_url = $RT::WebURL;
  91.  my $sendmail = "$RT::SendmailPath $RT::SendmailArguments";
  92.  
  93.  ################## Args ##################
  94.  my $queue         = '';
  95.  my $owner         = '';
  96.  my $advDate       = 0;
  97.  my $secInDay      = 60*60*24;
  98.  my $sendToAdminCC = 0;
  99.  my $cc            = '';
  100.  my $bcc           = '';
  101.  
  102.  my %options=();
  103.  Getopt::Std::getopts("Ada:q:o:c:b:",\%options);
  104.  
  105.  $queue         = $options{q} if defined $options{q};
  106.  $owner         = $options{o} if defined $options{o};
  107.  $advDate       = ( $options{a} * $secInDay ) if defined $options{a};
  108.  $debug         = $options{d} if defined $options{d};
  109.  $sendToAdminCC =  $options{A} if defined $options{A};
  110.  $cc            = $options{c} if defined $options{c};
  111.  $bcc           = $options{b} if defined $options{b};
  112.  
  113.  
  114.  ################## Variables Init ##################
  115.  my $User = new RT::User($RT::SystemUser); # Define an RT User variable
  116.  my $date = new RT::Date($RT::SystemUser); # Define a date variable (used for comparisions)
  117.  my $tickets = new RT::Tickets($RT::SystemUser); # Used to store Ticket search results
  118.  my $now = new RT::Date($RT::SystemUser); # get current time
  119.  $now->SetToNow();
  120.  my $report; # Used for output
  121.  my $subject; # Used as subject line
  122.  
  123.  ################## Main Program ##################
  124.  # Limit the ticket search to new and open only.
  125.  $tickets->LimitStatus(VALUE => 'new');
  126.  $tickets->LimitStatus(VALUE => 'open');
  127.  
  128.  # Loop through new/open tickets
  129.  while (my $Ticket = $tickets->Next) {
  130.      # Construct POP-Up Message
  131.      $User->Load($Ticket->Owner);
  132.  
  133.      # Compare Dates to check whether the ticket's due date is in the past + Due date exists
  134.      $date->Set(Format => "ISO",Value => $Ticket->Due);
  135.      if ($now->Unix - $date->Unix < (1 - $advDate ) or $date->Unix == -1 or $date->Unix == 0) { next; }
  136.  
  137.      # Compare owner and queue if given. Skip current ticket if invalid.
  138.      if ($owner) { if ( lc($User->Name) ne lc($owner) ) { next; } }
  139.      if ($queue) { if ( lc($Ticket->QueueObj->Name) ne lc($queue) ) { next; } }
  140.  
  141.      # Generate a report
  142.      $report = "";
  143.      $report .= "Ticket #: " . $Ticket->id . "\n";
  144.      $report .= "Subject:  " . $Ticket->Subject . "\n";
  145.      $report .= "Queue:    " . $Ticket->QueueObj->Name . " (". $Ticket->QueueObj->AdminCcAddresses .") \n";
  146.      $report .= "Owner:    " . $User->Name ."\n";
  147.      $report .= "Due date: " . $date->ISO . "\n";
  148.      $report .= "URL:      " . $rt_url . "Ticket/Display.html?id=" . $Ticket->id . "\n";
  149.  
  150.      # Set the subject based on the due date.
  151.      if( ($now->Unix - $date->Unix < 0  ) or $date->Unix == -1 ) {
  152.          $subject =  "Ticket #". $Ticket->id . " with owner " . $User->Name ." is due on " . $date->ISO;
  153.      } else {
  154.          $subject =  "Ticket #". $Ticket->id . " with owner " . $User->Name ." is overdue";
  155.      }
  156.  
  157.      # Get Queue Admin CC
  158.      # Do we send to Admin CC as well as to owner?
  159.      my @emails = ();
  160.      if ( $sendToAdminCC ) {
  161.          @emails = ($User->EmailAddress, split(/,/, $Ticket->AdminCcAddresses), split(/,/ , $Ticket->QueueObj->AdminCcAddresses));
  162.      } else {
  163.          @emails = ($User->EmailAddress);
  164.      }
  165.  
  166.      # remove duplicates
  167.      my %temp = (); @emails = grep ++$temp{$_} < 2, @emails;
  168.      send_report(@emails);
  169.  }
  170.  
  171.  # Close RT Handle
  172.  $RT::Handle->Disconnect();
  173.  exit 0;
  174.  
  175.  # This procedure will send a report by mail to the owner
  176.  #  parameter 1 - email addresses to send to
  177.  # Global variables refered to:
  178.  #  $subject - Subject line
  179.  #  @report - Message content
  180.  #  $from_address - address to send from
  181.  #  $cc - CarbonCoby email address
  182.  #  $bcc - BlindCarbonCopy email address
  183.  sub send_report {
  184.      my @tos = @_;
  185.      my $addr;
  186.  
  187.      foreach $addr (@tos) {
  188.          next if (length($addr) == 0);
  189.          my $msg = "";
  190.          $msg .= "From: $from_address\n";
  191.          $msg .= "To: $addr\n";
  192.          $msg .= "Cc: $cc\n" if $cc;
  193.          $msg .= "Bcc: $bcc\n" if $bcc;
  194.          $msg .= "Subject: $subject\n";
  195.          $msg .= "\n";
  196.          $msg .= $report;
  197.  
  198.          if ($debug) {
  199.              print "====== Would call '$sendmail' with this input:\n";
  200.              print "$msg\n\n";
  201.          } else {
  202.              open(SENDMAIL, "|$sendmail") || die "Error sending mail: $!";
  203.              print SENDMAIL $msg;
  204.              close(SENDMAIL);
  205.          }
  206.      }
  207.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement