Sending Large Amounts of Emails

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dubs
Forum Commoner
Posts: 28
Joined: Tue Oct 12, 2004 4:55 pm

Sending Large Amounts of Emails

Post by dubs »

I'm using the following code to bulk email my members, however, there's just too many emails in my database...and it kills my server.

Is there any why of getting my script to process 50 emails at a time and then refresh or load the page again and process the next 50 unitil complete.

Code: Select all

<?php
$sqlemail = "SELECT email.emailaddress,email.emailid FROM email WHERE ((email.emailid BETWEEN 1001 and 1500))";
$rsemail = mysql_query($sqlemail,$link);

$headers = "From:Speed Detectors UK<$mail_from>\r\n";
$headers .= "Content-type: text/html\r\n"; 
$headers .="reply-to:info@speed-detectors.co.uk";
set_time_limit(1000);
while ( $row = mysql_fetch_array($rsemail)) { 	
	if (mail($row["emailaddress"], $mail_subject, $mail_body, $headers)){
	}
}

?>
8O
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Why not just make the To: field yourself and then make the BCC: field a list of the email addresses. This way you send out one email and let the mail servers distribute to the correct people.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

I always create an email stack where i add emails that i wish to send, and then send all the emails in the stack when i wish or when the server is not under load. Once you create the stack in a database table, just select them and limit the query to x number of rows. Then send these emails, remove them from the stack, and repeat until its empty.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

you can also open a socket and send emails in bulk that way

look on zend.com in the code gallery, i saw a script or 2 there that did this
dubs
Forum Commoner
Posts: 28
Joined: Tue Oct 12, 2004 4:55 pm

Post by dubs »

protokol wrote:Why not just make the To: field yourself and then make the BCC: field a list of the email addresses. This way you send out one email and let the mail servers distribute to the correct people.
I've tried to do this buy building a string of emails with ; as a separator but it errors on the mail function.

Code: Select all

<?php
$bccemail = "";
while ( $row = mysql_fetch_array($rsemail)) { 
	$bccemail = $bccemail . $row["emailaddress"] . ';';
}

$headers = "From:Speed Detectors UK<$mail_from>\r\n";
$headers .= "Content-type: text/html\r\n"; 
$headers .= "nBcc: $bccemail \r\n";
$headers .="reply-to:info@speed-detectors.co.uk";

mail($mail_to, $mail_subject, $mail_body, $headers);

?>
What am i doing wrong
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

You should think about putting mail() inside the while loop. Also, where are the $mail_to, $mail_subject and $mailbody variables coming from?.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Try something like:

Code: Select all

<?php
$headers = "From:Speed Detectors UK<$mail_from>\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= "nBcc: $bccemail \r\n";
$headers .="reply-to:info@speed-detectors.co.uk";
$mail_subject = "testing";
$mail_body = "Message here...";

while (true) {     
 $row = mysql_fetch_assoc($rsemail);
 if ($row == false) break;
 $mail_to = $row['emailaddress'];
 mail($mail_to, $mail_subject, $mail_body, $headers);
}
?>
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

dont you use a comma, not semicolon to seperate the emails?


and what is nBcc: ? looks like a typo

still, opening a socket is the most efficient way to send bulk emails.....
dubs
Forum Commoner
Posts: 28
Joined: Tue Oct 12, 2004 4:55 pm

Post by dubs »

Does anyone know how to send direct to the SMTP using sockets
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

dubs wrote:Does anyone know how to send direct to the SMTP using sockets
rehfeld wrote:you can also open a socket and send emails in bulk that way

look on zend.com in the code gallery, i saw a script or 2 there that did this
Post Reply