【发布时间】: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->send()一次,所以肯定涉及更多代码