簡體   English   中英

如何使用 php 發送 email,其中收件人地址從 mySQL 中選擇?

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

我正在嘗試發送帶有 PHP 的 email ,其中收件人地址是從 MySQL 數據庫中選擇的。

這是我嘗試過的:

// 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);

藪貓嘗試后,我設法讓它工作。 請原諒我的無知。 我還是一個初學者。

 $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