views:

452

answers:

2

I have created a newsletter system and my question is: How should i write my code considering that i have to send that mail to hundreds of email addresses?

I've discussed with my host administrator and he told me that i should send my emails one by one but not more than 6 per minute.

Can i use the $Timeout property? If so, how?

Thanks.

A: 

Pear Mail will allow you to send email from PHP to allot of people.

http://pear.php.net/package/Mail/

Todd Moses
But PEAR Mail has many problems and incompatibilities with PHP 5.3
Paulocoghi
+3  A: 

If you have to send out the mails one by one (instead of using BCC), I'd use a database-queue to respect the limit of sending out only 6 mails per minute (no matter what solution you'll finally use to actually send the mails).

E.g. you'd have a database-table containing recipient, subject, mailbody, lastsenddate, timessent and status.

Save all mails, you will send out, to the database and then set up a cronjob that will run once a minute and check if there are still mails in the queue waiting to be send (e.g. status = "unsend"). Then you'd pick a maximum of 6 (or whatever your limit is) mails from the queue, send them out, set the status to "send" (and increase "timessent" and set "lastsenddate" to the actual time, if you like) and wait for the next cronjob until all mails are sent.

This way you have several advantages:

  1. you can respect your per-minute-limit
  2. you have all your mails in a database and can relate to them later (e.g. to find out how many mails - and which ones - you sent last Friday or to find out whether a certain address has been processed - and when and how many times - if someone claims he never received a mail / or too many)
  3. by tracking a mail-status you could implement a bounce-handler that'll e.g. set a mail-status to "bounced" if a mail returns, so you could start a resend of your mailing some time later to reach addresses that returned a "mailbox full"-message the first time
  4. by saving your mails to a database, you could even setup a "deferred mail service" by adding a database-field "starttime" and make your send-script respect this date, so you could already queue your Christmas-mails in spring :)
Select0r
this is a great post, all i hope is to find some cronjob references :D i never worked with it. thanks
kmunky