I have a simple MySQL table with just two fields: 'id' and 'email'. For test purposes, just two email addresses are stored.
To start with, I wrote the following test code:
$query="SELECT email FROM $tbl_name1 ";
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());
while ($row = mysql_fetch_row($result)) {
foreach ($row as $email) {
echo $email;
}}
echo "Newsletter sent";
Both email addresses appeared on the screen, so I thought I was on the right track.
However, the following did not result in an iterative process - just one email appeared on the screen, and just one arrived at its destination:
$query="SELECT email FROM $tbl_name1 ";
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());
while ($row = mysql_fetch_row($result)) {
foreach ($row as $email) {
$email_from="postmaster@xxxxxxxxxxxx.com";
$to=$email;
$subject="Test newsletter";
$headers = "From: $email_from\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; //content type
$headers .= "MIME-Version: 1.0\r\n";
$return_path="postmaster@xxxxxxxxxxxx.com";
//newsletter address
$html="http://www.xxxxxxxxxxxx.com/newsletter/one1.htm";
//read the entire newsletter to a string.
$message=file_get_contents ($html);
//wraps the content string
$message=wordwrap($message, 72);
//email out
$result = mail($to, $subject, $message, $headers, "-f $return_path");
echo $email; $count=$count+1;
}}
echo "Newsletter sent";
Any ideas why the email newsletter wasn't generated for each email address in the database? Could it be a web host thing? - they have said they have no problems with me sending out a newsletter.