timmah1 Posted March 12, 2009 Share Posted March 12, 2009 I'm trying to set up a newsletter for a person. Everything is working good, the insertion of the newsletter and the sending. Right now there 139 people in the database, running a test with just 2 email address, I'm receiving 139 emails, when in reality, I should only be receiving 2. Can anybody tell me why this is? require("config.php"); $q = mysql_query("SELECT * FROM users WHERE active = 'yes' ORDER BY user_id"); $i = 1; while ($b = mysql_fetch_array($q)) { $email = $b['email']; $to1 = "tpatterson@cheezyfries.net,traey@hxcteam.com"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers = "From: Solon Sportsmen Club <noreply@solonsportsmen.org>\n"; $headers .= "Reply-to: noreply@solonsportsmen.org\n"; $headers .= "Bcc: $to1" . ', '; $headers .= "Bcc: noreply@solonsportsmen.org\n"; $headers .= "Bcc: noreply@solonsportsmen.org\n"; $headers .= "Bcc: noreply@solonsportsmen.org\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; mail($to, $subject, $message, $headers); echo "Newsletter has been sent to:<br />$i - $email<br />"; $i++; } It sends everything right, it's just sending to each person, the number of registered, that number of emails. Thanks in advance Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted March 12, 2009 Share Posted March 12, 2009 Well, you're defining which emails you want to send to inside your loop which runs for every record in the table. In order to only send to those emails, you should specify that in your query and use the email address in the record. Something like: require("config.php"); $q = mysql_query("SELECT * FROM users WHERE active = 'yes' AND email IN ('tpatterson@cheezyfries.net','traey@hxcteam.com') ORDER BY user_id"); $i = 1; while ($b = mysql_fetch_array($q)) { $email = $b['email']; $to1 = $email; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers = "From: Solon Sportsmen Club <noreply@solonsportsmen.org>\n"; $headers .= "Reply-to: noreply@solonsportsmen.org\n"; $headers .= "Bcc: $to1" . ', '; $headers .= "Bcc: noreply@solonsportsmen.org\n"; $headers .= "Bcc: noreply@solonsportsmen.org\n"; $headers .= "Bcc: noreply@solonsportsmen.org\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; mail($to, $subject, $message, $headers); echo "Newsletter has been sent to:<br />$i - $email<br />"; $i++; } That said, you should check the notes from the php manual about the mail() function: Note: It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient. Quote Link to comment Share on other sites More sharing options...
timmah1 Posted March 12, 2009 Author Share Posted March 12, 2009 ok, so the emails to send to are in the query, that's great and all, but that was for testing only, to make sure that those 2 emails received the newsletter. The actual number of people that are going to get this, is 139. So with this code, will all 139 people receive 139 emails or will each one just get one? <?php require("config.php"); $to = "Member"; $q = mysql_query("SELECT * FROM users WHERE active = 'yes' ORDER BY user_id"); $i = 1; while ($b = mysql_fetch_array($q)) { $email = $b['email']; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers = "From: Solon Sportsmen Club <noreply@solonsportsmen.org>\n"; $headers .= "Reply-to: noreply@solonsportsmen.org\n"; $headers .= "Bcc: $email" . ', '; $headers .= "Bcc: noreply@solonsportsmen.org\n"; $headers .= "Bcc: noreply@solonsportsmen.org\n"; $headers .= "Bcc: noreply@solonsportsmen.org\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; mail($to, $subject, $message, $headers); echo "Newsletter has been sent to:<br />$i - $email<br />"; $i++; } ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted March 12, 2009 Share Posted March 12, 2009 for testing purposes, duplicate the structure of the users table and call it "users_test". then, populate it with 2 or 3 records of fake data with your email addresses. then, change your select statement to pull from that table. run it, tweak it, get it to exactly what you want. then change the table name back to users. run it ONCE to send the emails. make sure you use set_time_limit(0); so the script doesn't time out: http://us3.php.net/function.set-time-limit after it's done, change it back to users_test to make sure it doesn't send them again (in case you accidentally run the script again) 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.