JoanR
JoanR

Reputation: 128

Only send first 150 emails and get "Internal server error" when try to send more than 200 emails with phpmailer

My code get emails from Database and send it an email using a loop. If I send the emails in groups of 25 it's working properly, but when the table exceeds 200 or 300 emails I get this error on screen an only send the 150 first emails:

"Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request."

I check my Server Error Log and is empty, and I think is not a hosting smtp limitation problem.

There is something that I am doing wrong?

Edit: The error apears 30 seconds after load the web.

The server is in Safe Mode=Off

I use:

ini_set('max_execution_time', 300);

Also I add within the loop:

set_time_limit(3);

But the error still at 30 seconds.

Code:

require '../phpmailer/PHPMailerAutoload.php';
@MYSQL_CONNECT("xxx","xxx","xxx");
@mysql_select_db("xxx");
$query  = "SELECT * FROM users";
$result = @MYSQL_QUERY($query);
while ($row = mysql_fetch_array ($result)) {
    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->Host = 'smtp.xxx.com';
    $mail->SMTPAuth = true;
    $mail->SMTPKeepAlive = true;
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    $mail->Username = '[email protected]';
    $mail->Password = 'xxx';
    $mail->From = '[email protected]';
    $mail->FromName = '[email protected]';
    $mail->AddAddress($row["mail"]);
    $mail->Subject = 'xxx';
    $mail->Body = 'Hello World';
    $mail->IsHTML(true);
    $mail->CharSet = 'UTF-8';
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent to ".$row["mail"];
    }
}

A lot of thanks.

Upvotes: 0

Views: 742

Answers (2)

Kevin Seifert
Kevin Seifert

Reputation: 3572

Put a sleep in between sending batches of emails (you may be hitting a max send per second limit). Also put a try catch around each attempted send, and log the exception. Maybe you are hitting a bad email address.

Also the $mail constructor probably should be outside the loop, with exceptions enabled. Just create one object and reuse it.

Upvotes: 1

Synchro
Synchro

Reputation: 37730

You are throwing away a ton of performance in this code, and using excessive connections as a result. Try starting with the example mailing list code provided with PHPMailer. Your PHPMailer probably needs updating too.

You're using the deprecated mysql_* functions - use mysqli or PDO instead.

The error could be a simple time or memory limit - check your max_execution_time and memory_limit settings in php.ini - but you really need to look in your server logs to find out the cause. Failing that you could enable error output temporarily.

Upvotes: 1

Related Questions