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 "[email protected], [email protected], 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 ='[email protected]'"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "{$row['email']}"; } echo $row; mysql_close(); $to = '[email protected]'; $subject = 'Guard Admin - Gig update test email, please delete.'; $message = 'Test'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\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. Link to comment https://forums.phpfreaks.com/topic/197275-need-help-trying-to-get-email-to-addresses-out-of-db-and-into-mail/ 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 = '[email protected]'; 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. Link to comment https://forums.phpfreaks.com/topic/197275-need-help-trying-to-get-email-to-addresses-out-of-db-and-into-mail/#findComment-1035446 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 Link to comment https://forums.phpfreaks.com/topic/197275-need-help-trying-to-get-email-to-addresses-out-of-db-and-into-mail/#findComment-1035451 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 , Link to comment https://forums.phpfreaks.com/topic/197275-need-help-trying-to-get-email-to-addresses-out-of-db-and-into-mail/#findComment-1035800 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? Link to comment https://forums.phpfreaks.com/topic/197275-need-help-trying-to-get-email-to-addresses-out-of-db-and-into-mail/#findComment-1035823 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) Link to comment https://forums.phpfreaks.com/topic/197275-need-help-trying-to-get-email-to-addresses-out-of-db-and-into-mail/#findComment-1035836 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.