-1

I am having difficulties with the logic of sending to selected email addresses from a set of checkboxes.

I have a simple form with just a name and a surname inputs, and 4 checkboxes.

What I would like to do is when one of the checkboxes is checked or if multiple checkboxes are checked, to send the emails that those checkboxes represent. Depending of what checkboxes are checked to send to those specific emails only.

For example I have 4 emails, and if the first checkbox is checked to send to those emails. If checkbox 1 and checkbox 2 are checked to send to another emails or if checkboxes 2,3,4 are checked to send to another emails.

My code is working if I check only one checkbox.

If I check more than one, for example checkbox 1,3,4 it will send the emails only to the checkbox 1 emails, it will not proceed to the third one checkbox emails or the the fourth checkbox emails. I will ignore them.

Hopefully someone can help me solve this issue.

Thanks.

Here is my PHP code

<?php

use PHPMailer\PHPMailer\PHPMailer;
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";

if(isset($_POST['submit'], $_POST["cbone"])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->setFrom($email);
//$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


} else if(isset($_POST['submit'], $_POST['cbtwo'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbthree'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->addAddress("randomemailthree@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbfour'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->addAddress("randomemailthree@example.com");
$mail->addAddress("randomemailfour@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbone'], $_POST['cbtwo'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbone'], $_POST['cbthree'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbone'], $_POST['cbfour'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailfour@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbone'], $_POST['cbtwo'], $_POST['cbthree'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailthree@example.com");
$mail->addAddress("randomemailfour@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbone'], $_POST['cbtwo'], $_POST['cbthree'], $_POST['cbfour'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailfour@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbtwo'], $_POST['cbthree'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbtwo'], $_POST['cbfour'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailtwo@example.com");
$mail->addAddress("randomemailthree@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbthree'], $_POST['cbfour'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailtwo@example.com");
$mail->addAddress("randomemailfour@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 


}else if(isset($_POST['submit'], $_POST['cbtwo'], $_POST['cbthree'], $_POST['cbfour'])) {

$name   = $_POST['name'];
$surname  = $_POST['surname'];


$mail = new PHPMailer();
$mail->addAddress("randomemailone@example.com");
$mail->addAddress("randomemailthree@example.com");
$mail->setFrom($email);
$mail->isHTML(true);
$mail->Body = "
<p><strong>Name:</strong> $name</p> 
<p><strong>Surname:</strong> $surname</p>"; 

}else {
    echo "Email was not send"; 
}

?>

Form Code

<form method="post" action="">
    <div class="form-group">
        <input type="text" name="name" class="form-control" placeholder="Name">
    </div>
    <div class="form-group">
        <input type="text" name="surname" class="form-control" placeholder="Surname">
    </div>
    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="cbone" value="checkone" id="checkone">
        <label class="form-check-label" for="checkone">
            Checkbox One
        </label>
    </div>
    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="cbtwo" value="checktwo" id="checktwo">
        <label class="form-check-label" for="checktwo">
            Checkbox Two
        </label>
    </div>
    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="cbthree" value="checkthree" id="checkthree">
        <label class="form-check-label" for="checkthree">
            Checkbox Three
        </label>
    </div>
    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="cbfour" value="checkfour" id="checkfour">
        <label class="form-check-label" for="checkfour">
            Checkbox Four
        </label>
    </div>
    <input type="submit" name="submit" class="btn btn-primary" value="Send Email">
</form>
Synchro
  • 35,538
  • 15
  • 81
  • 104
NoName84
  • 407
  • 3
  • 12
  • 25

2 Answers2

3

Problem

Because you are using else statements you are forcing the program logic to choose this OR that, so it will only choose the first checkbox it discovers fits the criteria, and that's the only one.

Solution

Redesign your code so that in all valid instances the PHPMailer object is populated, and then add receivers as set by the form data (in this case, checkboxes):

if(isset($_POST['submit']) ){
    /***
     * Build mailer object with parts that are always true
     ***/

    $mail = new PHPMailer();
    $mail->setFrom($email);
    $mail->isHTML(true);
    $mail->Body = "
       <p><strong>Name:</strong> $name</p> 
       <p><strong>Surname:</strong> $surname</p>"; 

    /***
     * Check for IF clauses based on form data
     ***/
    if($_POST['cbone']) {
        $mail->addAddress("randomemailone@example.com");
        $mail->addAddress("randomemailtwo@example.com");
        ...etc....
    }
    if($_POST['cbtwo']) {
        $mail->addAddress("randomemailthree@example.com");
        ...etc....
    }
    if($_POST['cbthree']) {
        $mail->addAddress("randomemailthree@example.com");
        ...etc....
    }

    /***
     * finally, send mail
     ***/
    $mail->send(); 
}

This means that your code now checks every condition rather than only the first condition that is true.

I would also recommend using a CSRF token to prevent repeated sendings of emails on page refresh. And also as referenced by Synchro be aware you can clear PHPMailer addresses before sending.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • Great question I was looking something like this. However, when I try to combine if($_POST['cbone'] && $_POST['cbtwo'] && $_POST['cbthree'] &$_POST['cbfour']) { $mail->addAddress("randomemailone@example.com"); $mail->addAddress("randomemailtwo@example.com"); it will send the email 15 times instead of one. Why is that? Please provide us examples with checkbox combinations. Thanks } – NoName84 Nov 06 '18 at 15:29
  • Ask another question – Martin Nov 06 '18 at 16:09
  • Thank you for ur answer @Martin. What I would like to have happen is that when I check checkbox 1 or 2 or 3 or 4 or combinations of checkboxes for example checkbox 1,3,4 or 1,2,4 or 2,3 or 1,3,4 to send the results only to the emails that are under the if statement. When I try to add combination of checkboxes it will send the results many times to those specific emails instead of once. Can you please give me a full example of code of how you would do it? For example how you would combine if($_post['checkboxone'] && $_POST['checkboxfour']) and then the remaining code? Thank you. – NoName84 Nov 06 '18 at 17:45
  • Ask a new separate question or edit your original question and *add* details at the bottom. Do not ask questions in comments. – Martin Nov 06 '18 at 22:19
1

The logic of your if statements is incorrect. You're doing this (in pseudocode):

if (checkbox 1) {
  //send email 1
} elseif (checkbox 2) {
  //send email 2
} elseif (checkbox 3) {
  //send email 3
} elseif (checkbox 4) {
  //send email 4
}

Only one of these will ever be sent. If you want to handle any combination of the checkboxes independently, you should structure it like this:

if (checkbox 1) {
  //send email 1
}
if (checkbox 2) {
  //send email 2
}
if (checkbox 3) {
  //send email 3
}
if (checkbox 4) {
  //send email 4
}

You don't need to create a new PHPMailer instance every time; Create one instance at the beginning, set paras that are common to all, then re-use it for each one, clearing the address list with clearAddresses() before you use it each time.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • When I try to combine if($_POST['cbone'] && $_POST['cbtwo'] && $_POST['cbthree'] &$_POST['cbfour']) { $mail->addAddress("randomemailone@example.com"); $mail->addAddress("randomemailtwo@example.com"); it will send the email 15 times instead of one. Why is that? Please provide us examples with checkbox combinations. Thanks } – NoName84 Nov 06 '18 at 15:35
  • See what I said about `clearAddresses()`? It's probably that; `addAddress` **adds** addresses, it doesn't set (i.e. replace existing) addresses. I don't see what you mean about address box combinations - what I wrote already handles all combinations? I think you need to be clearer about what it is you're trying to achieve - you're getting lost in your own code at the moment. – Synchro Nov 06 '18 at 15:39
  • Thank you for ur answer @Synchro. What I would like to have happen is that when I check checkbox 1 or 2 or 3 or 4 or combinations of checkboxes for example checkbox 1,3,4 or 1,2,4 or 2,3 or 1,3,4 to send the results only to the emails that are under the if statement. When I try to add combination of checkboxes it will send the results many times to those specific emails instead of once. Can you please give me a full example of code of how you would do it? For example how you would combine if($_post['checkboxone'] && $_POST['checkboxfour']) and then the remaining code? Thanks you. – NoName84 Nov 06 '18 at 17:43
  • That’s pretty much what you said originally, but it’s still not clear. That example if statement is fine, but only matches a single possibility out of 16. Define your logic first, then worry about email. – Synchro Nov 07 '18 at 10:01