1

Does my $to variable have to equals something in this situation or will my functions fill that in automatically.

<?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 Answers3

1

I recommend to read some documentation on how to do posts in PHP, besides that what are you trying to accomplish?

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

Will do the same thing, further more if(isset($_POST['submit'])){ should always equal true, in fact this should be your most outer if else statement. Then you're rewriting the $to variable with with '' so remove that other wise the code before it has no use-case.

Code is logic, read the code understand it and adjust your if else statements where needed.

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!');
}

Now $to will have all the addresses wanted in 1 string to be used in mail() however should think about using a library such as PHPMailer, but first focus on the basics of programming.

Xorifelse
  • 7,878
  • 1
  • 27
  • 38
  • Except I am thinking he wants to send to 3 different addresses based on those IF's and not the same one – RiggsFolly Mar 22 '18 at 15:11
  • That is true Riggs – Ryan Downes Mar 22 '18 at 15:12
  • I dont need to pipe in this situation – Ryan Downes Mar 22 '18 at 15:12
  • Oops haha I suck at this, slowly getting it though! I think, I believe @Mickael Leger has the right answer, will check this out and get back to you guys – Ryan Downes Mar 22 '18 at 15:17
  • I have updated the code appropriately, but I really recommend you reading a tutorial or 2 on how to properly handle form posting. – Xorifelse Mar 22 '18 at 15:19
  • The isset will always have an email attached to them, you would never need to kill if blank email address, unless unchecked? im guessing – Ryan Downes Mar 22 '18 at 15:22
  • @RyanDownes That fully depends on the html form, which seems to be missing in the question. If the name `MainCB` is a checkbox with no value, it only shows weather the box is checked or not, no value. If its a `text input`, it should contain the given string. – Xorifelse Mar 22 '18 at 15:26
  • I have now added the form – Ryan Downes Mar 22 '18 at 15:40
  • With the new edit you did, will I still need the last part of my code which sends the email? – Ryan Downes Mar 22 '18 at 17:26
  • Yes. I am not providing a copy paste-able code. How else would you learn? Read the code, follow the logic and adjust where necessary. I only made sure the code provided handles your question. Your edit seems to do the job, I'm just not sure what you want to say in the `$message` variable. – Xorifelse Mar 22 '18 at 23:14
  • I found a way to do what I wanted sorry for wording the question wrong I guess – Ryan Downes Mar 23 '18 at 10:05
0

According to what you said in the comment and this question : PHP send mail to multiple email addresses

You can try somehting like this to have multiple "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';
}
?>

Is it what you are looking for?

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
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"];
  • 1
    It will be better if you add some description – smartrahat Mar 23 '18 at 10:41
  • @RyanDownes instead of commenting "this is correct", wait until your answer is acceptable, check if there's any more useful/detailed answer and accept the most useful one. As for your answer, please comment it so that it's clear what the problem was and how you solved it: this way, the Q&A you created will be helpful for others. Best regards – YakovL Mar 23 '18 at 12:53
  • Welcome to Stack Overflow! While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Mar 23 '18 at 13:35
  • I was told to read a book on PHP for the answer but this just adds an email to the variable email when it is selected, and wont overwrite like all the other answers! So if you need to send to multiple Mail boxes depending on Check boxes then this will do it – Ryan Downes Mar 26 '18 at 08:35