Hello, I'm trying to send an email containing a variable. My variable ($to = $row_Recordset1['email'] ;) isn't working. When i echo it i get nothing and no email is received. When i put in a specific address rather than a variable it works just fine. Anyone know what I'm doing wrong?

<?php require_once('Connections/con.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_bgrotary, $bgrotary);
$query_Recordset1 = "SELECT * FROM ducks ORDER BY id DESC LIMIT 25";
$Recordset1 = mysql_query($query_Recordset1, $bgrotary) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);


            $message = '<html><body>';
            $message .= '<img src="http://website/images/header.jpg" alt="header" />';
            $message .= '<br/>' . "Hello " . $row_Recordset1['firstname'] . ",<br/><br/>" . "Below are your assigned numbers. " . "<br/>";
            $message .="<span style='color:red'>| </span>";?>
            <?php do { ?>
            <?php $message .= $row_Recordset1['id'] . "<span style='color:red'> | </span>" ; ?> 
            <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
            <?php
            $message .= "</body></html>";


            $to =  $row_Recordset1['email'] ;

            $subject = 'You Have Been Assigned Numbers';

            $headers = "From: " . $cleanedFrom . "\r\n";
            $headers .= "Reply-To: numbers@site.com";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

            if (mail($to, $subject, $message, $headers)) {
              echo 'Your message has been sent.';
            } else {
              echo 'There was a problem sending the email.';
            }
            die();

mysql_free_result($Recordset1);
?>

When i echo it i get nothing and no email is received.

Your do .. while loop continues until it returns false. So, after the loop the $row_Recordset1 contains false, and no longer an array with columns and values.

Awesome, do you have a specific suggestion i can try?

Member Avatar for Zagga

You should move line 52 (where you set $to) above the while statement. This way, it will be set before you run out of records in $row_Recordset1. (move it to between lines 44 and 45 for example)

Much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.