【问题标题】:Multiple checkbox logic for sending emails用于发送电子邮件的多个复选框逻辑
【发布时间】:2019-04-09 20:40:39
【问题描述】:

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

我有一个简单的表单,只有姓名和姓氏输入,以及 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>

【问题讨论】:

  • 这对于这个问题来说绝对是很多代码......
  • 您尝试过什么调试问题?你没有调用$mail-&gt;send()一次,所以肯定涉及更多代码

标签: php phpmailer


【解决方案1】:

问题

因为您使用的是 else 语句,所以您正在强制程序逻辑选择 this OR that ,所以它只会选择它发现的第一个符合条件的复选框,这是唯一的一个

解决方案

重新设计您的代码,以便在所有有效实例中填充 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

【讨论】:

  • 很好的问题我正在寻找这样的东西。但是,当我尝试结合 if($_POST['cbone'] && $_POST['cbtwo'] && $_POST['cbthree'] &$_POST['cbfour']) { $mail->addAddress("randommailone @example.com"); $mail->addAddress("randommailtwo@example.com");它将发送电子邮件 15 次而不是 1 次。这是为什么?请为我们提供带有复选框组合的示例。谢谢 }
  • 再问一个问题
  • 感谢您的回答@Martin。我想要发生的是,当我选中复选框 1 或 2 或 3 或 4 或复选框组合时,例如复选框 1,3,4 或 1,2,4 或 2,3 或 1,3,4 发送结果仅发送到 if 语句下的电子邮件。当我尝试添加复选框组合时,它会将结果多次发送到这些特定的电子邮件,而不是一次。您能否给我一个完整的代码示例,说明您将如何做到这一点?例如,您将如何组合 if($_post['checkboxone'] && $_POST['checkboxfour']) 和其余代码?谢谢。
  • 提出一个新的单独问题或编辑您的原始问题并在底部添加详细信息。不要在 cmets 中提问。
【解决方案2】:

您的 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()清除地址列表。

【讨论】:

  • 当我尝试合并 if($_POST['cbone'] && $_POST['cbtwo'] && $_POST['cbthree'] &$_POST['cbfour']) { $mail ->addAddress("randommailone@example.com"); $mail->addAddress("randommailtwo@example.com");它将发送电子邮件 15 次而不是 1 次。这是为什么?请为我们提供带有复选框组合的示例。谢谢 }
  • 看到我对clearAddresses() 的评价了吗?大概就是这样; addAddress adds 地址,它不设置(即替换现有的)地址。我不明白你对地址框组合的意思 - 我写的已经处理了所有组合?我认为你需要更清楚你想要实现的目标 - 你现在迷失在自己的代码中。
  • 感谢您的回答@Synchro。我想要发生的是,当我选中复选框 1 或 2 或 3 或 4 或复选框组合时,例如复选框 1,3,4 或 1,2,4 或 2,3 或 1,3,4 发送结果仅发送到 if 语句下的电子邮件。当我尝试添加复选框组合时,它会将结果多次发送到这些特定的电子邮件,而不是一次。您能否给我一个完整的代码示例,说明您将如何做到这一点?例如,您将如何组合 if($_post['checkboxone'] && $_POST['checkboxfour']) 和其余代码?谢谢你。
  • 你原来说的差不多,但还是不清楚。该示例 if 语句很好,但仅匹配 16 种可能性中的一种。首先定义您的逻辑,然后担心电子邮件。
猜你喜欢
  • 2015-02-01
  • 2022-01-19
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多