首页 > 解决方案 > 使用 php,mysql 从数据库中的电子邮件列表发送电子邮件

问题描述

我正在尝试向存储在数据库中的所有成员电子邮件 ID 发送电子邮件。这是代码

<?php
$sql = mysqli_query($con,"select email from email_list where email");
//$recipients = array();
while($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)) {
    $recipients[]= $row['email'];
}

$to = $recipients;
$repEmail = 'abc@gmail.com';
$subject = "$title";
$body = "$description";
$headers = 'From: xyz <'.$repEmail.'>'. "\r\n"; 
$headers .= 'Reply-To: abc@gmail.com' . "\r\n";
$headers .= 'BCC: ' . implode(', ', $recipients) . "\r\n";

   if (mail($to, $subject, $body, $headers)){
     echo "<script>alert('Email sent')</script>";
    }

    else {
    echo "<script>alert('Email failed')</script>";
    }

?>

在上面的代码中,电子邮件发送给 1 个人。

标签: phpmysql

解决方案


您的 $to 应该是 $to = "address@one.com, address@two.com, address@three.com"

所以添加

$recipients = array(
  "youremailaddress@yourdomain.com",
  // more emails here
);
$email_to = implode(',', $recipients); // your email address

将 $email_to 添加到您的 $to。


推荐阅读