在这种情况下,我的$to变量是否必须链接


在这种情况下,

我的$to变量是否必须等于某些东西,或者我的函数会自动填充它。

<?php
if (isset($_POST["MainCB"])) {
    $to = "test@test.com";
}
if (isset($_POST["ITCB"])) {
    $to = "test@test.com";
}
if (isset($_POST["CateCB"])) {
    $to = "test@test.com";
}
if (isset($_POST['submit'])) {
    $full_name = $_POST['full_name'];
    $MainCB = $_POST['MainCB'];
    $ITCB = $_POST['ITCB'];
    $CateCB = $_POST['CateCB'];
    $subject = "Form submission";
    $message = $full_name . " " . $MainCB . " " . $ITCB . " " . $CateCB;
    mail($to, $subject, $message);
    include 'mail.php';
}
?>

<html>
    <head>
        <title>Event Form</title>
        <link rel="stylesheet" type="text/css" href="form.css">
    </head>
    <body>
        <h1 id="LOGS">LOGS</h1>
        <h1 id="FormTitle">Event Request Form</h1>
        <form action="" method="post">
            <table id="Table">
                <tr id="FullName"><td>Full Name:</td> <td><input type="text" name="full_name"></td></tr>
                <tr id="EventT"><td>Event Title:</td> <td><input type="text" name="EventT"></td></tr>
                <tr id="Department"><td>Person/Dept in Charge:</td> <td><input type="text" name="InCharge"></td></tr>
                <tr id="Venue"><td>Venue:</td> <td><input type="text" name="Venue"></td><tr>
                <tr id="Ven"><td>Have you checked venue availability:</td> <td>Yes <input type="checkbox" name ="VenY">No <input type="checkbox" id="VenN" name ="VenN"></td><tr>
                <tr id="Adults"><td>No. of Adults:</td> <td><input type="text" name="Adults"></td><tr>
                <tr id="Children"><td>No. of Children:</td> <td><input type="text" name="Children"></td><tr>
                <tr id="MainCB"><td>Maintenance:</td> <td><input type="checkbox" name ="MainCB"></td><tr>
                <tr id="ITCB"><td>IT:</td> <td><input type="checkbox" name="ITCB"></td><tr>
                <tr id="CateCB"><td>Catering:</td>  <td><input type="checkbox" name="CateCB"></td><tr>
                <tr id="CatReq"><td>Catering Requirments:</td></tr>
                <tr><td><textarea rows="4" cols="50" name="CatReq"></textarea></td><tr>
                <tr id="LogReq"><td>Logistical Requirements/Equipment:</td></tr> 
                <tr><td><textarea rows="4" cols="50" name="LogReq"></textarea></td><tr>
                <tr id="ITReq"><td>IT Requirements:</td></tr> 
                <tr><td><textarea rows="4" cols="50" name="ITReq"></textarea></td><tr>
                <tr id="Trans"><td>Transport Booked:</td> <td>Yes <input type="checkbox" name ="TransY"> No <input type="checkbox" name ="TransN"></td><tr> 
                <tr id="Email"><td>Email:</td> <td><input type="text" name="Email"></td><tr>
                <tr id="EXT"><td>EXT:</td> <td><input type="text" name="Ext"></td><tr>
            </table>
            <input type="submit" name="submit" value="Submit" id="submitbutton">
        </form>
    </body>
</html> 

<?php  
    $recipients = array();
    if(isset($_POST["MainCB"])) {
        $recipients[] = "test@test.com"// one address email; 
    }
    if(isset($_POST["ITCB"])) {
        $recipients[] = "test2@test.com"// one other address email; 
    }
    if(isset($_POST["CateCB"])) {
        $recipients[] = "test3@test.com"// one more address email; 
    }
    if(isset($_POST['submit']) && !empty($recipients) ){ // You need to have at least one email address to send it
        $to = implode(',', $recipients); // All your email address
        $full_name = $_POST['full_name'];
        $MainCB = $_POST['MainCB'];
        $ITCB = $_POST['ITCB'];
        $CateCB = $_POST['CateCB'];
        $subject = "Form submission";
        $message = $full_name . " " . $MainCB . " " . $ITCB . " " . $CateCB;
        mail($to,$subject,$message);
        include 'mail.php';
    }
?>

我建议阅读一些有关如何在 PHP 中发帖的文档,除此之外,您想完成什么?

if(isset($_POST["MainCB"]) || isset($_POST["ITCB"]) || isset($_POST["CateCB"])) {
  $to = "test@test.com"; 
}

会做同样的事情,更if(isset($_POST['submit'])){应该永远等于真实,事实上这应该是你最外在的陈述。然后你用 '' 重写 $to 变量,所以在没有用例之前删除其他明智的代码。

代码

是逻辑,阅读代码理解它并在需要时调整你的 if else 语句。

if(isset($_POST["MainCB"])){
  $to[] = 'mycustomemail@address.com';
}
if(isset($_POST["ITCB"])){
  $to[] = 'mycustomemail2@address.com';
}
if(isset($_POST["CateCB"])){
  $to[] = 'mycustomemail3@address.com';
}
if(!empty($to)){
  $to = array_unique($to); // remove duplicate entry's.
  foreach($to as $address){
    if(!filter_var($address, FILTER_VALIDATE_EMAIL)){
      die("'$address' is not a valid email");
    }
  }
  $to = implode(", ", $to);
} else {
  die('No addresses to send the mail to!');
}

现在$to将拥有 1 个字符串中想要的所有地址,以便在mail()但是应该考虑使用 PHPMailer 等库,但首先关注编程的基础知识。

根据您在评论中所说的和这个问题: PHP 将邮件发送到多个电子邮件地址

您可以尝试像这样设置多个"to":

<?php  
$recipients = array();
if(isset($_POST["MainCB"])) {
    $recipients[] = // one address email; 
}
if(isset($_POST["ITCB"])) {
    $recipients[] = // one other address email; 
}
if(isset($_POST["CateCB"])) {
    $recipients[] = // one more address email; 
}
if(isset($_POST['submit']) && !empty($recipients) ){ // You need to have at least one email address to sent it
    $to = implode(',', $recipients); // All your email address
    $full_name = $_POST['full_name'];
    $MainCB = $_POST['MainCB'];
    $ITCB = $_POST['ITCB'];
    $CateCB = $_POST['CateCB'];
    $subject = "Form submission";
    $message = $full_name . " " . $MainCB . " " . $ITCB . " " . $CateCB;
    mail($to,$subject,$message);
    include 'mail.php';
}
?>

这是你要找的吗?

if(isset($_POST["MainCB"])) {
    $email = $email.  ", example@logs.uk.com";
}
if(isset($_POST["ITCB"])) {
    $email = $email. ", example@logs.uk.com";
}
if(isset($_POST["CateCB"])) {
    $email = $email. ", example@logs.uk.com";
}
$mail = $_POST["Email"];

最新更新