Jump to content

Recommended Posts

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);
	}


}

Link to comment
https://forums.phpfreaks.com/topic/162986-cycle-through-an-array-sending-emails/
Share on other sites

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);
...
}

 

 

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

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";
}
?>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.