mail() - runs way too slow

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
akreider
Forum Commoner
Posts: 46
Joined: Mon May 22, 2006 3:54 pm
Location: USA

mail() - runs way too slow

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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! :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
akreider
Forum Commoner
Posts: 46
Joined: Mon May 22, 2006 3:54 pm
Location: USA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Post Reply