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
mail() - runs way too slow
Moderator: General Moderators
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! 
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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? 
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).
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).
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Yeah there's a PHP4 versionakreider 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.
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.