Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a database and in my database I have about 300 emails inside but when I am sending I am having this error:
504 Gateway Time-out

The server didn't respond in time.


What should I do ?
Here is the code:

PHP
$result = $pdo->query("SELECT * FROM table WHERE week= ".date('W')." ") $count = 1;
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
	
	if (!filter_var($row['Email'], FILTER_VALIDATE_EMAIL) === false) 

	{
	
$Fr_Email		= $row['Email'];



$title= "xxx - xxx";
$tete.= "From:XXX <xxx@xxx.com>\n";
$tete.= "X-Priority: 1 \n";
$tete.= "MIME-Version: 1.0"."\n";
$tete.= "Content-Transfer-Encoding: 8bit \n";
$tete.= "Content-type: text/html; charset=utf-8"."\n";
$corps= "Body";

   mail($Fr_Email, $title, $corps, $tete);
   
 if ($count % 5 == 0) {sleep(5);} $count++;


  	}

}


What I have tried:

Is there any solution for this ?
Posted
Updated 5-Jan-17 22:02pm
v2

1 solution

Most likely it takes too much time to send all the message so you encounter timeout. Try either increasing the timeout or send the mails in smaller chunks, for example 50 mails per each call.

Have a look at PHP: set_time_limit - Manual[^]
 
Share this answer
 
v2
Comments
TatsuSheva 6-Jan-17 4:05am    
Yes, I have tried to send each 50 by using in the sql request "LIMIT" the first 50 went but when I do this "LIMIT 50,100" the error appears again...
Wendelius 6-Jan-17 4:11am    
As far as I can see, in the SQL statement you have no criteria that would prevent sending the mail again and you don't have any sorting. So when you try to limit, you may get the same records again and if the table contains a lot of rows, then the time may be consumed by the query itself.

Having that said this sounds like query optimization issue. Few things to check:
- ensure that you have index on column week
- ensure that you have a proper primary key
- sort based on the primary key
- in the WHERE clause, add a condition to eliminate mails that are already sent
- if needed, add a column that holds information if the mail is sent or not
- add the newly added column to an index along with the week
TatsuSheva 6-Jan-17 4:18am    
By doing "LIMIT 50,100" is it correct ? I have 300 records...
Wendelius 6-Jan-17 4:28am    
LIMIT clause will take the desired amount of rows but you should be ordering the rows first. Otherwise you may get the same rows twice or more

Also if you have 300 records for this week, what is the total amount of records in the table. That's what affects the speed. Because of that I wrote the suggestions in my previous comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900