Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I just wrote a set of bulk-emailing classes for handling enormous amounts of emails and parsing their content according to passed params. If I test an email on 1000 random recipients and 1000 random senders from my database, up until the point the script hits the send() part (I commented it for now), I get performance of around 2 seconds, and 20 MB peak memory, which is great.

However, if I uncomment the send part, the sending takes 30 seconds. This is unacceptable, and I would like to speed it up somehow. It is obvious from testing that the delay is caused by none other than the $mail->send() call, as if it's waiting for it to return something before continuing the loop and sending the next email.

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

  1. Zend SMTP transport, connecting to the server directly and just sending. This takes 30 seconds per 1000 emails.
  2. Sendmail via Zend_Mail. Simply calling Zend_Mail's send function after preparing each email. This takes 60 seconds.

Please note that queueing is definitely an option, and I have built it into my classes. All it takes is activating a cron and it works like a charm. But I'm wondering about the actual sending and how to speed that up. So, the actual send() call.

like image 911
Swader Avatar asked Jul 12 '11 07:07

Swader


2 Answers

I'd save the mails in a directory and send them using a shell script (cron/daemon/...):

Zend_Mail::setDefaultTransport(
    new Zend_Mail_Transport_File(
        array(
            'path' => __DIR__,
            'callback' => function() {
                do {
                    $file = 'email-' . date('Y-m-d_H-i-s') . '_' . mt_rand() . '.eml';
                } while (file_exists($file));
                return $file;
            },
        )
    )
);
like image 78
Tomáš Fejfar Avatar answered Sep 20 '22 10:09

Tomáš Fejfar


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

There's only so much performance you can squeeze out of one machine, but a regular dedicated server should be able to deliver a rather impressive amount of mail once you've configured it correctly. The biggest performance bottleneck is usually the disk drives where the mail queue is stored, so consider using SAS (10k or even 15k RPM) or SSD drives.

like image 38
Emil Vikström Avatar answered Sep 22 '22 10:09

Emil Vikström