tags:

views:

132

answers:

6

Now i do send emails with this loop:

while($data = mysql_fetch_assoc($resultat)){
    ...
    sendMail();
}

function sendMail(){
    ...
    mail($to, $subject, $text, $headers);
}

But this is not so memory efficient what I can understand. The script should be able to send to over 1000 receivers.

Do you know how to do this in a better way? I want to keep it simple and do not wont to install any framework.

+7  A: 

Drop a "note" to a cron job. In other words, write a file that a cron job picks up and performs the lengthy operation outside of the PHP session.

Of course you can use PHP for the actual script that cron will run :-)

The reason I am suggesting this: you probably don't want to hold up your PHP session hanging for too long: delegating to an external process makes (probably) more sense.

Also note that each PHP session is very often limited in memory: delegating to an external process might relieve this strain.

jldupont
Hm, how do I do that? Have never heard of cron before.
Johan
cron is the Linux job scheduler.
jldupont
use the command : crontab -e
mnml
http://en.wikipedia.org/wiki/Crontab
mnml
+1  A: 

Similar question

mnml
A: 
$to='[email protected];[email protected];[email protected];...';
justastefan
this is a horrible way to do it, not only will the e-mail end up enormous in size with all of those addresses, you're exposing everyone's addresses too!
MalphasWats
I don't want all the email-addresses to be visible for everyone.
Johan
bcc? that'll strip off the addresses in the first MTA (mails won't be large, you won't display addresses, and the scheduling is done by your MTA). Only drawback is that the addressee won't see his email in the to:-field
roe
Missing the address in the To field will increase your chances of getting flagged as spam for many email providers.
MightyE
Sound not to bad. What's MTA?
Johan
Mail Transfer Agent
Kuroki Kaze
what??.. that answer stole all my points to do things like judging etc... ill go quit :-/
justastefan
+1  A: 

You say you don't want to install a framework, but I don't think PHP's mail is really built for sending out massive amounts of email quickly and efficiently. I would try Pear's Mail_Queue or Mail.

Chris Kloberdanz
Quickly is not important.
Johan
A: 

AJAX is probably the best for such operations.

With AJAX, you can call a small *.php-File containing just the function to send an email to the next user. That is called every bunch of seconds and you're done.

Of course, if you don't like client-scripting, such operations become quite difficult to manage with PHP.

ApoY2k
AJAX is not a magic potion, nor is it a solution for everything - you now have the exact same problem, distributed between the client and server, plus the overhead of thousands of requests (-> huge memory leaks in certain browsIErs). The fact that you shifted the loop to run in the browser doesn't mean the loop disappeared. Passing the buck != solving the problem.
Piskvor
+1  A: 

Look into integrating with list manager software such as Majordomo. It's easy to do an adequate job broadcasting emails. It's hard to do a great job, and these people have put a lot of blood sweat and tears into making this work well.

Just be mindful that your recipients are expecting the email, or you'll end up getting yourself put on realtime spam blacklists, and possibly have your host kick you off their network.

MightyE