I am trying to write some PHP code that emails every user in my database (around 500) their usernames. I have successfully managed to pull the email addresses and usernames for each user and store them in an array. However, I cannot figure out how to email each individual user with their individual usernames. I am pretty sure I need to use a foreach
loop to do this, but I have had no luck.
Here is what I have.
<?php
include('databaseConn.php');
$query = mysql_query("SELECT * FROM staff");
$emailArray;
while ($row = mysql_fetch_array($query)) {
$emailArray[] = array($row['email']=>$row['username']);
}
print_r($emailArray); //This associative array now contains each username along with their respective email address.
?>
==***********=== WITH MAIL FUNCTION
<?php
include('functions/core.php');
$query = mysql_query("SELECT * FROM users");
$emailArray;
while ($row = mysql_fetch_array($query)) {
$emailArray[] = array($row['email']=>$row['username']);
}
foreach($emailArray as $email => $username) {
echo $username; // outputs the indexes.
$subject = 'Accoutn Details';
$message = 'This email contains your login details.<br/><br/>
<b>Username: '.$username.'</b><br/>
<br/><br/>Kind regards,<br/>';
$headers = 'From: noreply@xxxxx.co.uk' . "\r\n" .
'Reply-To: noreply@xxxxx.co.uk' . "\r\n" .
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($emailAddress, $subject, $message, $headers);
}
//print_r($emailArray);
?>