cc has asked for the wisdom of the Perl Monks concerning the following question:
I'm getting 2 files: one INFO and one data file.#!/usr/bin/perl -w use strict; use warnings; use File::Copy; use Net::SFTP; use Mail::Sender; my $server = "myserver"; my $user = "user"; my %args = (ssh_args => []); $args{debug} = 1; $args{user} = "user"; # ftp directories my $remote_directory_1 = ""; my $recipient = "it\@mydomain.net"; my $linux = "root\@mydomain.net"; # write the log BEGIN { use CGI::Carp qw(carpout); my $errorlog = "/srv/ftp/data/logs/errorlog.txt"; open(LOG, ">$errorlog") or die("Unable to open $errorlog: $!\n"); print LOG "Errors:\n"; carpout(*LOG); } chdir "/srv/ftp/data/IN" or die "/srv/ftp/data/IN: $!\n"; -f "/srv/ftp/data/IN/INFO" or die "NO INFO NO TRANSFER !\n"; my @ftp_locations = $remote_directory_1; # open the file safely or complain it wouldn't open open(FILE, "<", "INFO") or die "Failed to open info file: $!"; # read all the lines in from INFO my @files; for(my $i = 1; $i <= @ftp_locations; $i++) { $_ = <FILE>; s/\W*$//; # remove trailing whitespace next if (!$_); # skip empty lines # check that we get our match. If not, # complain and move on. Want to see two # filenames. unless(/^([\w.-]+) \s+ ([\w.-]+)$/x) { print STDERR "$_ is not a valid line"; next; } # rename the files as per rename in file my ($old, $new) = ($1, $2); unless( -e $old ) { print STDERR "$old does not exist!\n"; next; } rename $old, $new; push @files, $new; } # check that we have the number of files that we expect unless(@files == @ftp_locations) { die "Not enough specified ftp_locations for ". "given number of files!"; } # create backup subfolder my @dt = localtime; my $subfolder_name = ((((1900 + $dt[5]) * 100 + 1 + $dt[4]) * 100 + $d +t[3]) * 100 + $dt[2]) * 100 + $dt[1]; mkdir "/srv/ftp/data/OUT/$subfolder_name" or die "$subfolder_name: $!" +; # FTP each file across, die on errors foreach my $new (@files) { my $destination = shift @ftp_locations; # sftp transfer my $sftp=Net::SFTP->new($server, %args) or die "could not open + connection to $server\n"; # change remote directory for the first file # $sftp->cd($destination); # $sftp->binary; $sftp->put($new, $new) or die "could not upload a file\n"; #quit SFTP When finished #$sftp->quit; # move files to backup directory unless(move("$new", "/srv/ftp/data/OUT/$subfolder_name")) { print STDERR "Oops! Couldn't move the file: $!"; } move "/srv/ftp/data/IN/INFO", "/srv/ftp/data/OUT/$subfolder_name"; move "/srv/ftp/data/logs/errorlog.txt", "/srv/ftp/data/OUT/$subfolder_ +name"; sleep (1 * 5) } close(FILE); # read the log file my $content; open(FILE2, "/srv/ftp/data/OUT/$subfolder_name/errorlog.txt") || die " +Cant open file. Reason: $!"; # (-: while(<FILE2>) { $content .=$_; # get all of the file into content including past new l +ines } close(FILE2); # send a mail when transfer completed my $mail = new Mail::Sender (smtp => 'localhost', from => $linux, to => $recipient, subject => 'data transfer', msg => $content); close(LOG); exit (0);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: problem with sending a file's content using Mail::Sender
by GrandFather (Saint) on Nov 01, 2007 at 02:26 UTC | |
by cc (Beadle) on Nov 01, 2007 at 14:48 UTC |