Forum Moderators: coopster
I have a php script setup to fetch records from a mysql database and send a message to all the users returned using sendmail() command.
This process does it by looping through each individual record returned by the query and takes about 2 minutes to finish on my server.
Does anyone know of a faster method to do this? I am in very urgent need of a solution. Thanks to anyone who can help.
Yes I'm sure this will be a lot faster. Instead of sending 200 emails individually, I can perhapse send only 10 with 20 recepients in each. Now I need to find someone who can help me modify my code :(
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
You will need to include it in a loop in your code somewhere but it should be able to send as many mails as you can pull from the database...
I actually have that code and as I stated before, it loops through each record and sends emails individually. here's what it looks like:
--------------------------------------------------
<?php
require_once('../Connections/authenticate.php');
$headers = 'From: me@domain.com' . "\r\n" .
'Reply-To: me@domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mysql_select_db($database_authenticate, $authenticate);
$result = mysql_query("SELECT email, mobile FROM dbusers WHERE sub = $sub");
if(mysql_num_rows($result) > 0)
{
$count = 0;
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC))
{
$to = $row['email'] . ', ';
$to .= $row['mobile'];
mail($to, $subject, $message, $headers);
$count++;
}
echo "myResult=$count Emails Sent. Done.";
}
else
{
echo "myResult=Email Submissions Failed.";
}
?>
-----------------------------------------------
As I mentioned before, currently the script is set to retrieve records one-by-one and email a message to each member individually. This process takes about 2 minutes to process appx 150 members. And it is crucial for us to reduce that time frame by a fraction.
I was wondering if sending emails in sets of, say 20 addresses each (in BCC field) would help? If so, can anyone please help me rewrite the code to achieve this? Thanks very much in advance.
-sned
$result = mysql_query("SELECT email, mobile FROM dbusers WHERE sub = $sub");
$counter = 0;
$to = '';
$bcc = '';
while($row = mysql_fetch_object($result)){
if($counter == 0){
$to = $row->email;
}
else{
$bcc .= $row->email;
// add a comma if we're less than 20
if($counter < 20){
$bcc .= ', ';
}
}
$counter++;
// if the counter has reached 20, send the mail and reset the variables
if($counter % 20 == 0){
mail($to, $subject, $message, "From: $from\r\nBcc: $bcc");
$to = '';
$bcc = '';
$counter = 0;
}
}