On 03/24/2005 05:31 PM, Gav.... said:
Can someone point me as to what is wrong with this :-
------------------------------------------------------------------------------------------------------------ #!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use DBI; $|=1;
open(MESSAGE, "/home/site/public_html/cgi-bin/email.txt") or die "cannot open message: $!";
flock(MESSAGE, 2) or die "cannot lock message exclusively: $!";
my @msgfile = <MESSAGE>;
close (MESSAGE) or die "cannot close message file: $!";
I don't think you need to lock the file to read it, especially since you are closing it so quickly. It might make some sense if the file is updated frequently by other processes.
my ($dbh, $sth, @row); $dbh = open_dbi(); my $sth = $dbh->prepare("SELECT email_address FROM users"); $sth->execute(); while(@row = $sth->fetchrow_array){
Since you aren't checking for errors in any of the three lines above, you haven't a clue whether an error occured or not.
I would either set RaiseError or do something like this:
my $sth = $dbh->prepare("SELECT email_address FROM users") or die "prepare() failed, $DBI::errstr\n"; $sth->execute(); or die "execute() failed, $DBI::errstr\n"; while(@row = $sth->fetchrow_array){ ... } die "fetchrow_array() failed, $DBI::errstr\n" if $DBI::err;
$address = $row[0]; chomp($address);
The chomp() shouldn't be needed for values stored in the database.
open (SENDMAIL, "|/usr/sbin/sendmail -t") or die "cannot open sendmail: $!"; print SENDMAIL "To:$address\n"; print SENDMAIL "From: [EMAIL PROTECTED]"; print SENDMAIL "Subject: ITgazette $address\n\n"; print SENDMAIL @msgfile; }
sub open_dbi { my $host = 'localhost'; my $db = 'databasename'; my $db_user = 'username'; my $db_password = 'password'; my $dbh = DBI->connect("dbi:mysql:$db:$host", "$db_user", "$db_password", {RaiseError => 0, PrintError => 0} ) or err_trap("Cannot connect to the database");
If you are going to disable error reporting when you connect, you need to explicitly check for errors at nearly every DBI method call. You would probably be better off with {RaiseError => 1}.
http://search.cpan.org/~timb/DBI/DBI.pm#Simple_Examples
I don't find your DSN syntax in the DBD::mysql manual, but since this is the one place where you're checking for errors, I guess it is working.
http://search.cpan.org/~rudy/DBD-mysql/lib/DBD/mysql.pm
return $dbh; }# end: open_dbi
sub err_trap { my $error_message = shift(@_); die "$error_message\n ERROR: $DBI::err ($DBI::errstr)\n"; }# end: err_trap
$sth->finish; $dbh->disconnect();
print "Location: http://sitename/index.php\n\n";
-----------------------------------------------------------------------------------------------------------------------------------------------------------
It does not produce an error message, it just does not send the emails out,but it does redirect to the location.
You are getting an error somewhere, but you aren't checking for errors except in the connect().
-- Mac :}) ** I usually forward private questions to the appropriate mail list. ** Ask Smarter: http://www.catb.org/~esr/faqs/smart-questions.html Give a hobbit a fish and he eats fish for a day. Give a hobbit a ring and he eats fish for an age.