Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

phpmailer sending twice in database loop

Status
Not open for further replies.

Muppsy007

Programmer
Apr 4, 2003
98
0
0
NZ
Hello,

I am using phpmailer in a script that gets partner details from a MySQL database, and loops through each one sending a personalised message.

At present, I am giving phpmailer my own address instead of the the database email value to make sure my code is alright.

I use a test database of four records, everything works fine. All sent, only once. The page is displayed with all the confirmations.

But if I change the table name to the live table, containing 129 partners, the page always times out, only half of the messages are sent, and all of these are sent twice.

Here is the code:
Code:
<?php

/***************************************************************************************************
/* This simply takes all the emails from the database and sends the hardcoded message to them
/***************************************************************************************************/

ini_set("include_path", ".:/path/to/phpmailer/");
require("class.phpmailer.php");

	// Connect to database
	$db = mysql_connect("localhost", "user", "pwd");
	mysql_select_db("data",$db);
	
	// Get the data
	$sql = "SELECT ID, Facility, Username, Password, Country, Email FROM table ORDER BY ID ASC";
	$RS = mysql_query($sql);
	
	$i = 1;
	while($Array = mysql_fetch_array($RS)) {
		// Only try to send emails to records that actually have one.
		if($Array['Email'] != "" | !is_null($Array['Email'])) {
			
			// This is where the message is sent
			$mail = new PHPMailer();
			
			$mail->IsSMTP();                                   // send via SMTP
			$mail->Host     = "smtp.host.com"; // SMTP servers
			$mail->SMTPAuth = true;     // turn on SMTP authentication
			$mail->Username = "username";  // SMTP username
			$mail->Password = "password"; // SMTP password
			
			// Set variables needed in the email
			$subject = "$i. Subject";
			$to_email = "me@mine.com";   // use my self to test all emails before going live
			$facility = $Array['Facility'];
			$username = $Array['Username'];
			$password = $Array['Password'];
			$country = $Array['Country'];
			
			
			// Now, create the html content
			$message = "hello";
			
			// Now, create the plain text content
			$plain_message = "Hello";
			
			$mail->From     = "me@mine.com";
			$mail->FromName = "ME ME ME";
			$mail->AddAddress($to_email); 
			$mail->AddReplyTo("me@mine.com","Information");
			
			$mail->WordWrap = 50;                              // set word wrap
			$mail->IsHTML(true);                               // send as HTML
			
			$mail->Subject  =  $subject;
			$mail->Body     =  $message;
			$mail->AltBody  =  $plain_message;
			
			if(!$mail->Send())
			{
			   echo "Message was not sent <p>";
			   echo "Mailer Error: " . $mail->ErrorInfo;
			   exit;
			}
			
			echo "$i. Message has been sent to $facility<br>";
			$i += 1;
			
			// Clear all addresses for next loop
 			$mail->ClearAddresses();
			
		}
	}
?>

I can only guess that this is a problem with efficiency of my code, as the smaller database works fine.

Has anyone got any ideas? I'm comfortable with php, but not knowledgable of speeding scripts up.

Side question: I've seen a few scripts that access databases and display results on the page on the fly (eg. one at a time as they are processed) My script is obvioulsy tring to display confirmation all at once. So how do these guys to that?

Thanks
Aaron
 
Forget that guys,

I did a bit of playing around after finding a totally random post about ob_start(); and ob_end_flush();.

Works like a bloody dream and I even answered my side question in the process. I love this stuff!

For anyone else have the above problem, here is my final code (additions highlighted)

Code:
<?php

/***************************************************************************************************
/* This simply takes all the emails from the database and sends the hardcoded message to them
/***************************************************************************************************/

ini_set("include_path", ".:/path/to/phpmailer/");
require("class.phpmailer.php");

	// Connect to database
	$db = mysql_connect("localhost", "user", "pwd");
	mysql_select_db("data",$db);
	
	// Get the data
	$sql = "SELECT ID, Facility, Username, Password, Country, Email FROM table ORDER BY ID ASC";
	$RS = mysql_query($sql);
	
	$i = 1;
    [b]$run = 0; // control variable to set a pause every 20 messages 
	ob_start();[/b]

	while($Array = mysql_fetch_array($RS)) {
		// Only try to send emails to records that actually have one.
		if($Array['Email'] != "" | !is_null($Array['Email'])) {
			
			// This is where the message is sent
			$mail = new PHPMailer();
			
			$mail->IsSMTP();                                   // send via SMTP
			$mail->Host     = "smtp.host.com"; // SMTP servers
			$mail->SMTPAuth = true;     // turn on SMTP authentication
			$mail->Username = "username";  // SMTP username
			$mail->Password = "password"; // SMTP password
			
			// Set variables needed in the email
			$subject = "$i. Subject";
			$to_email = "me@mine.com";   // use my self to test all emails before going live
			$facility = $Array['Facility'];
			$username = $Array['Username'];
			$password = $Array['Password'];
			$country = $Array['Country'];
			
			
			// Now, create the html content
			$message = "hello";
			
			// Now, create the plain text content
			$plain_message = "Hello";
			
			$mail->From     = "me@mine.com";
			$mail->FromName = "ME ME ME";
			$mail->AddAddress($to_email); 
			$mail->AddReplyTo("me@mine.com","Information");
			
			$mail->WordWrap = 50;                              // set word wrap
			$mail->IsHTML(true);                               // send as HTML
			
			$mail->Subject  =  $subject;
			$mail->Body     =  $message;
			$mail->AltBody  =  $plain_message;
			
			if(!$mail->Send())
			{
			   echo "Message was not sent <p>";
			   echo "Mailer Error: " . $mail->ErrorInfo;
			   exit;
			}
			
			$i += 1;
			$run += 1;
			
            [b]// This justs pauses the script every 20 emails to give the server time to breath.
			if ($run >= 20){
				sleep(4);
				$run = 0;
			}[/b]
			
			printf("$i. Message has been sent to $facility<br>");
			
			[b]ob_end_flush();[/b]

 			$mail->ClearAddresses();
			
		}
	}
?>

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top