Re: Re: writing a list of email addresses

From: Date: Thu, 24 Apr 2003 14:12:10 +0000
Subject: Re: Re: writing a list of email addresses
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-145154@lists.php.net to get a copy of this message
peter, i prefer to use 'for' statements when getting data from mysql. you can use 'while' also, it's just a matter of preference. anyway, you could do something like this: $to = ""; for ($i=0;$i<mysql_num_rows($res);#i++) { // This is to make sure a comma and a space is placed before every address except the first // Why not the first address? Because it would look like this: ", 'firstname' <firstemail>, 'secondname' <secondemail>" <-- no good if ($i!=0) { $to .= ", "; } // Get the email address $email = mysql_result($res,$i,email); $to .= $email; } and that's it...you're done. now the only thing about this is that your SQL query is only selecting the email address. no name information is being selected at all. so let's say you wanted to include the recipients name when sending a message, then you would have to modify a few things. first, your query has to include whatever 'name' field you have in your database: SELECT email,name from users where level = 'admin' then, you would modify your code to look like this: for ($i=0;$i<mysql_num_rows($res);#i++) { if ($i!=0) { $to .= ", "; } // Get the email address $email = mysql_result($res,$i,email); // Get the person's name $name = mysql_result($res,$i,name); $to .= "\"$name\" <$email>"; } and you're done. -jake "Peter Gumbrell" <peter@orillia.net> wrote in message news:CA420E50-7643-11D7-8C34-000393AB7938@orillia.net... > Thanks for the reply, but I still have a question. How does one get > from the array holding the addresses to the individual addresses? > From: > > $sql_email = "SELECT email from users where level = 'admin'"; > > $res = mysql_query($sql_email); > > while ($res_email = mysql_fetch_array($res)) > > to > > $to = "\"First Person\" <firstemail>, \"Second Person\" > > <secondemail>" > > Thanks > > Peter > > > On Wednesday, April 23, 2003, at 10:06 PM, Jake Malone wrote: > > > The mail() function will accept recipients in the following form: > > > > "name" <email> > > > > it's always a good idea to have quotes around the name. to add more > > than one > > recipient, just format your "To" line like this: > > > > $to = "\"First Person\" <firstemail>, \"Second Person\" > > <secondemail>" > > > > etc. > > > > -Jake > > > > > > "Peter Gumbrell" <peter@orillia.net> wrote in message > > news:B9662969-75F5-11D7-994D-000393AB7938@orillia.net... > > I would like to pull a list of email addresses from a group of users, > > based on certain conditions, then format the list so that it can be > > used within the mail() function. Am I right in assuming that there will > > need to be a semi-colon between each address? > > > > So far I have: > > > > $sql_email = "SELECT email from users where level = 'admin'"; > > $res = mysql_query($sql_email); > > while ($res_email = mysql_fetch_array($res)) > > > > but I am unsure as to what the next line should be. Any help would be > > appreciated. > > > > Many thanks > > > > Peter Gumbrell > > > > > > > > > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > >

« previous php.general (#145154) next »