Log in

View Full Version : Mailing to multiple addresses



captainjustin
03-14-2010, 03:35 AM
I'm trying to notify (certain members) of my site, when I upload new pictures. The php code will send out the email but, only to one person. I'm trying to send it to everyone with the set_note "2" if that makes any sense. I'm still learning and any help would be greatly appreciated....




<?
include_once "accesscontrol.php";
$notify = 2;

$q11 = "select * from members_info where set_note = \"$notify\" ";
$r11 = mysql_query($q11) or die(mysql_error());
$a11 = mysql_fetch_array($r11);



if( !empty( $a11 ) )
{
$to = $a11[members_email];
$subject = "New Pictures Posted";
$message = "Dear $a11[fname],\n\nWe posted some new baby pictures on our website. Please login to www.$_SERVER[HTTP_HOST] to view the album.\n\nIf you want to stop getting these automatic notices, please login and change your profile settings.\n\nThanks\nJustin";
$from = "From: $_SERVER[HTTP_HOST] <$aset[ContactEmail]>";

mail($to, $subject, $message, $from);
}


?>

<table align="center" width="600" cellpadding="0" cellspacing="0">
<tr><td align="center"><br /><br /><b>Notified Participating Members!</b></td></tr>




</table>

<? include("../footer.php"); ?>

djr33
03-14-2010, 05:58 AM
The simple explanation is that you are only getting one row from the database:
mysql_fetch_array() gets one row and then you can do things with it. Use it another time and it will magically grab the next row. Repeat until there are no results (it will return 'false' instead of a new row).

The traditional way to write this is:

$query = "SELECT 'x' FROM 'table';";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) { //loop through all rows, until no more exist
echo $row['x'];
}

You can easily apply the same method here.

Alternatively you could put them all into a single array (list of emails) then send to all using a single mail() line, but that would show them each their email addresses (using "CC").



To find out more about mysql, take a look at this website, starting with the "last" tutorial which was posted first, which will (as of posting) be on page 2:
http://php-mysql-tutorial.com