vendredi 9 septembre 2016

How to send multiple emails to only selected user's emails in php?

I am trying to send emails to only the users which i am selecting using checkbox from same index.php page. i am trying something here but i don't know that how to transfer and hold checked emails to "Bcc" field. here, is my code please have a look !

Php code for email (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 !";
    }
 }
 ?>

Code for form (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>

Any solution please how to do this ? right now when i click "Done" and submit emails nothing happens and i don't want to click "Done" button after selecting emails. I just select emails and they goes to "Bcc" field in a variable.




Aucun commentaire:

Enregistrer un commentaire