使用PHP向所有插入MYSQL数据库的电子邮件地址发送电子邮件

时间:2016-04-20 10:12:26

标签: php mysql email

我想向我的订阅者发送简报。我的订阅者的电子邮件地址被插入到MYSQL数据库中。

以下编码我用来发送。

<?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);
}
?>

但这不起作用。有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

尝试这样的事情。我假设您的代码适用于一封电子邮件。

if ($result->num_rows > 0){
   $email_array = array();
   while($row = $result->fetch_assoc()){
       $email_array[] = $row["email"];
   }
}
$emails = implode(",",$email_array);

答案 1 :(得分:0)

尝试类似的东西:

 <?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();


 }
 ?>