$row["email"] not pulling from my SELECT statement into phpmailer field

Viewed 22

I am using a select statement to pull data from my DB using a where clause and limited to 1, then i am trying to put the addresses it has pulled into the sendto field on phpmailer.

I have updated the question so the full code is here, i am still unable to pull up the echo from the var that i entered to collect the emails from the list.

<?php
$servername = "localhost";
$username = "####";
$password = "####";
$dbname = "####";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";



$sql = "SELECT email FROM emails WHERE status = 'new' LIMIT 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["email"]. " ";
  }
} else {
  echo "0 results";
}


$sql = "UPDATE emails SET status='done' WHERE status = 'new' LIMIT 1";

if ($conn->query($sql) === TRUE) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . $conn->error;
}


 $var_email = $row["email"]; 


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';
    Class SendMail{
        function send(){
            
            $mail = new PHPMailer(true);
            //Server settings
            $mail->SMTPDebug = SMTP::DEBUG_SERVER;;                  //Enable verbose debug output
            $mail->isSMTP();                                            //Send using SMTP
            $mail->Host       = '####';                     //Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
            $mail->Username   = '####';                     //SMTP username
            $mail->Password   = '####';   
               $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;   
    
            //Recipients
          $mail->setFrom('info@####', '####');
         $mail->addAddress($var_email);     
               

    
            //Content
            $mail->isHTML(true);                                  //Set email format to HTML
            $mail->Subject = '####';
            $mail->Body    =   "####";
    
            $mail->send();
            
          
            echo 'Message has been sent';
        }
        
    }
    
    
        
        $sendMail = new SendMail();
        $sendMail->send();
        
$conn->close();



   
?>

I am hoping its a simple fix, but i feel like somewhere i have put the wrong string which is not allowing me to echo the results into the address field.

any suggestions?

1 Answers

take a variable and assign value to it

ex. $var_email = $row["email"]; $mail->addAddress($var_email);

try it it's working. problem is occur because of php version

Related