Page 1 of 1

mail() - runs way too slow

Posted: Mon May 22, 2006 4:04 pm
by akreider
How can I optimize sending email so that it doesn't use so much CPU?

I am trying to send out 800 customized emails once a week, that are dynamically customized based on user location. I've been using the mail() function. Last time I did this my host (JaguarPC) shut-down my account because it was using too much CPU to send these emails. I'm on a typical cheap shared hosting plan.

To send email they are using exim with spamassassin.

The last time I executed the script, it took 18 minutes (!!!) to send 800 emails. I'm 99.5% sure that the problem is the mail function, as my database operates fast for everything else.

They're running PHP 4.4.2.

I think there are some solutions that exist for sending out the SAME email to 800 different people, but want to send DIFFERENT emails.

Aaron

Posted: Mon May 22, 2006 4:33 pm
by onion2k
Yeah .. mail() kinda sucks. It connects to your SMTP server, sends the email, and then disconnects again. If you're sending 800 emails that'll be 800 connections to build and tear down. Which isn't very quick. Look at D11's SwiftMail class in the Code Snippets folder .. perfect for a job like this. And award winning code nonetheless! :)

Posted: Mon May 22, 2006 6:13 pm
by Chris Corbyn
If you do download Swift and use it, be sure to check back in a couple of weeks for an updated version... it's more featureful (interface for existing features remains the same) yet lighter and faster, how's about that? :P

You can kinda sneak-preview the code as it changes (that should actually just work if you replace the existing Swift.php in the PHP5 version).

Posted: Tue May 23, 2006 2:23 am
by AKA Panama Jack
He is running PHP 4 and it looks like Swift is a PHP 5 only program so this will not help him at all.

Posted: Tue May 23, 2006 2:34 am
by akreider
Works on php 4. I haven't had any luck getting it to work though (see the other thread where the mailer is announced and discussed). I'd be grateful for suggestions on how to debug it/get it to work.

Posted: Tue May 23, 2006 2:58 am
by Chris Corbyn
akreider wrote:Works on php 4. I haven't had any luck getting it to work though (see the other thread where the mailer is announced and discussed). I'd be grateful for suggestions on how to debug it/get it to work.
Yeah there's a PHP4 version ;)

Can you post the code you've used (xxx'ing out the smtp server if it's not set up to block relaying).

To debug with swift there's two useful things you might want to dump... try a print_r() of $mailer->transactions to see exactly what was said between the SMTP server and the script, also print_r() $mailer->errors in case something went wrong before the SMTP server connection was ever established. The die loudly plugin that's in devlopment will make this more obvious in the new version at the option of the developer by thrwoing exceptions/errors to the browser.