开发者

Sending +-200 emails using php mail() function in a loop

Note: It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This fun开发者_运维技巧ction 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)


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.


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.


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.


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.


If you want to send mails up to 1000 users then just pass them in an array and then put the mail() in a loop.

The only thing to remember is to just put set_time_limit(0) on the first line and put flush() on the last line, and you can send as many mails as you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜