Page 1 of 1

sending mail to members off a mySQL table

Posted: Fri Apr 04, 2003 3:17 am
by deejay
hi

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

Code: Select all

<?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.

any ideas anyone?

thanks in advance

Posted: Fri Apr 04, 2003 4:58 am
by volka

Code: Select all

<?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 */
?>
and maybe after fixing that you should read Sticky: Before Post Read: Concerning Passing Variables in PHP 4.2+. It might apply to your php-version (might be not ;) )

_

Posted: Fri Apr 04, 2003 5:37 am
by deejay
i'm not sure where that sticky topic fits in with my problem (which may be why I'm struggling so much )

I'm sure this code will do pretty much want I want

Code: Select all

<?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. :?

Cheers

Posted: Fri Apr 04, 2003 6:07 am
by volka
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)

Posted: Fri Apr 04, 2003 6:52 am
by m3mn0n
is the form pointed to the correct page? :wink:

_

Posted: Fri Apr 04, 2003 7:04 am
by deejay
the form points to its own page,that part works fine.

I've now got the form to send email and its sends the correct amount of email except only sends to 1 address 16 times!

i take it the problem is somewhere here

Code: Select all

<?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.

Code: Select all

<?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


?>

Posted: Fri Apr 04, 2003 7:48 am
by volka
:oops: i'm blind. try this one

Code: Select all

// 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
or

Code: Select all

// 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

Posted: Fri Apr 04, 2003 8:03 am
by deejay
you think your blind ?!!?
:oops: my fault completely

used the first set of code and works fine now.


i originally had

Code: Select all

<?php
$row = mysql_fetch_array($searchResults);
?>
in the while statement but took it out as i though it was making an array inside an array :oops:

Thanks

:D

Posted: Fri Apr 04, 2003 10:27 am
by deejay
dont suppose you can think of a way of sending value that can be used in my email using this method?


Cheers