X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2Fbin%2Ffreeside-expiration-alerter;h=0bb61db4a9e920e0271a98865652f22c07b5440a;hb=a04f7d97cd3e57ee9061fba5f737d6d77c782c13;hp=c3dc37b31ef8e5e2c78c9e9c3c357c6954068146;hpb=a6aa711eb82626bfab39902a6c4d785f3f533ef4;p=freeside.git diff --git a/FS/bin/freeside-expiration-alerter b/FS/bin/freeside-expiration-alerter index c3dc37b31..0bb61db4a 100755 --- a/FS/bin/freeside-expiration-alerter +++ b/FS/bin/freeside-expiration-alerter @@ -5,12 +5,15 @@ use Date::Format; use Time::Local; use Text::Template; use Getopt::Std; +use Net::SMTP; +use Mail::Header; +use Mail::Internet; use FS::Conf; use FS::UID qw(adminsuidsetup); use FS::Record qw(qsearch); use FS::cust_main; -use vars qw($smtpmachine); +use vars qw($smtpmachine %agent_failure_body); #hush, perl! $FS::alerter::_template::first = ""; @@ -20,9 +23,8 @@ $FS::alerter::_template::payby = ""; $FS::alerter::_template::expdate = ""; # Set the mail program and other variables -my $mail_program = "/usr/sbin/sendmail -t -n"; -my $mail_sender = "billing\@mydomain.tld"; -my $default_mail_recipient = "postmaster"; +my $mail_sender = "billing\@mydomain.tld"; # or invoice_from if available +my $failure_recipient = "postmaster"; # or invoice_from if available my $warning_time = 30 * 24 * 60 * 60; my $urgent_time = 15 * 24 * 60 * 60; my $panic_time = 5 * 24 * 60 * 60; @@ -52,45 +54,27 @@ if (scalar(@customers) == 0) exit 1; } -# Open email pipe - -open (MAIL, "|$mail_program"); -print MAIL <config('alerter_template') - or die "cannot load config file alerter_template"; - -my $alerter = new Text::Template (TYPE => 'ARRAY', SOURCE => [ map "$_\n", @alerter_template ]) - or die "can't create new Text::Template object: Text::Template::ERROR"; -$alerter->compile() or die "can't compile template: Text::Template::ERROR"; - # Now I can start looping foreach my $customer (@customers) { + my $paydate = $customer->getfield('paydate'); + next if $paydate =~ /^\s*$/; #skip empty expiration dates + my $custnum = $customer->getfield('custnum'); my $first = $customer->getfield('first'); my $last = $customer->getfield('last'); my $company = $customer->getfield('company'); my $payby = $customer->getfield('payby'); my $payinfo = $customer->getfield('payinfo'); - my $paydate = $customer->getfield('paydate'); my $daytime = $customer->getfield('daytime'); my $night = $customer->getfield('night'); - + my ($payyear,$paymonth,$payday) = split (/-/,$paydate); my $expire_time = timelocal(0,0,0,$payday,--$paymonth,$payyear); #credit cards expire at the end of the month/year of their exp date - if ($payby eq 'CARD') { + if ($payby eq 'CARD' || $payby eq 'DCRD') { ($paymonth < 11) ? $paymonth++ : ($paymonth=0, $payyear++); $expire_time = timelocal(0,0,0,$payday,$paymonth,$payyear); $expire_time--; @@ -103,14 +87,31 @@ foreach my $customer (@customers) ($expire_time < $_date + $panic_time && $expire_time > $_date + $panic_time - $window_time)) { + # Prepare for sending email, now inside the customer loop so i can be agent + # virtualized + my $agentnum = $customer->agentnum; + + $mail_sender = $conf->config('invoice_from', $agentnum ) + if $conf->exists('invoice_from', $agentnum); + $failure_recipient = $conf->config('invoice_from', $agentnum) + if $conf->exists('invoice_from', $agentnum); + + $ENV{MAILADDRESS} = $mail_sender; + + my @alerter_template = $conf->config('alerter_template', $agentnum) + or die "cannot load config file alerter_template"; + + my $alerter = new Text::Template TYPE => 'ARRAY', + SOURCE => [ map "$_\n", @alerter_template ] + or die "can't create new Text::Template object: $Text::Template::ERROR"; + + $alerter->compile() or die "can't compile template: $Text::Template::ERROR"; my @packages = $customer->ncancelled_pkgs; if (scalar(@packages) != 0) { my @invoicing_list = $customer->invoicing_list; if ( grep { $_ ne 'POST' } @invoicing_list ) { - $ENV{SMTPHOSTS} = $smtpmachine; - $ENV{MAILADDRESS} = $mail_sender; my $header = new Mail::Header ( [ "From: $mail_sender", "To: ". join(', ', grep { $_ ne 'POST' } @invoicing_list ), @@ -122,7 +123,7 @@ foreach my $customer (@customers) $FS::alerter::_template::first = $first; $FS::alerter::_template::last = $last; $FS::alerter::_template::company = $company; - if ($payby eq 'CARD') { + if ($payby eq 'CARD' || $payby eq 'DCRD') { $FS::alerter::_template::payby = "credit card (" . substr($payinfo, 0, 2) . "xxxxxxxxxx" . substr($payinfo, -4) . ")"; @@ -133,28 +134,63 @@ foreach my $customer (@customers) } $FS::alerter::_template::expdate = $expire_time; + $FS::alerter::_template::company_name = + $conf->config('company_name', $agentnum); + $FS::alerter::_template::company_address = + join("\n", $conf->config('company_address', $agentnum) ). "\n"; + my $message = new Mail::Internet ( 'Header' => $header, 'Body' => [ $alerter->fill_in( PACKAGE => 'FS::alerter::_template' ) ], ); - $message->smtpsend or die "Can't send invoice email!: $!"; #die? warn? + $!=0; + $message->smtpsend( Host => $smtpmachine ) + or $message->smtpsend( Host => $smtpmachine, Debug => 1 ) + or die "Can't send expiration email: $!"; } elsif ( ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list ) { - printf(MAIL qq{%5d %-32.32s %4s %10s %12s %12s\n}, - $custnum, - $first . " " . $last . " " . $company, - $payby, - $paydate, - $daytime, - $night); + push @{$agent_failure_body{$customer->agentnum}}, + sprintf(qq{%5d %-32.32s %4s %10s %12s %12s}, + $custnum, + $first . " " . $last . " " . $company, + $payby, + $paydate, + $daytime, + $night + ); } } } } -# Now I need to close EMAIL -close MAIL || die "Could not close printer: $default_mail_recipient\n"; - +# Now I need to send failure EMAIL + +foreach my $agentnum ( keys %agent_failure_body ) { + + $mail_sender = $conf->config('invoice_from', $agentnum ) + if $conf->exists('invoice_from', $agentnum); + $failure_recipient = $conf->config('invoice_from', $agentnum) + if $conf->exists('invoice_from', $agentnum); + + $ENV{MAILADDRESS} = $mail_sender; + my $header = new Mail::Header ( [ + "From: Account Processor", + "To: $failure_recipient", + "Sender: $mail_sender", + "Reply-To: $mail_sender", + "Subject: Unnotified Billing Arrangement Expirations", + ] ); + + my $message = new Mail::Internet ( + 'Header' => $header, + 'Body' => [ @{$agent_failure_body{$agentnum}} ], + ); + $!=0; + $message->smtpsend( Host => $smtpmachine ) + or $message->smtpsend( Host => $smtpmachine, Debug => 1 ) + or die "can't send alerter failure email to $failure_recipient". + " via server $smtpmachine with SMTP: $!"; +} # subroutines sub untaint_argv { @@ -183,10 +219,6 @@ is about to expire. Usually run as a cron job. user: From the mapsecrets file - see config.html from the base documentation -=head1 VERSION - -$Id: freeside-expiration-alerter,v 1.1 2002-03-06 22:44:13 jeff Exp $ - =head1 BUGS Yes..... Use at your own risk. No guarantees or warrantees of any