Jump to content

getting 500 internet server error while using php email


Goinfory

Recommended Posts

Hi, i want to send email to my 600 clients through php email. but the loop works till 20 contacts, after that it show 500 internal server error. I am using hostforlife server. Here is the script:

<?php
for($x=1;$x<=600;$x++)
{
$con = mysqli_connect("host","database","password");
if (!$con){die('Could not connect: ' . mysqli_error($con));}
mysqli_select_db($con,"database");
$sql="SELECT email FROM emails Where id = ".$x;
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
$to = $row['email'];
if ($to == "NULL")
{

}
else
{
  echo $row['email'];
mysqli_close($con);
// mail($to,"subject","message");
sleep(2);
}
}
?>

Please help me, Thanks! :)

You could try something like this:

<?php
//CONNECT TO DATABASE
$con = mysqli_connect("host","database","password");
if (!$con){die('Could not connect: ' . mysqli_error($con));}
mysqli_select_db($con,"database");
 
//GET EMAILS
$sql = "SELECT email 
        FROM emails 
        WHERE 
            id <= 600 AND
            email IS NOT NULL";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result)) {
    echo $row['email'];
}
 
//CLOSE DATABASE
mysqli_close($con);
?>
 
 
Note: if you're not doing so already, you'll want to look into storing you're database connection script in an external file and use something like require_once() to import it. That way when you want / need to change the database login credentials, you'll only need to change it once.

What is likely happening is, as was pointed out, you are trying to make 600 database connections.  On your host, this is limited and you are running out of resources and memory at 20, and then you get the 500 error.

 

cyberRobot's post should fix your problem and is the standard way to have code like this.  

 

You should also try and work in 2 parameters:

 

start=

count=

 

This will allow you to have this script be used to email any batch of your database.  This is the type of script you would want to run from a shell, but I'm guessing you don't have shell access to your host.

 

Taking Cyber's example and expanding on it:

 

<?php
// Get params
if (isset($_GET['start'])) {
  $start = (int)$_GET['start'];
} else {
  $start = 0;
}

if (isset($_GET['count'])) {
  $count = (int)$_GET['count'];
} else {
  //default to 600 rows
  $count = 600;
}
$count += $start;


//CONNECT TO DATABASE
$con = mysqli_connect("host","database","password");
if (!$con){die('Could not connect: ' . mysqli_error($con));}
mysqli_select_db($con,"database");
 
//GET EMAILS
$sql = "SELECT email 
        FROM emails 
        WHERE 
            id BETWEEN $start AND $count AND
            email IS NOT NULL";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result)) {
    echo $row['email'];
    // Send email here
}
 
//CLOSE DATABASE
mysqli_close($con);
Now you could call your script as:

 

email.php?start=100&count=50

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.