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:
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
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