用于发送电子邮件的多个复选框逻辑
Multiple checkbox logic for sending emails
我对从一组复选框发送到选定电子邮件地址的逻辑有困难。
我有一个简单的表单,只有一个名字和一个姓氏输入,以及 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>
您的 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()
清除地址列表。
问题
因为您正在使用 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 to prevent repeated sendings of emails on page refresh. And also as be aware you can clear PHPMailer addresses。
我对从一组复选框发送到选定电子邮件地址的逻辑有困难。
我有一个简单的表单,只有一个名字和一个姓氏输入,以及 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>
您的 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()
清除地址列表。
问题
因为您正在使用 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 to prevent repeated sendings of emails on page refresh. And also as