如何在php中仅向选定用户的电子邮件发送多封电子邮件?

时间:2016-09-09 09:06:15

标签: php jquery mysql email checkbox

我正在尝试使用同一index.php页面中的复选框向我选择的用户发送电子邮件。我在这里尝试一些东西,但我不知道如何将已检查的电子邮件转移到" Bcc"领域。在这里,是我的代码请看看!

电子邮件的Php代码(index.php):

<?php
if (isset($_POST['submit'])) 
{

    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $comments = $_POST['comments'];

    $to = "";

    $headers = "From:$name<$email>";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= "Bcc: $selectedemailsall\r\n";

    $message = "Name: $name\n\n Email: $email \n\n Subject : $subject \n\n Message : $comments";
    if(mail($to,$subject,$message,$headers))
    {
        echo "Email Send";
    }
    else
    {
        echo "Error : Please Try Again !";
    }
 }
 ?>

表格代码(index.php):

 <!DOCTYPE html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Mail Document</title>
 </head>
 <body>

 <form action="" method="post" >
 <p>Name :<br>
 <input type="text" name="name" id=""></p>
 <p>Email :<br>
 <input type="text" name="email" id=""></p>
 <p>Subject :<br>
 <input type="text" name="subject" id=""></p>
 <p>Comments :<br>
 <textarea name="comments" id="" cols="30" rows="10"></textarea></p>
 <p><input type="submit" value="Send Email" name="SubmitEmail"></p>
 </form>    

 <form action="#" method="post">
 <?php
 error_reporting(E_ERROR | E_PARSE);
 $connection = mysqli_connect("localhost","root", "");
 $db = mysqli_select_db("testdb", $connection);
 $query = mysqli_query("select * from users", $connection);

 while ($row = mysqli_fetch_array($query)) 
{
 echo "
 <input type='checkbox' name='check_list[]' value='{$row['email']}'>
 <label>{$row['username']}</label><br/>
 ";
}
?>

<?php
if(isset($_POST['submituserchk']))
{
  //to run PHP script on submit
  if(!empty($_POST['check_list']))
{
  // Loop to store and display values of individual checked checkbox.
  foreach($_POST['check_list'] as $selectedemails)
  {
    $selectedemailsall = $selectedemails.",";
    //echo $selectedemailsall;
  }
 }
}
?>
</div> <!-- End of RightUsersDivWithCheckBox -->
 <input type="submit" name="submituserchk" style="margin-left: 87%; margin-top: 20px;" value="Done"/>
</form>
</body>
</html>

任何解决方案请该怎么做?现在当我点击&#34;完成&#34;并提交电子邮件没有任何反应,我不想点击&#34;完成&#34;选择电子邮件后按钮。我只选择电子邮件,然后转到&#34; Bcc&#34;变量中的字段。

2 个答案:

答案 0 :(得分:2)

不要为许多用户使用密件抄送标头。哟可以做的是:

您的表格:

...
<input type="checkbox" name="email[]" value="foo@host.tld"> - foo@host.tld
<input type="checkbox" name="email[]" value="bar@host.tld"> - bar@host.tld
<input type="checkbox" name="email[]" value="baz@host.tld"> - baz@host.tld
...

您的后端代码:

...
if (array_key_exists('email', $_POST) && is_array($_POST['email'])) {
    foreach ($_POST['email'] as $to) {
        mail($to, $subject, $message, $headers);
    }
}
...

所有收件人都单独发送所有电子邮件。这适用于您的应用程序 - 您可以检查每个发送状态。

答案 1 :(得分:0)

最后我得到了自己问题的答案。下面是我的代码,

使用数据库中的复选框显示用户名和电子邮件:

    Running migrations:
      No migrations to apply.
      Your models have changes that are not yet reflected in a migration, and so won't be applied.
      Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

电子邮件表单:

$getemail = mysqli_query("SELECT * FROM users",$connection);
if(!$getemail) die('MsSQL Error: ' . mysqli_error());
echo '<div class="AllUserDiv" style="overflow-y:scroll;height:400px;"><table class="table table-bordered">';
echo "<thead>
    <tr>
    <th><input type='checkbox' onchange='checkedbox(this)' name='chk' /> </th>
    <th>Username</th>
    <th>Email</th>
    </tr>
    </thead>";

 if(mysqli_num_rows($getemail) == 0)
 {
   echo "<tbody><tr><td colspan='3'> No Data Available</td></tr></tbody>"; 
 }

while($row = mysqli_fetch_assoc($getemail))
{
  echo "<tbody><tr><td><input value='".$row['email']."' type='checkbox' name='check[]' checked /> </td>";
  echo "<td>".$row['username']."</td>";
  echo "<td>".$row['email']."</td></tr></tbody>";   
}

用于复选框选择的JavaScript:

<form method="post" action="">
<p style="margin-top:30px;">Email Subject: <input type="text" name="subject" value="" class="form-control" /></p>
<p>Email Content: <textarea name="message" cols="40" rows="6" style="width:100%;"></textarea></p>
<center><input type="submit" name="submit" value="Send Email Now" class="btn btn-primary btn-block" />
</form>