Jump to content

Recommended Posts

Hello all,

 

First time I'm attempting to use a foreach statement and was just wondering if my code is correct:

 

$sql = "SELECT email from mail_list where subcribed = \"Y\" ";

			$rs = mssql_query( $sql, $conn )
			or die( "Err EXE" );
			$row = mssql_fetch_array( $rs );

			foreach($row["email"] as $email)
			{
			$to = "$email";
				$re = "Newsletter";
				$msg = "The latest Newsletter is ready for download \n\nThank You";
				$headers = "From: No-Reply@doamin.com \r\n";
				}

 

Hard to test as I dont want to start sending out rouge emails

 

Many Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/178332-solved-foreach-help/
Share on other sites

mssql_fetch_array returns one record as an array of fields then moves the cursor to the next record. So your foreach will return one record only. You need a while loop.

 

$sql = "SELECT email from mail_list where subcribed = \"Y\" ";
if ($rs = mssql_query( $sql, $conn )) {
  if (mssql_num_rows($rs)) {
    $re = "Newsletter";
    $msg = "The latest Newsletter is ready for download \n\nThank You";
    $headers = "From: No-Reply@doamin.com \r\n";
    while ($row = mssql_fetch_array($rs)) {
      $to = $row['$email'];
      // call mail()
    }
  }
}

Link to comment
https://forums.phpfreaks.com/topic/178332-solved-foreach-help/#findComment-940334
Share on other sites

Thanks Thorpe.

I have added your code but I'm worried it will try and insert the record into the published table and send a confirmation email to the uploader for each subscriber found?

 

Heres codes as it is now

 

<?php 
$newsletter_id = $_POST['newsletter_id'];
$uplaoder = $_POST['admin_name'];
$sent = $_POST['sent'];

include("../php_include/connection.php");	

if($sent)
{ 
$valid=true; 
}

if( $valid != true)
{
echo( "Error - Data not sent from main form." );
}
else 
{ 
$sql = "SELECT email from mail_list where subcribed = \"Y\" ";
if ($rs = mssql_query( $sql, $conn )) 
	{
  			if (mssql_num_rows($rs)) 
			{
    			$re = "Newsletter";
    			$msg = "The latest Newsletter is ready for download \n\nThank You";
    			$headers = "From: No-Reply@doamin.com \r\n";
    			while ($row = mssql_fetch_array($rs)) 
				{
      				$to = $row['$email'];
    				}
  				}
	}

if( mail( $to, $re, $msg, $headers ) )
	{
		$sql2 = "insert into published (newsletter_id, published_by) values (\"$newsletter_id\", \"$uploader\") ";
		$rs2 = mssql_query( $sql2, $conn )
			or die( "Err EXE 2");

		$to = "$uploader@northamptoncollege.com";
		$re = "Newsletter Email Sent";
		$msg = "Distribution confirmed \n\nThank You";
		$headers = "From: No-Reply@domain.com \r\n";

		if( mail( $to, $re, $msg, $headers ) )
			{			
				echo("Thank you, An email has been sent to all subcribers plus confirmation to yourself.<br><form action=\"published_newsletters.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>");
			}
			else
			{
				echo("Error.....Email not sent to Uploader. <br>Please go back and check the record has been saved, if so emails have been sent to subscribers.<br><form action=\"published_newsletters.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>");
			}
	}
	else
	{
		echo("Error.....Email not sent and record not saved. <br>Please go back and try again.<br><form action=\"send_newsletter.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>");
	}
}
?>

 

and before, which would only send to 1 subscriber.

 

<?php 
$newsletter_id = $_POST['newsletter_id'];
$uplaoder = $_POST['admin_name'];
$sent = $_POST['sent'];

include("../php_include/connection.php");	

if($sent)
{ 
$valid=true; 
}

if( $valid != true)
{
echo( "Error - Data not sent from main form." );
}
else 
{ 
$sql = "SELECT email from mail_list where subcribed = \"Y\" ";
$rs = mssql_query( $sql, $conn )
	or die( "Err EXE" );

$row = mssql_fetch_array( $rs );

$email = $row["email"];

$to = "$email";
$re = "Newsletter";
$msg = "The latest Newsletter is ready for download \n\nThank You";
$headers = "From: No-Reply@domain.com \r\n";

if( mail( $to, $re, $msg, $headers ) )
	{
		$sql2 = "insert into published (newsletter_id, published_by) values (\"$newsletter_id\", \"$uploader\") ";
		$rs2 = mssql_query( $sql2, $conn )
			or die( "Err EXE 2");

		$to = "$uploader@domain.com";
		$re = "Newsletter Email Sent";
		$msg = "Distribution confirmed \n\nThank You";
		$headers = "From: No-Reply@domain.com \r\n";

		if( mail( $to, $re, $msg, $headers ) )
			{			
				echo("Thank you, An email has been sent to all subcribers plus confirmation to yourself.<br><form action=\"published_newsletters.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>");
			}
			else
			{
				echo("Error.....Email not sent to Uploader. <br>Please go back and check the record has been saved, if so emails have been sent to subscribers.<br><form action=\"published_newsletters.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>");
			}
	}
	else
	{
		echo("Error.....Email not sent and record not saved. <br>Please go back and try again.<br><form action=\"send_newsletter.php\"><input type=\"submit\" name=\"back\" value=\"Back\"></form>");
	}
}
?>

 

I hope I'm making sense.

 

Many Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/178332-solved-foreach-help/#findComment-940388
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.