#!/usr/bin/perl -w # casma_archive.pl # script to email and archive CASMA registration files use MIME::Lite; $data_path = "/actapps/suitespot/cgi-bin/forms"; $archive_dir = "/actapps/suitespot/cgi-bin/forms/archive"; $from_email = "me\@someplace.org"; $to_email = "her\@someotherplace.org"; $file_date = &sys_date; #get the current date to append to file name $orig_file = "casma.csv"; $new_file = "casma_$file_date.csv"; $subject = "CASMA registrations flatfile"; archive_results($to_email, $orig_file, $new_file, $subject); sub archive_results { my($to_email, $old_file, $new_file, $subject) = @_; #name subroutine variables system("mv $data_path/$old_file $data_path/$new_file"); #rename the flatfile with date $msg = MIME::Lite->new(From => $from_email, To => $to_email, Subject => $subject, Type => 'multipart/mixed'); $msg->attach(Type => 'application/octet-stream', Path => "$data_path/$new_file", Filename => $new_file ); $msg->send(); system("mv $data_path/$new_file $archive_dir/$new_file"); #move flatfile to archive directory } sub sys_date{ my %mn = ('Jan','01', 'Feb','02', 'Mar','03', 'Apr','04', 'May','05', 'Jun','06', 'Jul','07', 'Aug','08', 'Sep','09', 'Oct','10', 'Nov','11', 'Dec','12' ); my $sydate=localtime(time); my ($day, $month, $num, $time, $year) = split(/\s+/,$sydate); my $zl=length($num); if ($zl == 1) { $num = "0$num";} my $retval="$mn{$month}$num$year"; return $retval; } #### $file_date = POSIX::strftime("%C%y%m%d", localtime); #get the current date to append to file name while (my ($filename, $recipient ) = each %csv_files) { my $newname = $file_date.$filename; archive_results($recipient, $filename, $newname, $subject); } exit 0; sub archive_results { my($to_email, $old_file, $new_file, $subject) = @_; #name subroutine variables if (-e $old_file) { #only if file exists system("mv $data_path/$old_file $data_path/$new_file"); #rename the flatfile with date $msg = MIME::Lite->new(From => $from_email, To => $to_email, Subject => $subject, Type => 'multipart/mixed'); $msg->attach(Type => 'application/octet-stream', Path => "$data_path/$new_file", Filename => $new_file ); $msg->send(); system("mv $data_path/$new_file $archive_path/$new_file"); #move flatfile to archive directory } } #end archive_results