is this possible to do?

if ($sent_mail <= 200 ($limit))
{
// construct mailing list array
$merc_list = explode(",",$merc_mailingList);

// deploy the emails
for($i=0; $i<count($merc_list); $i++){

// email body inside here

} // END for
$sent_mail = $sent_mail + $i;
} // END if condition
	
else{
include("limit.php");
}

Dani AI

Generated

A few practical corrections and a safer pattern to enforce a 200/day limit (addresses the bugs in ’s posts and builds on ideas from and ).

Common pitfalls seen above

  • Adding the loop index to the counter ($send_mail = $send_mail + $i) is wrong — increment by 1 only when a message is actually sent.
  • Calling count() on every loop iteration wastes CPU; compute it once.
  • Watch for assignment vs comparison (send = true vs $send === true) and missing semicolons.
  • Don’t assume mail() means delivery — it only reports whether the message was handed to the MTA.

Recommended approach (safe, clear, and concurrency-aware)

  • Read the persistent sent count (from DB) and compute remaining quota: $remaining = max(0, $limit - $sent_mail).
  • Slice the recipient array to that many addresses, validate each email, attempt sending, and increment the persistent counter only on success. Persist updates atomically (transaction or atomic UPDATE) so two concurrent scripts can’t overshoot the limit. Optionally throttle between sends or use a queue/cron to process N per run.

Example (illustrative)

$limit = 200;
$sent_mail = (int)$sent_mail_from_db;
$merc_list = array_filter(array_map('trim', explode(',', $merc_mailingList)));
$remaining = max(0, $limit - $sent_mail);

if ($remaining <= 0) {
    include 'limit.php';
} else {
    $toSend = array_slice($merc_list, 0, $remaining);
    foreach ($toSend as $email) {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) continue;
        $ok = mail($email, $subject, $body, $headers);
        if ($ok) {
            $sent_mail++;
            // persist $sent_mail back to DB (use atomic update/transaction)
        }
        // optional: usleep(...) to avoid throttling
    }
}

Extras: validate and trim addresses, log failures, use PHPMailer/SMTP or a provider if deliverability matters, and update the DB atomically (SELECT FOR UPDATE / UPDATE ... WHERE sent_mail < limit) to prevent race conditions.

Recommended Answers

All 7 Replies

what exactly are you trying to do?

i have created an email distribution system for creating and sending out newsletters. i want to be able to limit the user to only be able to send out 200 emails per day. the reason for this is the highest number of emails some servers will allow to be sent each day is 200.

also to try and stop this from being used for spamming.

here is what i would use:

<?php

$merc_list = explode(',',$merc_mailingList);

$send = true;
$i = 0;
while ($i < count($merc_list) && send = true) {
  //send emails
  if ($i > 200) {
    $send = false;
  }
}

?>

maybe not exactly what you need but i think you will get it and be able to manipulate it to your needs.

the code above didnt do all i needed so i have been away working on the solution still not sure if i have it tho.

my code now looks like this

$merc_list = explode(",",$merc_mailingList);

if ($send_mail < 200) {

// deploy the emails
for($i=0; $i<count($merc_list); $i++){

// email message body

$send_mail = $send_mail + $i
	} // END for
	} // END if
	else {
	include ("limit.php");
	} // END else

is the code i have used valid

no it is not.

has anyone ever written a piece of code like this before? probably people have just not the ones looking at this.

just to clarify my problem.

i need to be able to stop the emails being sent out when the counter reaches 200. there is already a count set up so that it counts the number of emails sent out, so i need to put some sort of an if statement in to stop it from sending the mail.

everything i have tried has failed.

Doesn't this work ?

<?php
$merc_list = explode(",",$merc_mailingList);
for($i=0; $i<count($merc_list); $i++) {
 if($i <=200) {
 	//send mail 
 } else {
 	//you can't send any more mails today
 }
}
echo "200 out of ".count($merc_list)." mails have been sent today !";	
?>

that looks like it has done the trick thanks for the reply. just having a bit of a problem with the sql i am adding to it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.