1

I'm trying to create a php script to send a filled form from a website to a specific email. I tried several ways but I don't get the email on my webmail. I also don't get any errors so I am not sure where the problem is. The files are in the same directory and the webmail seems to work well. I would be grateful for your input.

This is email.php file:

<?php
$error = ""; $successMessage = "";
        
if ($_POST) {
    if (!$_POST["name"]) {
        $error .= "A name is required.<br>";
    }
    if (!$_POST["email"]) {
        $error .= "An email address is required.<br>";
    }
                
    if (!$_POST["choice"]) {
        $error .= "Select a trial session.<br>";
    }
    if (!$_POST["date"]) {
        $error .= "Select a date.<br>";
    }
                
    if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {                
        $error .= "The email address is invalid.<br>";
    }
                
    if ($error != "") {
                    
        $error = '<div class="alert alert-danger" role="alert"><p>There were error(s) in your form:</p>' . $error . '</div>';
                    
    } else {
                    
        $emailTo = "my@addressemail.com";
        $subject = "trial session";
        $name = $_POST['name'];
        $choice = $_POST['choice'];
        $date = $_POST['date'];
        $headers = "From: ".$_POST['email'];
    
        if (mail($emailTo, $subject, $name, $choice, $date, $headers)) {
            $successMessage = '<div class="alert alert-success" role="alert">Your message was sent, we\'ll get back to you ASAP!</div>';
        } else {
            $error = '<div class="alert alert-danger" role="alert"><p><strong>Your message couldn\'t be sent - please try again later</div>';
        }
    }
}
?>


<div class="container">
    <h1>Get in touch!</h1>
          
    <div id="error"><? echo $error.$successMessage; ?></div>
          
        <form method="post" action=”email.php”>
            <fieldset class="form-group">
                <label for="name">Name</label>
                <input type="text" class="form-control" id="name" name="name" >
            </fieldset>
            <fieldset class="form-group">
                <label for="email">Email address</label>
                <input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
                <small class="text-muted">We'll never share your email with anyone else.</small>
            </fieldset>
            <div class="mb-3">
                <div class="control-label" id="choice" name="choice">Which trial session you would like to book?</div>
                    <select class="form-select" required aria-label="select example" id="choice" name="choice">
                        <option value="">I'm interested in...</option>
                        <option value="1">option1</option>
                        <option value="2">option2</option>
                    </select>
                </div>
                <div class="form-group"> <!-- Date input -->
                    <label class="control-label" for="date">Date</label>
                    <input class="form-control" id="date" name="date" placeholder="MM/DD/YYY" type="text"/>
                </div>
                <button type="submit" id="submit" class="btn btn-primary">Submit</button>
            </form>
        </div>


RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
student24
  • 252
  • 1
  • 9
  • Do you have an email server installed on this system? And configured in the php.ini file? – RiggsFolly Sep 19 '21 at 15:31
  • 1
    Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Sep 19 '21 at 15:36
  • 1
    Beware of, and remove non-ascii quotes like this one `”` – RiggsFolly Sep 19 '21 at 15:37
  • You have a HTML layout issue, visible now the code is readable – RiggsFolly Sep 19 '21 at 15:38
  • @RiggsFolly thank you for your input. I changed the "" and now after filling up the form I get this message: Not Found The requested URL was not found on this server. – student24 Sep 19 '21 at 15:40
  • that will likely be when the form calls `email.php`. Is it in the same directory as the form script? – RiggsFolly Sep 19 '21 at 15:45
  • yes, email.php is in the same directory as the form script – student24 Sep 19 '21 at 15:48
  • Then like all error messages! Please show it ALL, not a summary – RiggsFolly Sep 19 '21 at 15:54
  • two errors were found. One: Undefined variable $error , second: Undefined variable $successMessage – student24 Sep 19 '21 at 16:09
  • What did I just say, **Please show ALL the message** Not a summary that leaves off useful information – RiggsFolly Sep 19 '21 at 16:19
  • I'm sorry the server shows just that and a line which seems to be this one:
    echo $error.$successMessage; ?>
    . Unfortunately, nothing else is shown.
    – student24 Sep 19 '21 at 16:24

1 Answers1

0

By default, the server cannot send an email. You have to set up for it an mail server. And as my experience, most of the time we must use the SMTP server to send the email.

You can check the way to send email via SMTP in PHP here:

Sending email with PHP from an SMTP server

You also have to set up your email by allow less secure apps:

https://kb.synology.com/en-global/SRM/tutorial/How_to_use_Gmail_SMTP_server_to_send_emails_for_SRM

Tuan Dao
  • 2,647
  • 1
  • 10
  • 20