Link to home
Start Free TrialLog in
Avatar of SniperCode Sheva
SniperCode Sheva

asked on

How to send multiple emails at the same time in PHP

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:
$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++;


  	}

}

Open in new window

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

why do you put a sleep(5); in your while loop?

on a side note, are you considering a mail client to do the task instead? perhaps you need a more stable/reliable solution?

I'm using GroupMail to send out newsletters years ago.

http://group-mail.com/
Avatar of SniperCode Sheva
SniperCode Sheva

ASKER

the sleep(5) will do a break of 5 seconds when each 5 emails are sent
I don't want to pay to do this task... if I can do it myself why not
php scripts are not multi-threading by nature, we gonna find another workable solution for it.

back to your original error of 504 Gateway Time-out ... The server didn't respond in time., , i guess your mail server is too busy to process your requests?

you may also try raising the max_execution_time setting in php.ini file, not too sure if that help
Okay, I am waiting then, I tried to use LIMIT in the SQL Statement it works when I put this : "LIMIT 50" but when I put "LIMIT 50,100" the error appears again...
LIMIT 50 will only select 50 records from your SQL output, hence 50 emails seem working fine for you.

LIMIT 50, 100 is more for pagination in which it select records from 50 to 100 from your SQL output.
So what do you suggest ?
I don't want to pay to do this task... if I can do it myself why not
So you don't end up trying to solve problems like this.

Sending email from a server is not a simple task - the code may be simple but there are a host of potential hurdles that get in the way that make doing it yourself a pain.

Due to the SPAM issue on the net most ISP's implement all sorts of restrictions on SMTP sessions to prevent the proliferation of SPAM. When you pay for an email service that all gets done behind the scenes and in addition you get a host of other functionality that would take a lot of effort to do yourself.

If you are dead set on doing it yourself - the first thing to do is to contact your ISP to find out what their restrictions are on sending mail - do you need to connect to a specific SMTP server - authentication / port information. If they offer a SMTP service for servers that requires SMTP authentication and or a different port then using the PHP mail function is not going to cut it - you would need to use a package like PHPMailer that provides functionality for SMTP mail.
I agree with Julian.  Email is one of those things that sounds like it should be easy.  And so we start doing it ourselves, then run into a veritable hornet's nest of problems.  Some of the problems are mitigated by PHPMailer, but there is a learning curve and it's only a server-side solution.  Once the email leaves the server, you still have to contend with SPF records, anti-spam software, unruly client email reader programs, blacklists, potential criminal investigations if someone reports your email to the authorities, etc.  The list of things that can go wrong is both wide and deep.  And nothing you will do about email gets a "thank-you," because everyone believes that it ought to "just work."  So when it doesn't, you are the goat!

Fortunately there is a highly professional (publicly traded) corporation that runs ConstantContact.com and they can take on all of these burdens.  Their service is almost embarrassingly inexpensive, and loaded with marketing goodies.  There are competitors (SalesForce, MailChimp, etc) but in my experience ConstantContact was easy to integrate into my apps and easy for my staff to use.  They offer a free trial.  When you truly understand how hard it can be to get your own email right, you will be in awe of their services and skills.
https://www.constantcontact.com/index.jsp
One option (maybe not the better one) is refresh the page, then every time that the page is loaded a email will be sent and I wont have Time-out problems.
I resolved the problem like this...
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial