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

时间:2018-03-22 14:56:12

标签: php variables

我的$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';
    }
?>

3 个答案:

答案 0 :(得分:1)

我建议阅读一些关于如何在PHP中发帖的文档,除了你想要完成什么?

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

将做同样的事情,更多if(isset($_POST['submit'])){应该总是等于true,实际上这应该是你最外面的if else语句。然后,您使用$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将在mail()中使用1个字符串中想要的所有地址,但是应该考虑使用PHPMailer这样的库,但首先要关注编程的基础知识

答案 1 :(得分:0)

根据您在评论和此问题中所说的内容:PHP send mail to multiple email addresses

您可以尝试这样的多个“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';
}
?>

这是你在找什么?

答案 2 :(得分:0)

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"];