简体   繁体   English

如何使用 php 发送 email,其中收件人地址从 mySQL 中选择?

[英]How to send a email with php where the recipient address is selected from mySQL?

I am trying to send an email with PHP where the recipient address is selected from MySQL database.我正在尝试发送带有 PHP 的 email ,其中收件人地址是从 MySQL 数据库中选择的。

Here is what I tried:这是我尝试过的:

// Create connection
  $DBConnect = new mysqli($servername, $username, $sqlpassword, $database);

// Check connection
if ($DBConnect->connect_error) {
  die("Connection failed: " . $DBConnect->connect_error);
}

  $to = "SELECT email_address FROM customer WHERE id = '$customerNum'";
  $subject = "Hello";
  $message = "Thank you!";
  $headers = "From: xxx@xxx.com";

  mail($to,$subject,$message,$headers);


mysqli_close($DBConnect);

After serval tries, I manage to get it to work.薮猫尝试后,我设法让它工作。 Please forgive my ignorance.请原谅我的无知。 I am still a beginner learner.我还是一个初学者。

 $sql = "SELECT email_address FROM customer WHERE customer_number = $customerNum";
 $result = $DBConnect->query($sql);

  if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    $to = $row["email_address"];
  }
} 
  $subject = "Hello";
  $message = "Thank you!";
  $headers = "From: xxx@xxx.com";


  mail($to,$subject,$message,$headers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM