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! :)

Link to comment
Share on other sites

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.
  • Like 1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.