CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
error in while loop

 



aneesh
New User

Apr 10, 2013, 12:05 AM

Post #1 of 5 (392 views)
error in while loop Can't Post

Hi All,

My perl script successfully fetches data (username and password) from a mysql database and sends them to corresponding email address. It works just fine if all the email addresses are in valid format. If the first email is not in valid format (like without @ symbol etc) the while loop terminates with errors. Here is the code :

while (@data = $sth->fetchrow_array()) {
$email_address = $data[0];
$id = $data[1];
$pass = $data[2];

# count the number of emails that we send
$CurrentLine++;

print "sending to $email_address\n";

my $msg = MIME::Lite->new (
Subject => $subject,
From => $from,
To => $email_address,
Data => $body,
Type => 'text/html',
);
$msg->send( 'smtp' , $smtpserver , AuthUser=> $smtpuser , AuthPass=> $smtppassword , Debug=>4 );

sleep 1;

}

Is there anyway I can ignore the error and continue with the while loop (mail sending) until the last entry reaches?

Thanks in advance.

Aneesh K


g4143
Novice

Apr 10, 2013, 1:53 AM

Post #2 of 5 (384 views)
Re: [aneesh] error in while loop [In reply to] Can't Post

Did you try eval'ing the section of code that's failing and checking the $@ value?


FishMonger
Veteran / Moderator

Apr 10, 2013, 6:15 AM

Post #3 of 5 (371 views)
Re: [aneesh] error in while loop [In reply to] Can't Post

Use the Email::Valid module to check for valid format.

http://search.cpan.org/~rjbs/Email-Valid-0.190/lib/Email/Valid.pm


Code
use Email::Valid; 

while ( my (email_address, $id, $pass) = $sth->fetchrow_array ) {

# count the number of emails that we send
$CurrentLine++;

print "sending to $email_address\n";

if ( Email::Valid->address($email_address) ) {
my $msg = MIME::Lite->new (
Subject => $subject,
From => $from,
To => $email_address,
Data => $body,
Type => 'text/html',
);
$msg->send( 'smtp' , $smtpserver , AuthUser=> $smtpuser , AuthPass=> $smtppassword , Debug=>4 );

sleep 1;
}
else {
# do something else
}
}



aneesh
New User

Apr 10, 2013, 6:28 AM

Post #4 of 5 (365 views)
Re: [g4143] error in while loop [In reply to] Can't Post

Thank you very much. I used the eval function and it seems to be working fine. Thanks again.


aneesh
New User

Apr 10, 2013, 11:09 PM

Post #5 of 5 (355 views)
Re: [FishMonger] error in while loop [In reply to] Can't Post

The Email::Valid module seems to be a nice idea. I will give that a try. Thanks :-)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives