On the site there is a need to send users personal email notifications about some events. More than 3000 users have already signed up for some events.

The script works according to the crown, takes a task from the database, creates a letter template, then selects a list of recipients from the database (MySQL) and in the loop substitutes the MemberName, MemberMail template and sends it as banal mail (). Letters are small (<1 KB) without attachments. Further letters is engaged in exim.

The problem is that the distribution takes a very considerable time (namely the execution of the specified php-script). For example:
421 letters - 37.764 p.
858 letters - 95.971 seconds
1337 letters - 217.12 p.
1607 letters - 301.72 seconds

It is clear that you can redo the queue and send out 100 letters or there are 200, but the question is different - is the execution time of such a script normal? Can the script be optimized in principle?

The site runs on a Hezner server (Intel® Core ™ i7-2600, 16 GB DDR3).

Closed due to the fact that off-topic party Nick Volynkin ♦ Mar 23 '16 at 4:58 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nick Volynkin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • For starters, you can try to parallelize the list (on the local MTA). Spread out the packet of letters to be sent to a dozen tables of contents (the amount is determined experimentally) and run its sending process on each table of contents (make a fork). If it does not help, then you will have to program sending via ESMTP and do it in each process. And (first of all) you need to look at (examine) the Exim settings (I don’t understand it specifically) to understand why your speed drops from the number of letters (from your measurements - 11, 9, 6, 5 letters per second) - avp
  • did a mailing for an online toy site, - used direct access to port 25 via curl, 10k emails took about a minute and a half, serever parameters are three times worse - deivan_
  • deivan_ by the way, yes, you opened the so-called stream, but spent the traffic ... It is a pity that the php does not implement the streams ... - Denis Kotlyarov
  • Links from the answers: profyclub.ru/docs/297 php.su/articles/?cat=email&page=007 - Nick Volynkin ♦

3 answers 3

You can use the queue. You can see the report on this topic: For the first time in RuNet: the tale of 100M letters per day

  • It remains to repeat the comment at the end of your link Excellent report. From myself I will add, an extremely professional article on the postal system. I advise everyone to read. - avp
  • Many thanks, of course, for your article! Well, I think it would be better to use special services, such as Unisender - user19657
  • report about anything "manager ruddy .... on the walls" - zb '31

Hmm mail () I remember problems with it, it always creates a new connection and then it closes it, loads the server, try a socket, communicate with the email server via SMTP, here's an example http://i-leon.ru/smtp-php/ if, of course hosting will allow ...

    The mail () function on each call creates a new connection to the smtp server. So the speed with it can not be improved. As an option to use the solution described in this article . The option with sending a large number of letters for one connection is listed.

    • mail () can also send local mail, which adds a queue letter from stdin. - zb '