views:

508

answers:

10

Lets say, I have 1000 subscribed users in mysql database. If I want to send an email to all users, what should i do?

EDIT:

What is (everybody talking) cron jobs? Can give example?

+4  A: 

I'd suggest creating a database table with all the emails that need to be sent. This can be done fairly rapidly. From there, set up a cron job of sorts that takes the top row, emails it, and deletes the row. This way you can parallelize the chore and the user doesn't have to wait for the emails to be sent before their response page loads.

Stefan Mai
A: 

You can begin by setting a query to get each email for each user, something along the lines of:

SELECT email FROM users;

Once you have that stored in a variable you can use a combination of foreach() and mail() to send an email to each user.

Jim Walker
+1  A: 

The best thing to is write a cron job in the language of your choice that executes the task when the server is usually at its calmest. If you want to use PHP, still use a cron job. Don't do it while responding to a page request WTF-style.

Matthew Flaschen
A: 

Hi,

i would create a table with all users and emails and an status. Setup 2 or 3 Cronjobs to process this table.

In your php script fetch 30 entrys from the db (limit 30) and process them with an foreach loop. After each run change the status of the "email" or delete the entry from the db.

With this way you can be sure that your server is able to handle the system load.

ArneRie
+2  A: 

Use a combination of a for loop and the mail function such as:

$rs = mysql_query('SELECT email from emailList LIMIT 0, 10');

while(list($email) = mysql_fetch_row($rs))
{
  // Send Email
  mail($email, 'Your Subject', 'Your Message');
}

But as everyone says donot run it all in one go. I seriously doubt any server would handle the load of making 1000 email requests. Run a cron job that runs such that your code upon each run retrieves lets say 30 - 50 emails and sends them off.

Ali
A: 

I would suggest doing what everyone else has said, create a seperate database table containing the message, subject, and e-mail address. I would also use PHPMailer (http://phpmailer.codeworxtech.com/) to send the e-mails through SMTP.

James Simpson
A: 

detach the mail sending code from the creation code (as stefan mai suggested). write all the addresses/subjects/bodies into a table, and process the table later.

then, run the cron-job only every x minutes and only send y mails. if you send too many mails at once, you might get flagged as spammer faster than you think. remove the mailing-task from the table only if sending succeeded.

  • do this if you got to send bulk mail (e.g. newsletters)
  • don't do this if the mail should be sent out instantly (like registration confirmation) - users hate to wait

use a library like phpmailer or swiftmailer.

Schnalle
A: 

Should every email be unique to each user? If not, do not send a new mail for each user, add a BCC (Blind carbon copy) to a single mail. This will make your smtp server happy.

alexn
+1  A: 

You could also try using PHP List.

Install PHP List, export your DB of email addresses and import it into PHP List as a CSV. Then you don't have to worry about about managing user's, etc and you get click tracking.

Paul Sheldrake
A: 

Write a PHP script that handles the send mail the way you'd like (Stefan Mai's answer is a great way to do this). Design the script so that it logs it's work, and doesn't print anything when it's running (this leaves you a way to find errors later). Assuming you're running Linux you can setup cron to run the script at the appointed time.

Edit your cron jobs by typing crontab -e into a terminal session. You then enter something like this (this will run the job every night at midnight).

 0 * * * * wget -O - -q -t 1 http://www.example.com/mymailer.php
acrosman