PHP, sendmail and transports - how to speed up sending mail

I just wrote a set of classes for mass mailing to process a huge number of letters and analyze their contents in accordance with the parameters passed. If I test email for 1000 random recipients and 1000 random senders from my database, until the point of the script hits the send () part (I commented on it for now), I get a performance of about 2 seconds and 20 MB of peak memory which is great.

However, if I uncomment the portion of the submission, the submission will take 30 seconds. This is unacceptable, and I would like to somehow speed it up. From the test it is clear that the delay is caused by nothing more than a call to $ mail-> send (), as if he expected him to return something before continuing the cycle and sending the next letter.

I am wondering: how to speed up the send () call? What can I do to make it faster? I tried using two sending methods:

  • Zend SMTP transit, connecting to the server directly and sending. It takes 30 seconds per 1000 letters.
  • Sendmail through Zend_Mail. By simply calling the send function Zend_Mail after preparing each letter. It takes 60 seconds.

Note that queue is definitely an option, and I have built it into my classes. All that is required is to activate cron, and it works like a charm. But I am interested to know about the actual dispatch and how to speed it up. So, the actual call to send ().

+6
source share
3 answers

I would save the mail in a directory and send them using the shell script (cron / daemon / ...):

Zend_Mail::setDefaultTransport( new Zend_Mail_Transport_File( array( 'path' => __DIR__, 'callback' => function() { do { $file = 'email-' . date('Ym-d_H-i-s') . '_' . mt_rand() . '.eml'; } while (file_exists($file)); return $file; }, ) ) ); 
+2
source

You will need to speed up the MTA on the server. I recommend Postfix and that you really read each parameter so that you know how to fine-tune it. For a commercial solution, I heard that PowerMTA is a good choice, but I have never tried it myself.

There is only so much performance that you can squeeze out of one machine, but a regular dedicated server should be able to deliver a pretty impressive amount of mail as soon as you configure it correctly. The biggest bottleneck in performance is usually the disk drives that hold the mail queue, so consider using SAS disks (10k or even 15k RPM) or SSDs.

+2
source

You can try to insert PHP pcntl-fork . That way, you can leave the sending in a different process when parsing the next message.

PLAN B

You can serialize and save the email object into the database queue and let another script send them in the background. This script can work in an infinite loop ( while true ) with some sleep at each iteration. You can even run multiple instances of this script, but make sure the two scripts do not start sending the same email address at the same time.

To make sure the script is still working, you can use monit on Unix machines. It can run the script if for some reason the old instance did not work.

0
source

Source: https://habr.com/ru/post/892555/


All Articles