Page 1 of 1

Multiple email recipients and dynamic data

Posted: Tue Dec 22, 2009 8:41 am
by kidcobra
Hi. I have a working cron job that looks at a mysql dbase and sends an email (up to three email addresses per company) to each company whose event occurred the day before (yesterday's date in the dbase date field, SDate). The email works fine thanks to some great help I received a couple of weeks ago in this forum. If six dbase records show yesterday's date, and they each have 1 email address entered, 6 emails go out to the right 6 addresses. I'm trying to add dynamic data from each row of the respective recipient's dbase record to their email. For example, a photo URL that is in their dbase record as shown in the email. That also works fine. But the way I have the added loop at the end to send out the emails automatically, it sends the same dynamic data (in this case the same picture) to each recipient... using the data from the first dbase record it finds that meets the date criteria. Basically, the dynamic data is stuck on the first record, and does not update as the email addresses update (properly) and it sends out the next email. Any help or suggestions in getting it to re-look at the respective dbase info for the dbase info that matches each record/email address so that the right picture (or any other dynamic data from that record) is in it's proper email would be greatly appreciated.

Code: Select all

mysql_select_db($database_conntest, $conntest);
$query_rsa = "SELECT * FROM Items WHERE SDate= CURDATE() - Interval 1 Day";
$rsa = mysql_query($query_rsa, $conntest) or die(mysql_error());
$row_rsa = mysql_fetch_assoc($rsa);
$totalRows_rsa= mysql_num_rows($rsa);
?>
 
<?php
$headers =  "From: House@Example.com <house@example.com>\r\n" .
            "MIME-Version: 1.0\r\n" .
            "Content-type: text/html; charset=UTF-8";
$subject = "Example Subject";
$headerPic = "<IMG src='http://example,com/photos/$row_rsa[Image_URL]' width='198' class='imgfloatlft' align='left' hspace='10'>";
$body = "<html><body>" .
        "<h2><center>Heading</center></h2>" .
        
        "<p>TEXT</p>" . 
          
        "<p>$headerPic</p>" .
                   
         "<p>TEXT</p>" . 
 
         "</body></html>";
 
mysql_select_db($database_conntest, $conntest);
$result = mysql_query("SELECT Email1, Email2, Email3 FROM Items WHERE SDate=CURDATE() - Interval 1 Day");
if(mysql_num_rows($result) > 0) 
{ 
$count = 0; 
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) 
{
$to = $row['Email1'] . ', '; 
$to .= $row['Email2'] . ', ';
$to .= $row['Email3']; 
mail($to, $subject, $body, $headers); 
$count++; 
} 
echo "myResult=$count Emails Sent."; 
} 
else 
{ 
echo "myResult=Email Submissions Failed."; 
} 
 
?>
 

Re: Multiple email recipients and dynamic data

Posted: Tue Dec 22, 2009 12:27 pm
by Christopher
Just move the code that assigns to the $subject, $body, $headers into the loop. You will only need one query then (which will need to be SELECT *)

Re: Multiple email recipients and dynamic data

Posted: Mon Dec 28, 2009 7:42 am
by kidcobra
Hi Christopher. Thanks so much for the input. It took me a few days to get back to this with the holidays etc., but using your advice/directions as a guide this morning, I got it working! I really appreciate the help, I would not have got it figured out without your answering my question.

I hope you have a great finish to the holiday season and happy new year as well! Greg