我使用php编写了以下代码,从我的数据库表向所有用户发送电子邮件。但它无法发送任何电子邮件。其他一切都运作良好。 我的代码有问题吗?
$message = 'How are you?';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <gph.payment@gmail.com>' . "\r\n";
$subject = "HI";
$sql = "SELECT Email FROM test";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$to = $row['Email'];
mail($to,$subject,$message,$headers);
echo $to."<br>";
}
} else {
echo "0 results";
}
$conn->close();
但如果我写的,它运作良好,邮件已被发送。
$message = 'How are you?';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <gph.payment@gmail.com>' . "\r\n";
$subject = "HI";
$to = "z2228391@gmail.com";
mail($to,$subject,$message,$headers);