vendredi 1 avril 2022

How to display all selected Checkboxes in PHPmailer

I was tying to create a checkbox and get all checkboxes that were selected and ues phpmailer to email them to my email. Apparently i can get only 1 value selected from checkboxes out of all selected ones. Sorry if i make ur brain suicide from these indents xd This is my html file :

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Send Mail</title>
</head>
<body>
<h3> Send Email using SMTP </h3>
    <form action="mail.php" method="post">
<label for="cfselection-0">
      <input name="cfselection" id="cfselection-0" value="website design type="checkbox">
      Website Design
    </label>
    </div>
  <div class="checkbox">
    <label for="cfselection-1">
      <input name="cfselection" id="cfselection-1" value="search engines" type="checkbox">
      Search Engines and Ranking
    </label>
    </div>
  <div class="checkbox">
    <label for="cfselection-2">
      <input name="cfselection" id="cfselection-2" value="media marketing" type="checkbox">
      Social Media Marketing and Campaigns
    </label>
    </div>
  <div class="checkbox">
    <label for="cfselection-3">
      <input name="cfselection" id="cfselection-3" value="other" type="checkbox">
      Other
    </label>
    </div>
  </div>
</div>
        <button type="submit"> Dërgo </button>
    </form>


</body>
</html>

and this is my php file :

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//get data from form
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$checkbox = $_POST['cfselection'];
// preparing mail content
$messagecontent ="Name = ". $name . "<br>Email = " . $email . "<br>Message =" . $message . "<br>Checkbox =" . $checkbox;


//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'xxxxxxxxxxxxx@gmail.com';                     //SMTP username
    $mail->Password   = 'xxxxxxxxxxxxxxx';                               //SMTP password
    $mail->SMTPSecure = "tls";            //Enable implicit TLS encryption
    $mail->Port       = 587;

    //Recipients
    $mail->setFrom('xxxxxxxxx@gmail.com');
    $mail->addAddress('xxxxxxxx@gmail.com' );     //Add a recipient
    //$mail->addAddress('ellen@example.com');               //Name is optional
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    //Attachments

    //$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
   // $mail->addAttachment('photo.jpeg', 'photo.jpeg');    //Optional name

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = "blabla";
    $mail->Body    = $messagecontent;
    

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    // echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}



Aucun commentaire:

Enregistrer un commentaire