Jump to content

mail() function sending messages in multiple times instead of sending each message once to each of the emails from database

francis Aneke

Recommended Posts

The code is sending multiple messages to all the emails at the same time.
Even if you send one message, it will send it into multiple times to each email in my database...
PLEASE how can i twist my code to send the messages without having it multiple times in each email
while($row = mysqli_fetch_array($sql_level, MYSQLI_ASSOC)) {
$moderator = $row['moderator'];
array_push($all_moderators, $moderator);
$Logic = '';
foreach($all_moderators as $key => $user){
$Logic .= "username='$user' AND msgDont='0' OR ";
//chop off the last OR from the $Logic
$Logic = chop($Logic, "OR ");
$all_DU = array();
///Regulate whom to receive message
$DoesAndDont = "SELECT username FROM doesanddont WHERE $Logic";
$query_does = mysqli_query($db_conx, $DoesAndDont) or die('line'.__LINE__.':'.mysqli_error($db_conx));
while($row = mysqli_fetch_array($query_does, MYSQLI_ASSOC)) {
$DU = $row['username']; //Does_user
array_push($all_DU, $DU);
$DU_Logic = '';
$DoesU = '';
foreach($all_DU as $key => $DU){
//$DoesU .="$DU ";
$DU_Logic .= "username='$DU' OR ";
$DU_Logic = chop($DU_Logic, "OR ");
$errorContactMessage = "<br /><br />Carry on $DU_Logic ";
$all_e = array();
$query_email = "SELECT email FROM users WHERE $DU_Logic";
$sql_email = mysqli_query($db_conx, $query_email);
while($row = mysqli_fetch_array($sql_email, MYSQLI_ASSOC)) {
$e = $row['email'];
array_push($all_e, $e);
foreach($all_e as $key => $e){
///insert into EMAIL
$to = "$e";
//$to = "francisaneke488@yahoo.com";
$from = "auto_responder@awaenvirons.com";
$subject = '[AE] AwaEnvirons Contact Message';
$message = '<!DOCTYPE html><html><head><meta charset="UTF-8">
<title>AwaEnvirons Contact Message</title>
</head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;">
<div style="
background-image: -webkit-linear-gradient(top, #ffffff, #4e6d04);
background-image: -moz-linear-gradient(top, #ffffff, #4e6d04);
background-image: -o-linear-gradient(top, #ffffff, #4e6d04);
background-image: linear-gradient(#ffffff, #4e6d04);
<a href="http://www.awaenvirons.com"><img src="http://www.awaenvirons.com/images/logo.png" width="36" height="30" alt="awaenvirons.com" style="border:none; float:left;"></a>AwaEnvirons Contact Message</div><divstyle="padding:24px; font-size:15px;">mmmmmmTmmmm<p style="margin-top:30px; font-size:12px; text-align:right; color:green; border-top:1px green solid;">Powered By: MR FRANCIS and AwaEnvirons Team</p></div></body></html>';
$headers = "From: $fromn";
$headers .= "MIME-Version: 1.0n";
$headers .= "Content-type: text/html; charset=iso-8859-1n";
$mail = mail($to, $subject, $message, $headers);
$errorContactMsg = "<br /><br /><p style='color:green'>Contact Message Sent Successfully</p>";
Edited by francis Aneke
Link to comment
Share on other sites

You have a lot of nested loops there. Of course it's going to send multiple e-mails.


You should properly indent your code, your braces don't match.


This is the structure your current code has:

while($row = mysqli_fetch_array($sql_level, MYSQLI_ASSOC)) {    [ ... ]    while($row = mysqli_fetch_array($query_does, MYSQLI_ASSOC)) {        [ ... ]        while($row = mysqli_fetch_array($sql_email, MYSQLI_ASSOC)) {            [ ... ]            foreach($all_e as $key => $e) {                [ ... ]                $mail = mail($to, $subject, $message, $headers);                [ ... ]            }        }

If A is the number of rows in $sql_level, B is the number of rows in $query_does and C is the number of rows in $sql_email then the total number of e-mails sent will be A · B · C · (C + 1)/2 which is a whole lot of e-mails.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...