用于发送电子邮件的多个复选框逻辑

时间:2018-11-06 12:42:58

标签: php phpmailer

我很难从一组复选框发送到选定的电子邮件地址的逻辑上。

我有一个简单的表单,只有一个名称和一个姓氏输入,以及4个复选框。

我想做的是选中一个复选框或选中多个复选框时,发送那些复选框代表的电子邮件。取决于选中哪个复选框仅发送到那些特定的电子邮件。

例如,我有4封电子邮件,并且是否选中了第一个复选框以发送到这些电子邮件。如果选中了复选框1和复选框2以发送给另一封电子邮件,或者选中了复选框2、3、4以发送给另一封电子邮件。

如果仅选中一个复选框,我的代码将正常工作。

如果我选中多个复选框(例如复选框1,3,4),它将仅将电子邮件发送到复选框1电子邮件,它将不会继续处理第三个复选框电子邮件或第四个复选框电子邮件。我会忽略它们。

希望有人可以帮助我解决这个问题。

谢谢。

这是我的PHP代码

<?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 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>

2 个答案:

答案 0 :(得分:3)

问题

由于您使用else语句,因此迫使程序逻辑选择 this OR 那个,因此它将仅选择它发现的符合条件的第一个复选框,这是唯一的

解决方案

重新设计代码,以便在所有有效实例中填充PHPMailer对象,然后按照表单数据(在此情况下为复选框)设置添加接收者:

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(); 
}

这意味着您的代码现在检查所有条件,而不是仅检查第一个为真的条件。

我还建议您使用CSRF token来防止在页面刷新时重复发送电子邮件。另外,referenced by Synchro也请注意,clear PHPMailer addresses可以在发送前进行。

答案 1 :(得分:1)

if语句的逻辑不正确。您正在执行此操作(使用伪代码):

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

将仅发送其中之一。如果要独立处理复选框的任何组合,则应按以下方式进行构造:

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

您不需要每次都创建一个新的PHPMailer实例;首先创建一个实例,设置所有实例共有的参数,然后对每个实例重新使用它,并在每次使用前用clearAddresses()清除地址列表。