-4

I am developing a mass-mailing system. At a time we send 2-4K emails, the email contacts are imported using PHPexcel library at same quantity of emails. Last night when we are sending 2k emails we get the "500 internal server" error.

I think I should develop the new process for email handling and the contacts importing, am I right? If so, how should I do it? Is there any other way to overcome such 500 errors?

The PHP script is called by web browser and browser loads it for 5-10 minutes and then 500 error occurs. I am using the PHPMailer library for sending the mail.

halfer
  • 19,824
  • 17
  • 99
  • 186
siddhesh
  • 598
  • 6
  • 19
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – Prophet Feb 25 '15 at 09:37
  • 1
    You haven't identified the root cause of the server error. Until you do anything you do is based on guesswork. Read the server error log. –  Feb 25 '15 at 09:39
  • I am just aking that should i make a another background process for mail sending script and for contact list importing script – siddhesh Feb 25 '15 at 09:39
  • A "500 internal server error" sounds like it comes from Apache, but to send emails on a background process you don't need a web process. Is this on cron (or what method are you using to run this in the background)? How are you calling the PHP script? Are you using `wget` or `curl`? (This question will likely close, due to insufficient detail, but I will vote to reopen if you edit it with more detail). – halfer Feb 25 '15 at 09:46
  • I have edited the question discription please check out once. – siddhesh Feb 25 '15 at 09:49
  • no i am not running the process on background I want to ask should i run the background process or not? It is done now normally on HTTP Request – siddhesh Feb 25 '15 at 09:55

1 Answers1

1

Calling a long-running PHP script from a web browser is not really the same as running PHP in the background. That will lock up an Apache thread, and is likely to be subject to whatever timeouts PHP has configured. My guess is the timeout is being hit before the send has completed.

It would be better to do this on a cron. Here are some general pointers:

  • Every ten minutes, select the next unsent set of email addresses from your data store, maybe 100 of them.
  • Send an email to each one, logging to a database what you have done
  • Pause for a few seconds. This is helpful as it makes it less likely your mail will be directed to spam bins
  • If your script has been running for more than five minutes, exit (it will do the next set of email addresses on the next cron call)
  • Otherwise, loop back to the start

That will be much more reliable. For bonus points, write a web page to show you what emails are sent and which are still waiting. Also, you may wish to use a third-party mailing system like MailChimp, to increase your delivery reliability. Make sure all of your recipients really have opted into receiving email from you.

I've suggested the script should batch in groups of 100, run for five minutes, be called every ten minutes, and pause for a few seconds after each send -- but these are just examples. If you don't mind sending more slowly (e.g. overnight) then you can change these figures to suit. Sending more slowly is generally more reliable, so do this if you can.

ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
halfer
  • 19,824
  • 17
  • 99
  • 186
  • sir please help me in this http://programmers.stackexchange.com/questions/289482/how-to-use-solid-principles-practically/289504#289504 if you have free time – siddhesh Jul 14 '15 at 09:29