sending an email to a email address after a perl operation

2003-04-04 Thread mel awaisi
Hi,

I have a script named renamer.pl that takes images from a directory and 
renames images and stores image meta information in a mysql. i also have a 
script that sends an email notification when excecuted to a desired email 
address.

is there away that i could combine the 2 scripts together, so when the meta 
information are inserted into MySQL, an email is sent to me.

Cheers

Mel

---emailtest.pl

#!/usr/local/bin/perl

use Net::SMTP;

print Content-type: text/plain, \n\n;

my $DEBUG = 1;

if($DEBUG)
{
$| = 1;
open(STDERR, STDOUT);
}
# Set this variable to your smtp server name
my $ServerName = smtp.dundee.ac.uk;
# Create a new SMTP object
$smtp = Net::SMTP-new($ServerName, Debug = 1);
# If you can't connect, don't proceed with the rest of the script
die Couldn't connect to server unless $smtp;
# Initiate the mail transaction
# Your real email address
my $MailFrom = [EMAIL PROTECTED];
# Recipient's real email address
my $MailTo = [EMAIL PROTECTED];
$smtp-mail( $MailFrom );
$smtp-to( $MailTo );
# Start the mail
$smtp-data();
# Send the header
# This address will appear in the message
$smtp-datasend(To: [EMAIL PROTECTED]);
# So will this one
$smtp-datasend(From: [EMAIL PROTECTED]);
$smtp-datasend(Subject: image_alert\n);
$smtp-datasend(\n);
# Send the body.
$smtp-datasend(This is to notify you of a new image being sent to the 
server!\n\n);

# Send the termination string
$smtp-dataend();
# Close the connection
$smtp-quit();


renamerr.pl
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Date::Manip;
=head1 NAME # renamer - renames files received by ftp, moving them to a new
directory
=head1 SYNOPSIS

nohup ./renamer image /home/httpd/htdocs /home/me/images jpg renamer.process

=head1 DESCRIPTION

#The above instructs renamer to look for files called image.jpg in
/home/httpd/htdocs.
#It checks once per minute for such a file to appear. If it sees a
#readable file called /home/httpd/htdocs.jpg it moves it
to/home/httpd/htdocs/image.200302251530.jpg
#where the number is a
#time stamp with year (four digits), month, day of the month, hour (in24
mode), and minute.
#Read the bugs section closely.
=head1 BUGS

#The original and new directories must be on the same file system.The
#program probably does not work on windows systems.
#The daemon behavior is weak.Not much testing has been done, so the script
may have other problems.
=cut

my $usage = EOUSAGE;
usage: $0 initial_name original_dir new_dir suffix lockfile
example: $0 pic /home/httpd/htdocs /home/me/images jpg
/home/me/renamer.process
EOUSAGE
my $check_file = shift or die $usage;
my $original_dir = shift or die $usage;
my $new_dir = shift or die $usage;
my $suffix = shift or die $usage;
my $lockfile = shift or die $usage;
##
# If you put it into the cron, comment out between the START and END BLOCK,
# and uncomment the section below it so you don't get multiple
# copies running. Also, comment out the
# lockfile bits above.
#START BLOCK
exit if (fork());
while (-e $lockfile) {
process($check_file) if (-r $original_dir/$check_file.$suffix);
sleep 30;
}
#END BLOCK
##
#
# process($check_file) if (-r $original_dir/$check_file.$suffix);
#
##
sub process {
my $file = shift;
my @st = (stat($original_dir/$file.$suffix));
my ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYear,
$IsDST) = localtime($st[10]);
$Year += 1900;
$Month++;
my $stamp = sprintf %4d_%02d_%02d_%02d_%02d_%02d, $Year, $Month, $Day,
$Hour, $Minute, $Second;
print renaming $original_dir/$file.$suffix to
$new_dir/$stamp.$suffix\n;
rename $original_dir/$file.$suffix, $new_dir/$stamp.$suffix or warn
couldn't rename file: $! $file to $new_dir/$file.$stamp.$suffix\n;
print adding $new_dir/$stamp.$suffix to database\n;
my $single_string = $new_dir . '/' . $stamp . '.' . $suffix;
infoinsert ($single_string);
}





#
# Connect to Database Named cctvimages on the localhost with the root user
# $dbh=DBI-connect(DBI:mysql;$database, $user, $password);
# and insert info about the file given as the argument $_[0];

#


sub infoinsert
{
my ($file) = @_;
dieFailed to get the info\n\$file is: $file if not defined $file;
my $dbh = DBI-connect(DBI:mysql:dbname=cctvimages;host=localhost,root,
, {'RaiseError' = 1});
my $size;
my $mtime;
my $secs;
($size, $secs) = (stat ($file))[7,9];
$mtime = ParseDateString(epoch $secs);
# even after conversion ':' is used to seperate hh and mn and ss
$mtime =~ s/://g;
# the above swaps out the ':' for nothing
$file =~ s/\/home\/me\/images\///;
# the above strips path
printsize is $size\nmodified is $mtime\nfilename is $file\n;
my $rows_affected = $dbh-do(INSERT INTO imageinfo VALUES(null, '$file',
'$size', '$mtime'))
or die Do Fails: 

RE: sending an email to a email address after a perl operation

2003-04-04 Thread Dan Muey
 Hi,

Howdy again, still working on this huh?

 
 I have a script named renamer.pl that takes images from a 
 directory and 
 renames images and stores image meta information in a mysql. 
 i also have a 
 script that sends an email notification when excecuted to a 
 desired email 
 address.
 
 is there away that i could combine the 2 scripts together, so 
 when the meta 
 information are inserted into MySQL, an email is sent to me.

Sure.

#!/usr/bin/perl

...code that inserts into mysql ...

...code that sends mail ...


Ta Da! One script

DMuey

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: sending an email to a email address after a perl operation

2003-04-04 Thread Dan Muey

 Hi Dan

Hey, reply to the list not just me.

 
 yeh still here, back from a small break. just want to finish it!!.
 
 i tried this and this is what i got:
 
 Global symbol $smtp requires explicit package name at 

Make 
$smtp = ...

my $smtp = ...

There was a thread earlier today about namespaces. Check out the archive it was quite 
informative.

 rename.pl line 140. Global symbol $smtp requires explicit 
 package name at rename.pl line 143. Global symbol $smtp 
 requires explicit package name at rename.pl line 151. Global 
 symbol $smtp requires explicit package name at rename.pl 
 line 152. Global symbol $smtp requires explicit package 
 name at rename.pl line 155. Global symbol $smtp requires 
 explicit package name at rename.pl line 159. Global symbol 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: sending an email to a email address after a perl operation

2003-04-04 Thread mel awaisi
thanks Dan,

sorry for forgetting to send to list.

the email script works alone, as it is, so maybe it is something else.

Regards,

Mel





From: Dan Muey [EMAIL PROTECTED]
To: mel awaisi [EMAIL PROTECTED]
CC: [EMAIL PROTECTED]
Subject: RE: sending an email to a email address after a perl operation
Date: Fri, 4 Apr 2003 17:10:10 -0600
 Hi Dan

Hey, reply to the list not just me.


 yeh still here, back from a small break. just want to finish it!!.

 i tried this and this is what i got:

 Global symbol $smtp requires explicit package name at
Make
$smtp = ...
my $smtp = ...

There was a thread earlier today about namespaces. Check out the archive it 
was quite informative.

 rename.pl line 140. Global symbol $smtp requires explicit
 package name at rename.pl line 143. Global symbol $smtp
 requires explicit package name at rename.pl line 151. Global
 symbol $smtp requires explicit package name at rename.pl
 line 152. Global symbol $smtp requires explicit package
 name at rename.pl line 155. Global symbol $smtp requires
 explicit package name at rename.pl line 159. Global symbol
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


_
Surf together with new Shared Browsing 
http://join.msn.com/?page=features/browsepgmarket=en-gbXAPID=74DI=1059

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: sending an email to a email address after a perl operation

2003-04-04 Thread R. Joseph Newton
mel awaisi wrote:

 There was a thread earlier today about namespaces. Check out the archive it
 was quite informative.
 
   rename.pl line 140. Global symbol $smtp requires explicit
   package name at rename.pl line 143. Global symbol $smtp
   requires explicit package name at rename.pl line 151. Global
   symbol $smtp requires explicit package name at rename.pl
   line 152. Global symbol $smtp requires explicit package
   name at rename.pl line 155. Global symbol $smtp requires
   explicit package name at rename.pl line 159. Global symbol
 

Hi Mel,

Since your two scripts were developoed separately, you might want to add
use strict;
to emailtest.pl, and work on that until it compiles cleanly.  These errors
indicate that you are not declaring your variables properly.  Once that script
compiles cleanly, you could integrate the code from renamerr.pl into it.

I would say that you are also pushing the limits of what you can sensibly do
with scripting.  If you wish to be able to develop complicated processes, you
should really learn how to write subroutines and functions.

perldoc perlsub
perldoc perlref
perldoc perlreftut

Hint: If the code in renamer was properly packaged within functions, you would
not have to cut and paste code.  You could use renamer as a module, and call
functions from it in other scripts.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]