hello all.

i have this simple script to send emails to my subscribers. i've broken the sending into 20 emails per batch to get around getting a timeout error in my browser. without bias from using PEAR in sending multiple emails, any comments to improve the code will be appreciated. thank you.

<?php 
include("user_functions.php"); 
include("connection.php");     // connects to MySQL database 
$allsent = false;            // flag to signal that message has been sent to all subscribers 
$subject = "email subject"; 
$message = "Content of the Email message."; 
$headers = "From: name@domain.com"; 

// send email 20 at a time 
$query = "SELECT * FROM email_database WHERE sent != 1 LIMIT 20"; 
$result = mysql_query($query, $conn); 
if (mysql_num_rows($result) > 0) { 
    while ($rows = mysql_fetch_assoc($result)) { 
        mail ($rows['email'], $subject, $message, $headers);  
        $query2 = "UPDATE email_database SET sent = 1"; 
        $result2 = mysql_query($query2, $conn); 
    } 
} else { 
    $allsent = true; 
} 
include("close_connection.php");    // close the connection 
for ($i=0; $i<=1000000; $i++) { 
//delay counter 
} 
if !$allsent { 
    redirect_to( self );     // user defined function (header function) 
} 
?>

Recommended Answers

All 8 Replies

Post your time out error here

This is what the php manual says:

It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.

commented: yes +4

By the way, is it correct that the subject and body are the same for every mail? If so, use BCC to send them all at the same time.

better to use BCC.
It may because of execution time.
Use below function for overcoming time limit.

set_time_limit(0);

By the way, is it correct that the subject and body are the same for every mail? If so, use BCC to send them all at the same time.

if i use BCC, wouldn't the emails be sent to the spam folder instead of all the recipient's inboxes?

picked up a lot from your all your posts. thank you for your time and effort.

Check your php.ini setting max_execution_time and other time_out setting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.