Jump to content

PHP script to send a lot of mail to user


Recommended Posts


I have over 10 000 users in my DB who want receive newsletter from me.

Until now, i use SELECT Email,Username...... from my table and i loop each row with a mail() function to send a mail to this user.


Someone told me that i should only select 100 users and send email to them, after 5-10 minutes select another 100 user and so on...


Is that a good idea? Why my first way wasn't good?


Thanks a lot!



Link to comment
Share on other sites

If your server is shared, maybe you have a send mail limit.


If your server is dedicated, you can send unlimited mails, but you must be sure that users who get the emails dont send your mail to spam. If many users, send your newsletter to a spam folder, maybe your dedicated server/ip blacklisted.


The guy who told you that you must send 100mails per 5 minutes, maybe has his script in a shared server.


Mail function is an easy way to send mails (usually used on contact forms). Try search on google for a tutorial how to send newsletters.


Maybe this tutorial help you!


Link to comment
Share on other sites

Good Thanks for all!

I appreciate.


I still have a question:

I know now how to create a cron job,

I understand that my script should select 100 rows and send one email per row in a loop

But what i don't understand is how to figure the script that will select another 100 rows, and sent email to each row and so on, and when all members receive my newsletter, how i would stop de cron job.... for now, i have over 10 000 emails to send weekly, i hope in a near future, sending over 100 000 or more weekly...


How i settup a cron job that will call my script every 10 minutes 'till all my newsletter was send...?


Thanks a lot!



Link to comment
Share on other sites

Maybe these "how to" on youtube help you.


1 - http://www.youtube.com/watch?v=NZ73k_KPEuo
2 - http://www.youtube.com/watch?v=tlqmbNtW_x8
3 - http://www.youtube.com/watch?v=g4LxF5sVgPE
4 - http://www.youtube.com/watch?v=hDKCb5o-SHg
5 - http://www.youtube.com/watch?v=zeT2RpGbiug
6 - http://www.youtube.com/watch?v=ZM2uxb8gfn4

Link to comment
Share on other sites

I just got my script working.. I'll post it here:


// Grab our config settings

// Grab the FreakMailer class

//set execution time limit to 5 minutes 

$safeMode = ( @ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1 ) ? TRUE : FALSE;
if ( $safeMode === FALSE ) {
  set_time_limit(300); // Sets maximum execution time to 5 minutes (300 seconds)
  // ini_set("max_execution_time", "300"); // this does the same as "set_time_limit(300)"

echo "max_execution_time " . ini_get('max_execution_time') . "<br>";

//db connection
$con = mysql_connect("xx","xx","xx");
if (!$con)
  die('Could not connect: ' . mysql_error());

mysql_select_db("xx", $con);

// Setup body
$textBody = "Dear {MEMBER_NAME},\n\nCheck out PHP Freaks: http://www.phpfreaks.com\n\nSincerely,\nAdmin";
$htmlBody = "Dear {MEMBER_NAME},<br /><br />Check out PHP Freaks: http://www.phpfreaks.com<br /><br />Sincerely,<br />Admin";

// instantiate the class
$mailer = new FreakMailer();

// Get the user's Email
$sql = mysql_query("SELECT displayname,email FROM engine4_users2")or die(mysql_error());

//lets reset the time limit of the server everytime an email is sent to bypass maximum
while (1==1) {
  set_time_limit(30); // sets (or resets) maximum  execution time to 30 seconds)
  // .... put code to process in here

  		while($row = mysql_fetch_object($sql))
    	// Send the emails in this loop.
    	$member_name = $row->displayname;

        $mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $htmlBody);
        $mailer->AltBody = str_replace('{MEMBER_NAME}', $member_name, $textBody);

    	echo "Mail sent to: " . $member_name . "<br />";

  usleep(1000000); // sleep for 1 million micro seconds - will not work with Windows servers / PHP4
  // sleep(1); // sleep for 1 seconds (use with Windows servers / PHP4
  if (1!=1) {



Just change the query, and follow the tutorial in my thread and you'll get it :)


If not let me know and I'll help you!

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.