Multiple email recipients and dynamic data
Posted: Tue Dec 22, 2009 8:41 am
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.";
}
?>