dazzathedrummer Posted April 1, 2010 Share Posted April 1, 2010 Hi, I'm trying join up two things here, i have the following code that will return selected email addresses from a DB as a string with commas (eg "eamail1@email.com, email2@email.com, etc": - <?php // Connects to your Database mysql_connect("database", "host", "password") or die(mysql_error()); mysql_select_db("the_guards_org_uk_users") or die(mysql_error()); $query = "SELECT fname, sname, username, email FROM users where is_guard=1"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo"{$row['email']}, "; } echo $row; mysql_close(); ?> I also have this simple code for sending mail from the server: - <?php // Connects to your Database mysql_connect("database.lcn.com", "LCN_7792", "theguards9402") or die(mysql_error()); mysql_select_db("the_guards_org_uk_users") or die(mysql_error()); $query = "SELECT fname, sname, username, email FROM users where email ='darren.bullough@btinternet.com'"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "{$row['email']}"; } echo $row; mysql_close(); $to = 'eamil1@email.com'; $subject = 'Guard Admin - Gig update test email, please delete.'; $message = 'Test'; $headers = 'From: admin@the-guards.co.uk' . "\r\n" . 'Reply-To: admin@the-guards.co.uk' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> How can I use the results of the first query as the email addresses for the 'To' feild in the mail code?? I've tried 'including' the file, setting a variable and call the variable where $to is set and I've also tried the same but with the first code incorporated as opposed to included. admittedly, I must be doing it wrong???? The reasoning behind this is so that email recipients can change their email address elsewhere on the site. Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted April 1, 2010 Share Posted April 1, 2010 Change: while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo"{$row['email']}, "; } to: $email_list = ''; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $email_list .= "{$row['email']}, "; } Then change $to = 'eamil1@email.com'; To $to = $email_list; Actually, I'm not entirely sure if this 2nd part will work as I do not normally e-mail multiple users at the same time. Hopefully if it is wrong someone else can point out how to fix it, if no one does and it doesn't work, let me know and I'll get you something that does. Quote Link to comment Share on other sites More sharing options...
dazzathedrummer Posted April 1, 2010 Author Share Posted April 1, 2010 Wow - this forum is so fast!! Thats great, it works perfectly - thanks a lot Quote Link to comment Share on other sites More sharing options...
ignace Posted April 2, 2010 Share Posted April 2, 2010 If you only need the e-mail you better change: SELECT fname, sname, username, email FROM users where is_guard=1 to: SELECT email FROM users where is_guard=1 now you can: $emails = array(); while (list($email) = mysql_fetch_array($result, MYSQL_NUM)) { $emails[] = $email; } $email_list = implode(',', $emails); // avoids the trailing , Quote Link to comment Share on other sites More sharing options...
dazzathedrummer Posted April 2, 2010 Author Share Posted April 2, 2010 that's great thanks a lot. ...out if interest, this particular email is for band members so we dont mind seeing each others addresses in the 'to' field - how simple would it be to adapt this code so that it sends one email per email address in a mailing list - so that each person in the list can only see their own address? Quote Link to comment Share on other sites More sharing options...
ignace Posted April 2, 2010 Share Posted April 2, 2010 while (list($email) = mysql_fetch_array($result, MYSQL_NUM)) { mail($email, ..) } This sends n-emails, where n equals mysql_num_rows($result) Quote Link to comment 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.