unistake Posted February 28, 2008 Share Posted February 28, 2008 Hi, I have got an array of users using a PHP program. However I am now trying to SELECT email FROM Member WHERE user='$user' Once I have somehow selected all the email addresses I want to be able to send the same email to each of them! The code I have attempted to make so far is.... <?php include("../dbinfo.inc.php"); $cxn = mysqli_connect($host,$username,$password,$database) or die ("Couldn't connect to server."); $game="Chelsea V Liverpool"; ////// MANUALLY CHANGE THE VARIABLE $query = "SELECT DISTINCT user FROM history WHERE game='$game'"; ///////// SELECTS RELEVANT USERS $result = mysqli_query($cxn,$query) or die ("Couldn't execute query."); while($row = mysqli_fetch_assoc($result)) /// Take all the results $tsubject = "subject"; $ttext = "text"; $sender = "sender@sender.com"; @mail({$row['user']}, $tsubject, $ttext, "FROM: $sender"); /////////// SENDS EMAIL } ?> The problem I have is somehow putting all the users email addresses in to the line. @mail({$row['user']}, $tsubject, $ttext, "FROM: $sender"); But I dont have a clue how to! Any suggesstions are much appreciated! thanks Quote Link to comment https://forums.phpfreaks.com/topic/93559-sending-emails-via-php-to-an-array-of-emails/ Share on other sites More sharing options...
Psycho Posted February 28, 2008 Share Posted February 28, 2008 This will put all the emails in a single string, comma separated. <?php $recipients = new array(); while($row = mysqli_fetch_assoc($result)) { $recipients[] = $row['user']; } $recipient_list = implode(", ", $recipients); ?> Quote Link to comment https://forums.phpfreaks.com/topic/93559-sending-emails-via-php-to-an-array-of-emails/#findComment-479425 Share on other sites More sharing options...
unistake Posted February 28, 2008 Author Share Posted February 28, 2008 sorry can you make that a bit simpler for a PHP newbie! What would i need to type in the £email voriable here? --> @mail($email, $subject, $text, $header) thanks Quote Link to comment https://forums.phpfreaks.com/topic/93559-sending-emails-via-php-to-an-array-of-emails/#findComment-479473 Share on other sites More sharing options...
unistake Posted February 28, 2008 Author Share Posted February 28, 2008 Would someone be able to edit my code to make this work?! Or tell me a site I could find out how to do this! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/93559-sending-emails-via-php-to-an-array-of-emails/#findComment-479506 Share on other sites More sharing options...
Psycho Posted February 28, 2008 Share Posted February 28, 2008 The code I provided takes the email addresses from the query and puts them into a single string that is comma separated. You just need to use that value ($recipient_list) as the first parameter in the email() function: <?php include("../dbinfo.inc.php"); $cxn = mysqli_connect($host,$username,$password,$database) or die ("Couldn't connect to server."); $game="Chelsea V Liverpool"; ////// MANUALLY CHANGE THE VARIABLE $query = "SELECT DISTINCT user FROM history WHERE game='$game'"; ///////// SELECTS RELEVANT USERS $result = mysqli_query($cxn,$query) or die ("Couldn't execute query."); $recipients = new array(); while($row = mysqli_fetch_assoc($result)) { $recipients[] = $row['user']; } $recipient_list = implode(", ", $recipients); $tsubject = "subject"; $ttext = "text"; $sender = "sender@sender.com"; @mail($recipient_list, $tsubject, $ttext, "FROM: $sender"); /////////// SENDS EMAIL ?> Quote Link to comment https://forums.phpfreaks.com/topic/93559-sending-emails-via-php-to-an-array-of-emails/#findComment-479546 Share on other sites More sharing options...
unistake Posted February 28, 2008 Author Share Posted February 28, 2008 Tried that but it came up with an error "Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING or T_VARIABLE or '$' in .....l.php on line 12" The actual email addresses have to be taken from another table in the database called Member. The two tables history and Member have the columns 'user' in common. This may be something to do with it. Also could you tell me what this part does $recipients = new array(); thanks Quote Link to comment https://forums.phpfreaks.com/topic/93559-sending-emails-via-php-to-an-array-of-emails/#findComment-479555 Share on other sites More sharing options...
Psycho Posted February 29, 2008 Share Posted February 29, 2008 That creates an empty array variable. It's just good practice to initialize your variables before you start using them. Quote Link to comment https://forums.phpfreaks.com/topic/93559-sending-emails-via-php-to-an-array-of-emails/#findComment-479647 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.