Jump to content

Mass Email messaging


francis Aneke

Recommended Posts

Please i have been on this for days without any final correct result:-
1.The code below simply select names from moderators, a table upgrading user levels based on how they use the wbsite.
2.It uses the names to select users who loves contact messages to appear on their emails, when a user sents a contact message( the names of this email lovers are entered on the doesanddont table with msgDont='0') .
3.The very names of users on doesanddont table is use to in turn select emails from users table,
Which is use to finally passed the message to all of them as coded in the "foreach($e as $mailTo){}" loop below.
But when this message is being sent, it comes in multiple, multiple times into each emails.
PLEASE HELP ME TWIST THIS CODE for the emails to receive the message once every time an email is sent from contact page(http://awaenvirons.com/contact.php)....
And "foreach($e as $mailTo)" is not working after i replaced a line of code with it...
*******************************************************************
if($username !="" || $nickname !=""){
$emailer = "";
if($username !=""){
$emailer = $username;
}else $emailer = $nickname;
$all_moderators = array();
$query_level = "SELECT moderator FROM moderators WHERE userlevel='a' OR userlevel='b' OR userlevel='c' OR userlevel='d' OR userlevel='e'";
$sql_level = mysqli_query($db_conx, $query_level);
///$row = mysqli_fetch_assoc($sql_level);
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 ";
$query_email = "SELECT username, 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'];
$e_user = $row['username'];
///learn array in other to pack email and username inside
///$mailTo ='';
foreach($e as $mailTo){
///insert into EMAIL
///$errorContactMsg = "<br /><br /><p style='color:green'>Contact Message Sent Successfully </p>";
$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="
padding:10px;
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);
font-size:24px;
font-weight:bold;
color:red;
">
<a href="http://www.awaenvirons.com"><img src="http://www.awaenvirons.com/images/logo.png" width="50" height="30" alt="[AE]" style="border:none; float:left;"></a>AwaEnvirons Contact Message</div><divstyle="padding:24px; font-size:15px;">chi chi<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($mailTo, $subject, $message, $headers);
if($mail){
$errorContactMsg = "<br /><br /><p style='color:green'>Contact Message Sent Successfully $mailTo</p>";
}
}
//}
}
}///for $query_does
}///for $sql_level
}
Link to comment
Share on other sites

You have too many nested loops. It's hard to read your code when you don't put it in a code box so we can see the formatting, but it looks like you're doing this:

loop over database moderators:  add moderator to moderators array  loop over moderators array:    build contact list  get usernames  loop over emails:    send email
It should do something more like this:
loop over database moderators:  add moderator to moderators arrayloop over moderators array:  build contact listget usernamesloop over emails:  send email
Hopefully you see the difference. Your code is looping over the list of moderators and sending the emails to everyone for each moderator. It should build the lists and things first, then get everyone from the database and send the emails.

And "foreach($e as $mailTo)" is not working after i replaced a line of code with it...

That's because $e is a single value from a database field, not an array.
Link to comment
Share on other sites

That's because $e is a single value from a database field, not an array.

 

that is among what am saying how can you get an array of those emails and then use "foreach($e as $mailTo)" to get them one by one and pass the contact message to them; Just a brief code please.......

Link to comment
Share on other sites

You're already doing it with the moderators. You get the results from the database, loop through them, and add each value to an array. Or, you could just loop through the results and send the email in that loop without adding to another array.

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...