LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2003, 04:38 PM   #1
meluser
Member
 
Registered: Mar 2003
Posts: 65

Rep: Reputation: 15
sending an email to a email address after a perl operation


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 = "melawaisi\@dundee.com";

# Recipient's "real" email address
my $MailTo = "cctv_alert\@hotmail.com";

$smtp->mail( $MailFrom );
$smtp->to( $MailTo );

# Start the mail
$smtp->data();

# Send the header
# This address will appear in the message
$smtp->datasend("To: cctv_alert\@hotmail.com\n");

# So will this one
$smtp->datasend("From: cctv_server\@hotmail.com\n");
$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) = @_;
die"Failed 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
print"size 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: $DBI::errstr\n";


my $sql = "SELECT * FROM imageinfo";
my $sth = $dbh->prepare($sql);

$sth->execute or die"Execute fails: $DBI::errstr\n";
$sth->finish;

$dbh->disconnect;
}
 
Old 04-04-2003, 04:44 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
I usually use sendmail but I think this should work - It is just your 2 scripts merged so if they worked properly seperate they should now
Code:
#!/usr/bin/perl

use strict;
use warnings;

use DBI;
use Date::Manip;

use Net::SMTP;

# Set this variable to your smtp server name
my $ServerName = "smtp.dundee.ac.uk";


=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) = @_;
die"Failed 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
print"size 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: $DBI::errstr\n";


my $sql = "SELECT * FROM imageinfo";
my $sth = $dbh->prepare($sql);

$sth->execute or die"Execute fails: $DBI::errstr\n";
$sth->finish;

$dbh->disconnect;

# 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 = "melawaisi\@dundee.com";

# Recipient's "real" email address
my $MailTo = "cctv_alert\@hotmail.com";
$smtp->mail( $MailFrom );
$smtp->to( $MailTo );

# Start the mail
$smtp->data();

# Send the header
# This address will appear in the message
$smtp->datasend("To: cctv_alert\@hotmail.com\n");

# So will this one
$smtp->datasend("From: cctv_server\@hotmail.com\n");
$smtp->datasend("Subject: image_alert\n");
$smtp->datasend("\n");

# Send the body.
$smtp->datasend("size is $size\nmodified is $mtime\nfilename is $file\n");

# Send the termination string
$smtp->dataend();

# Close the connection
$smtp->quit();

}
 
Old 04-04-2003, 04:58 PM   #3
meluser
Member
 
Registered: Mar 2003
Posts: 65

Original Poster
Rep: Reputation: 15
thanks for the help,

but do i not need to put the email notifier inside a loop?
 
Old 04-04-2003, 05:05 PM   #4
meluser
Member
 
Registered: Mar 2003
Posts: 65

Original Poster
Rep: Reputation: 15
this is what i get:

Global symbol "$smtp" requires explicit package name at 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 "$smtp" requires explicit package name at rename.pl line 162.
Global symbol "$smtp" requires explicit package name at rename.pl line 163.
Global symbol "$smtp" requires explicit package name at rename.pl line 164.
Global symbol "$smtp" requires explicit package name at rename.pl line 167.
Global symbol "$smtp" requires explicit package name at rename.pl line 170.
Global symbol "$smtp" requires explicit package name at rename.pl line 173.
Execution of rename.pl aborted due to compilation errors.
[root@cctv cgi-bin]#
 
Old 04-04-2003, 05:15 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
like I said I usualy use sendmail - it is much easier
Code:
# Open Sendmail
open(MAIL, "|/usr/lib/sendmail -t");
# Write to the sendmail program
print MAIL "To: whoever\@wherever.com\n";
print MAIL "From: whoever\@wherever.com\n";
print MAIL "Subject:Your Subject\n\n";
print MAIL "Your message here";
# Close the sendmail program
close(MAIL);
The path to sendmail may vary between distros.
 
Old 04-05-2003, 11:29 AM   #6
meluser
Member
 
Registered: Mar 2003
Posts: 65

Original Poster
Rep: Reputation: 15
Hi,

i tried to run this seperatly, and i got nothing:

[root@cctv cgi-bin]# perl retest.pl
[root@cctv cgi-bin]#


-----------------------------------------retest.pl
# Open Sendmail
open(MAIL, "|/usr/lib/sendmail -t");
# Write to the sendmail program
print MAIL "To: melawaisi\@dundee.ac.uk\n";
print MAIL "From: cctv_aler\@hotmail.com\n";
print MAIL "Subject:Your Subject\n\n";
print MAIL "Your message here";
# Close the sendmail program
close(MAIL);

Cheers
 
Old 04-05-2003, 11:45 AM   #7
meluser
Member
 
Registered: Mar 2003
Posts: 65

Original Poster
Rep: Reputation: 15
Hi,

i tried to run this seperatly, and i got nothing:

[root@cctv cgi-bin]# perl retest.pl
[root@cctv cgi-bin]#


-----------------------------------------retest.pl
# Open Sendmail
open(MAIL, "|/usr/lib/sendmail -t");
# Write to the sendmail program
print MAIL "To: melawaisi\@dundee.ac.uk\n";
print MAIL "From: cctv_aler\@hotmail.com\n";
print MAIL "Subject:Your Subject\n\n";
print MAIL "Your message here";
# Close the sendmail program
close(MAIL);

Cheers
 
Old 04-07-2003, 12:49 AM   #8
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
shouldn't you have to put two dots at the end to send this? like:

Code:
# Open Sendmail
open(MAIL, "|/usr/lib/sendmail -t");
# Write to the sendmail program
print MAIL "To: melawaisi\@dundee.ac.uk\n";
print MAIL "From: cctv_aler\@hotmail.com\n";
print MAIL "Subject:Your Subject\n\n";
print MAIL "Your message here";
print MAIL "..\n";
# Close the sendmail program
close(MAIL);
I didn't try it, but piping open apps is just like using them on the command line and sendmail takes two dots and a newline to send the mail, right? Or am I mistaken?
 
Old 04-07-2003, 12:51 AM   #9
lackluster
Member
 
Registered: Apr 2002
Location: D.C - USA
Distribution: slackware-current
Posts: 488

Rep: Reputation: 30
sorry, [from man 8 sendmail]:

Quote:
With no flags, sendmail reads its standard input up to an end-of-file or a line consisting only of a
single dot and sends a copy of the message found there to all of the addresses listed. It determines
the network(s) to use based on the syntax and contents of the addresses.
so it's actually one dot and a newline.
 
Old 04-07-2003, 01:26 PM   #10
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Note the "With no flags", "-t" causes sendmail to read the addresses from the header.

As for the problem - Is the path to sendmail right for your machine?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sending email cambie Linux - Software 1 11-20-2004 06:02 PM
Sending a form to an email address problems! cucolin@ Programming 1 06-23-2004 05:21 AM
Creating A Second Email Address For Email Account On Sendmail treedstang Linux - Software 1 04-27-2004 10:31 PM
Sending Email to an SMTP address from sendmail guilmetrp Linux - Newbie 6 03-01-2004 08:51 PM
Sending email to a smtp address through sendmail guilmetrp Linux - General 0 02-27-2004 08:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration