PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have made a page that gives you the choice of email you would like to send and a selection of choices to who from the members list you would like to send them too. Problem is I have a loop in here somewhere that sends me loads of mail repeatedly
<?php
if ($submit) { //open1
$mail = new htmlMimeMail();
$mail->setFrom('"CCTV-City" <news@cctv-city.com>');
$mail->setHeader('X-Mailer', 'HTML Mime mail class (http://www.cctv-city.com)');
$mail->setReturnPath('news@cctv-city.com');
//the if statements will find out which email is being sent
// inside the if we will try and work out which email address we wish the email to be sent to
if ($newsletter == "generalNewsletter" ){ //open 2
$mail->setSubject('CCTV Newsletter');
$html = $mail->getFile('../emails/AutomaticNewsletter.php');
$mail->setHtml($html);
// need to run a statement that finds how we are sending to
if ($group == "all"){ //open3
$query = "SELECT user_email FROM ccMembers
ORDER BY user_id ASC ";
$searchResults = mysql_query($query) or die('error making query');
$resultRowCount = mysql_num_rows($searchResults);
// the while will run all email addresses
while ( $resultRowCount > 0){ //open4
$row = mysql_fetch_array($searchResults);
$mail->send(array($row["user_email"]), 'smtp');
} //close4
echo 'emails have been sent';
} //close3
?>
after clicking on submit in my form all i get is a blank not fully loaded page and no ''emails have been sent' message.
<?php
if ($submit) { //open1
$mail = new htmlMimeMail();
$mail->setFrom('"CCTV-City" <news@cctv-city.com>');
$mail->setHeader('X-Mailer', 'HTML Mime mail class (http://www.cctv-city.com)');
$mail->setReturnPath('news@cctv-city.com');
//the if statements will find out which email is being sent
// inside the if we will try and work out which email address we wish the email to be sent to
if ($newsletter == "generalNewsletter" ){ //open 2
$mail->setSubject('CCTV Newsletter');
$html = $mail->getFile('../emails/AutomaticNewsletter.php');
$mail->setHtml($html);
// need to run a statement that finds how we are sending to
if ($group == "all"){ //open3
$query = "SELECT user_email FROM ccMembers ORDER BY user_id ASC ";
$searchResults = mysql_query($query) or die('error making query');
$resultRowCount = mysql_num_rows($searchResults);
// the while will run all email addresses
while ( $resultRowCount > 0){ //open4
$row = mysql_fetch_array($searchResults);
$mail->send(array($row["user_email"]), 'smtp');
} //close4
echo 'emails have been sent';
} //close3
/** something's missing here */
?>
<?php
if ($group == "all"){ //open3
$query = "SELECT user_email FROM ccMembers
ORDER BY user_id ASC ";
$searchResults = mysql_query($query) or die('error making query');
$resultRowCount = mysql_num_rows($searchResults);
$row = mysql_fetch_array($searchResults);
// the while will run all email addresses
while ( $resultRowCount > 0){ //open4
echo 'emails are being sent to the following';
echo '<br>';
echo $row["user_email"];
echo '<br>';
$mail->send(array($row["user_email"]), 'smtp');
$resultRowCount--;
} //close4
echo 'emails have been sent';
} //close3
?>
although I seemed to get lots of bounced mail when running it. Basically I can remove the $mail->send and i get the result I want. So is it a problem with my mimeMAIL. Thanks for you help anyway and I'll get back to the drawing board.
hm, I can only review code that I can see
And in the first code snippet the closing brackets for //open1 and // open2 were missing.
if ($submit) { //open1
Thought this might come from a form and register_globals is turned off, but as mention this might apply to your version, might not, only guessing (and obviously wrong ).
$mail->send(array($row["user_email"]), 'smtp');
it takes an array as first parameter and will send the mail to each address within the array but you don't want the other people's addresses shown in the mail, so sending ony-by-one?
The examples at http://www.php.net/manual/en/function.mail.php work, your smtp-settings are correct? (but this should not prevent the page from beeing displayed)
<?php
$searchResults = mysql_query($query) or die('error making query');
$resultRowCount = mysql_num_rows($searchResults);
$row = mysql_fetch_array($searchResults);
// the while will run all email addresses
while ( $resultRowCount > 0){ //open4
echo 'emails are being sent to the following';
echo '<br>';
echo $row["user_email"];
echo '<br>';
$mail->send(array($row["user_email"]), 'smtp');
$resultRowCount--;
} //close4
?>
in as much as it counts the right amount of lines and then counts them down but is not picking up a fresh value each time.
Here is my full code below. I didn't want to print it all originally as it seems a bit 'heres my code fix it for me' . thanks for any light you can throw on this for me.
only the first choice has mail enabled for testing purposes.
<?php
if ($submit) { //open1
$mail = new htmlMimeMail();
$mail->setFrom('"CCTV-City" <news@cctv-city.com>');
$mail->setHeader('X-Mailer', 'HTML Mime mail class (http://www.cctv-city.com)');
$mail->setReturnPath('deej@cctv-city.com');
//the if statements will find out which email is being sent
// inside the if we will try and work out which email address we wish the email to be sent to
if ($newsletter == "generalNewsletter" ){ //open 2
$mail->setSubject('CCTV Newsletter');
$html = $mail->getFile('../emails/AutomaticNewsletter.php');
$mail->setHtml($html);
// need to run a statement that finds how we are sending to
if ($group == "all"){ //open3
$query = "SELECT user_email FROM ccMembers
ORDER BY user_id ASC ";
$searchResults = mysql_query($query) or die('error making query');
$resultRowCount = mysql_num_rows($searchResults);
$row = mysql_fetch_array($searchResults);
// the while will run all email addresses
while ( $resultRowCount > 0){ //open4
echo 'emails are being sent to the following';
echo '<br>';
echo $row["user_email"];
echo '<br>';
$mail->send(array($row["user_email"]), 'smtp');
$resultRowCount--;
} //close4
echo 'emails have been sent';
} //close3
if ($group == "homeBusiness"){ //open3
$query = "SELECT user_email FROM ccMembers
WHERE user_sector='business_home_user'
ORDER BY user_id ASC ";
$searchResults = mysql_query($query) or die('error making query');
$resultRowCount = mysql_num_rows($searchResults);
$row = mysql_fetch_array($searchResults);
// the while will run all email addresses
while ( $resultRowCount > 0){ //open4
echo $row["user_email"];
$resultRowCount--;
} //close4
echo 'emails have been sent';
} //close3
if ($group == "home"){ //open3
$query = "SELECT user_email FROM ccMembers
WHERE user_sector='home_user'
ORDER BY user_id ASC ";
$searchResults = mysql_query($query) or die('error making query');
$resultRowCount = mysql_num_rows($searchResults);
$row = mysql_fetch_array($searchResults);
// the while will run all email addresses
while ( $resultRowCount > 0){ //open4
echo $row["user_email"];
$resultRowCount--;
} //close4
echo 'emails have been sent';
} //close3
if ($group == "business"){ //open3
$query = "SELECT user_email FROM ccMembers
WHERE user_sector='business_user'
ORDER BY user_id ASC ";
$searchResults = mysql_query($query) or die('error making query');
$resultRowCount = mysql_num_rows($searchResults);
$row = mysql_fetch_array($searchResults);
// the while will run all email addresses
while ( $resultRowCount > 0){ //open4
echo $row["user_email"];
$resultRowCount--;
} //close4
echo 'emails have been sent';
} //close3
if ($group == "installers"){ //open3
$query = "SELECT user_email FROM ccMembers
WHERE user_sector='installer'
ORDER BY user_id ASC ";
$searchResults = mysql_query($query) or die('error making query');
$resultRowCount = mysql_num_rows($searchResults);
$row = mysql_fetch_array($searchResults);
// the while will run all email addresses
while ( $resultRowCount > 0){ //open4
echo $row["user_email"];
$resultRowCount--;
} //close4
echo 'emails have been sent';
} //close3
} //close2
} //close1
?>
// the while will run all email addresses
while ( $resultRowCount > 0){ //open4
// in each iteration of this loop fetch the next address
$row = mysql_fetch_array($searchResults);
echo 'emails are being sent to the following';
echo '<br>';
echo $row["user_email"];
echo '<br>';
$mail->send(array($row["user_email"]), 'smtp');
$resultRowCount--;
} //close4
// without mysql_num_rows
// the while will run all email addresses
// while there is a next record to fetch
while ($row = mysql_fetch_array($searchResults)){ //open4
echo 'emails are being sent to the following';
echo '<br>';
echo $row["user_email"];
echo '<br>';
$mail->send(array($row["user_email"]), 'smtp');
} //close4