imperialized Posted June 20, 2009 Share Posted June 20, 2009 Ok, here is my problem.. this script cycles through the array $email[] and replaces certain wildcards with different variables. However, It puts the same variables for every email that is sent. How would I change my loop so that it will send a customized email to each person? foreach ($emails as $email) { // do something $to = "$email"; $subject = "$subject"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= 'From: [email protected]' . "\r\n" . 'reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); #connec to DB and get their info to replace the codes $query = mysql_query("SELECT * FROM poker_users WHERE email='$email' LIMIT 1"); while($x = mysql_fetch_array($query)){ $fname = $x['fname']; $lname = $x['lname']; $poker_name = $x['poker_name']; $msg = str_replace("%fname%", $fname, $msg); $msg = str_replace("%lname%", $lname, $msg); $msg = str_replace("%pname%", $poker_name, $msg); print "to: $email msg: $msg"; # $go = mail($to, $subject, $msg, $headers); } } Quote Link to comment https://forums.phpfreaks.com/topic/162986-cycle-through-an-array-sending-emails/ Share on other sites More sharing options...
fnairb Posted June 20, 2009 Share Posted June 20, 2009 Not sure this is what you are going for but.... $original will be the same with every pass. $original = 'Hello %fname% %lname%, AKA %pname%'; foreach (...) { ... $msg = str_replace("%fname%", $fname, $original); $msg = str_replace("%lname%", $lname, $msg); $msg = str_replace("%pname%", $poker_name, $msg); ... } Quote Link to comment https://forums.phpfreaks.com/topic/162986-cycle-through-an-array-sending-emails/#findComment-860005 Share on other sites More sharing options...
imperialized Posted June 20, 2009 Author Share Posted June 20, 2009 I need the $msg to change with each pass to make each email customized with the name that is on hte account $query = mysql_query("SELECT * FROM poker_users WHERE email='$email' LIMIT 1"); while($x = mysql_fetch_array($query)){ $fname = $x['fname']; $lname = $x['lname']; $poker_name = $x['poker_name']; $msg = str_replace("%fname%", $fname, $msg); $msg = str_replace("%lname%", $lname, $msg); $msg = str_replace("%pname%", $poker_name, $msg); that's why I put the query inside the foreach loop.. not sure why it doesnt change Quote Link to comment https://forums.phpfreaks.com/topic/162986-cycle-through-an-array-sending-emails/#findComment-860018 Share on other sites More sharing options...
fnairb Posted June 20, 2009 Share Posted June 20, 2009 What is $msg set to originally? Quote Link to comment https://forums.phpfreaks.com/topic/162986-cycle-through-an-array-sending-emails/#findComment-860120 Share on other sites More sharing options...
fnairb Posted June 20, 2009 Share Posted June 20, 2009 This code snippet runs as expected producing We now welcome Jake.... The Snake... Roberts!!!! We now welcome Rick.... The Nature Boy... Flare!!!! We now welcome Vince.... Sissy Boy... McMahon!!!! <?php /* You would use your real query here. I am assuming * your query is returning the correct data. I just * faked it so I could test functionality */ //$query = mysql_query("SELECT * FROM poker_users WHERE email='$email' LIMIT 1"); $query = array( array('fname' => 'Jake', 'lname' => 'Roberts', 'poker_name' => 'The Snake'), array('fname' => 'Rick', 'lname' => 'Flare', 'poker_name' => 'The Nature Boy'), array('fname' => 'Vince', 'lname' => 'McMahon', 'poker_name' => 'Sissy Boy') ); /* This is the original msg with the place holders that * will be replaced with each execution of the while loop */ $originalMsg = 'We now welcome %fname%.... %pname%... %lname%!!!!'; //while($x = mysql_fetch_array($query)){ foreach ($query as $x) { $fname = $x['fname']; $lname = $x['lname']; $poker_name = $x['poker_name']; /* $originalMsg is only used in the first replace */ $msg = str_replace("%fname%", $fname, $originalMsg); $msg = str_replace("%lname%", $lname, $msg); $msg = str_replace("%pname%", $poker_name, $msg); print "$msg\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/162986-cycle-through-an-array-sending-emails/#findComment-860133 Share on other sites More sharing options...
imperialized Posted June 20, 2009 Author Share Posted June 20, 2009 I see my problem, thanks for the replies.. It replaces the vars with the orig msg on the first loop but after that there is nothing to replace as the wildcards are already replaced with names.. will fix this later. After a couple hours I start to miss the little things. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/162986-cycle-through-an-array-sending-emails/#findComment-860201 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.