Forum Moderators: coopster

Message Too Old, No Replies

Sending mail to users in mysql DB

How to send multiple emails at once

         

rjames

7:12 pm on Jul 12, 2005 (gmt 0)

10+ Year Member



Hi,

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.

coopster

10:27 am on Jul 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If they are all receiving the same message I would retrieve all the users, put their emails addresses in a comma-separated list, build a Bcc: header and send one email rather than loop and send individually. It depends on how many users are in that list, too. You may have to send it in blocks of users. Either way, much faster.

rjames

1:17 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



Great advice Coopster! Thanks for your suggestion. I was thinking about that possiblity but couldnt figure out how exactly I can do it.

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

Angelis

1:24 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



You could use the general PHP Mail script to send the mails. Like this...

<?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...

rjames

2:34 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



Thanks for your reply Angelis,

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.

Angelis

2:44 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



You can use BCC in the headers by using;

$headers .= 'Bcc: user@example.com' . "\r\n";

Im guessing you can add more users by splitting them with comma's but I have not tested it.

sned

5:32 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



I've been thinking about this type of thing lately as well. Right now, my code just makes a long Bcc list to send out (, separated).

Now, I'm wondering about aliases, so I can just have a single address to mail to.

-sned

rjames

5:53 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



I wonder if anyone can help us with the php code that can be used to split up the addresses into sets of 20 each and send emails using bcc?

appreciate it...

sned

6:53 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



Here's one way you could do it, (untested), but might give a general picture?

-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;
}
}

rjames

7:06 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



Wow, Thanks for taking the time out to write up that code sned!

I gotta give this a try. I am a novice with PHP and mysql, but I think i can handle it from here. Thanks much!

fromholland

7:20 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



Why don't you send all the mails at once with one mail() function and all the emailadresses in BCC?