paulsimpson 0 Newbie Poster

Hi Guys,

My first post here although I have been reading the forums here for quite a while now. i was wondering if anyone can help me with this problem I am having.

I am trying to use preg_replace to replace text with contact information in the database when sending emails to my contacts list.

I have this working correctly for the email body but when it comes to the headers I am having problems. What is currently happening is the preg_replace is working correctly but all emails contain the information for the first contact only and not the correct contact details as they should.

Here is a sample of the code I am currently using:

$text = '%{contact:first_name}% %{contact:last_name}%.';
$html = '%{contact:first_name}% %{contact:last_name}%.';
$headers = array(
'From' => '"'.$email['sender_name'].'" <'.$email['sender_email'].'>',
'To' => '%{contact:first_name}% <%{contact:email}%>'
);


// Get the details of all the contacts for the given broadcast

$query=("SELECT * FROM contacts WHERE c_id IN(1,2,3)");

$resq=mysql_query($query);

while($row=mysql_fetch_array($resq)){

// replace the template tags and send the mails
$rawhtml = $html;
$rawtext = $text;
$rawhdrs = $headers;


foreach($row as $key => $value) {
       
     
          $rawhtml = preg_replace('/\%\{contact:'.$key.'\}\%/ism',$value,$rawhtml);
      $rawtext = preg_replace('/\%\{contact:'.$key.'\}\%/ism',$value,$rawtext);
	  
	  foreach($rawhdrs as $hdr => $hval) {
$rawhdrs[$hdr] = preg_replace('/\%\{contact:'.$key.'\}\%/ism', $value, $hval);
} 
	    
  }

If anyone can spot the problem here and shed some light on it for me it would be greatly appreciated.

Thanks

Paul