views:

467

answers:

6

Note: It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient. Source: PHP manual

What are larger volumes? A 100 or a 1000?? Can I safely make it loop 200 times without much problems? (I can't install pear)

A: 

Why would you send large volumes of mails? Unless you are spamming/phishing? Every server these days has maximum allowed mails in a minute tweaked, so there is no way around it.

c0mrade
There are many legit reasons, mailing lists and such for customers.
TravisO
@TravisO if that is the case you can send only that much is allowed by server admin no way around it
c0mrade
You're assuming he's not in charge of his own server, or doesn't know the sysadmin, or that his hosting package has limits.
zombat
If you have a customer list where the customers have opted-in to receive communications, it's perfectly legit. Often, the hosting provider will have an approved method for sending such e-mail. Sometimes it's pretty miserable though (pairlist, majordomo, etc.) and you want something better.
Chris Thornton
@zombat if he was in charge of his server or his hosting had not limits then he wouldn't be asking this question now would he, if he knew sysadmin he'd ask him. But hey that is just me :D
c0mrade
@c0mrade - he's asking about what it says in the manual. The manual says "not suitable for larger volumes", and his question was "what exactly does 'larger volumes' mean". Resource limits weren't mentioned at all.
zombat
@c0mrade please read the question next time.
Adnan
+1  A: 

The smaller the batch the better, but it depends on your setup (server speed, network, etc). I would probably use a cron job and do small batches. You need to assume a mail() might hang and stop processing, which makes it important to mark each row in your list as email sent.

For example, if you can do 1 email per second or slightly faster, then I would do a batch of 50, in a cron job that runs every minute. Use your SQL query to get the TOP 50 results which haven't been sent yet, since you can't be sure where you are starting.

TravisO
+1 for the SQL query to avoid re-processing the same 50 over and over, each minute!
Chris Thornton
A: 

You need to first have a look at the your Terms Of Service (TOS) with your hosting or upstream provider. If you cause trouble for them, and you're in violation of the TOS, they'll drop you like a hot spoon.

Next, you may be able to avoid overwhelming the mail system and stay below the radar of any "reaper bots" by simply adding a sleep() call every 10 messages or so. Make it adjustable so that you can limit both the number of iterations and the sleep delay. Eitehr as parameters, or via a config file (the latter could be polled at the top of the loop, to make adjustments on-the-fly.

Chris Thornton
+2  A: 

You can loop it 200 times with few problems I would imagine, although it will be much slower than a custom mailer or a package set up properly to handle that.

The end result depends on many factors. The main thing you'll want to make sure of is that you use set_time_limit() to give the script enough time to do the work. Offloading the work into some kind of queue that's serviced by a cron script can make life easier on you as well, as keeping PHP scripts running for a long time will bring up other resource problems.

Back in the day, I used to send about 50,000 emails to a subscriber newsletter using PHP's mail function and a RedHat server with Exim installed. It would take 4-6 hours with the custom script I had running. There was nothing efficient about it, but it did the job.

zombat
+2  A: 

About 5-6 years ago (the last time I looked into this sort of thing), I saw mailing list software in PHP using the mail() function that was sending hundreds of messages everytime the "send to mailing list" feature was invoked. As the client added more and more names (into many thousands, last I checked) the system was getting rather slow. In the end, they bought some 3rd party software to handle large volume mailing and hosted on a server separate from their web server to avoid slowing down their web site.

As others have pointed out, you should clear this with your hosting provider before you start sending batches of more than a few dozen at a time - every hosting company will have their own policies, and if this violates the TOS, they can disconnect you/cut off your hosting. Ideally, large-volume mail transmission should be done from a server just for this purpose. That way, if it hangs or freezes, you won't have to worry about affecting other applications.

If you really are sending very very large amounts of mail, there are commercial packages out there that will also manage the mailing list, they will manage opt-outs and opt-ins, versions of emails, they will do text vs. HTML mail, etc... research some of them if you are serious.

I know this doesn't answer the main question of "alternatives to the mail() function?" but it's the best I can do - I haven't seen any! The only thing I can think of is manually managing the SMTP connections in PHP (not sure how possible that is) or using some external library to do it.

FrustratedWithFormsDesigner
A: 

If u want to send mails upto 1000 users then just pass of them in an array nd then put the mail() in loops

only one thing to remember just put set_time_limit(0) on first line and put flush() on last line nd u can send as many mails u want

pramod