0

I want to send newsletter to my subscribers. The email adress of my subscribers are inserted into MYSQL database.

This below coding i am using to send.

<?php if(isset($_POST['send'])){
$servername = "localhost";
$username = "#";
$password = "#";
$dbname = "db_table";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT ALL email FROM db_table";
$result = $conn->query($sql);

if ($result->num_rows > 0) {while($row = $result->fetch_assoc()){
$emails = $row["email"];
}
} else {
}
$conn->close();

$email_to = "$emails";
$headers = "From:" . "noreply@website.in";
$subject = "Newsletter";
$message = 'Hello There,

This is and Test email.

Thank you';
mail($email_to,$subject,$message,$headers);
}
?>

But this is not working. Any help?

Rahul
  • 23
  • 6

2 Answers2

0

Try something like this. I am assuming your code is working for one email.

if ($result->num_rows > 0){
   $email_array = array();
   while($row = $result->fetch_assoc()){
       $email_array[] = $row["email"];
   }
}
$emails = implode(",",$email_array);
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0

Try something like that:

 <?php if(isset($_POST['send'])){
 $servername = "localhost";
 $username = "#";
 $password = "#";
 $dbname = "db_table";

 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
 }

 $sql = "SELECT email FROM db_table";
 $result = $conn->query($sql);

 if ($result->num_rows > 0) {while($row = $result->fetch_assoc()){

      $email_to = $row["email"];
      $headers = "From:" . "noreply@website.in";
      $subject = "Newsletter";
      $message = 'Hello There,

      This is and Test email.

      Thank you';
      mail($email_to,$subject,$message,$headers);

 }
 } else {
 }
 $conn->close();


 }
 ?>
ryrysz
  • 907
  • 5
  • 11