Sal Z.
Sal Z.

Reputation: 39

Trying to send email through form

Hey I am trying to create this form that will validate that the email address is from a certain domain and make sure that the required fields are filled in, and then send an email to the admin that has all the information they would need such as

Subject: New Server Request

Message: You have a new server request from the user: ________
The Primary need for this server is for ___________
Comments: ____________
please email them at _________ once the request has been fulfilled
with the necessary information

the only problem is I can't figure out how I should go about sending the email after everything is validated...I am sort of a noob when it comes to php so i have no idea how to create a script that would create this functionality..

    <!DOCTYPE HTML> 
    <html>
    <head>
    <style>
    .error {color: #FF0000;}
    </style>
    </head>
    <body> 

    <?php
    // define variables and set to empty values
    $usernameErr = $emailErr = $servErr  = "";
    $username = $email = $serv = $comment  = "";
    //change at the discretion of what domains you want to allow as such::
    //$allowed_domain    = array('domain.com', 'domain2.com', 'domain3');

    $allowed_domain = array('domain.com');
    $subject = "New Request";


    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
    //checks to see if username has been inputted
      if (empty($_POST["username"]))
        {$usernameErr = "username is required";}
      else
        {$username = test_input($_POST["username"]);}
    //checks to see if email has been inputted   
      if (empty($_POST["email"]))
        {$emailErr = "Email is required";}
      else
        {$email = test_input($_POST["email"]);}

    //checks to see if it is a email address from the  $allowed_domain variable above
    if (filter_var($email, FILTER_VALIDATE_EMAIL))
        {
            $domain = array_pop(explode('@',$email));

            if ( ! in_array($domain, $allowed_domain))
            {
            $emailErr = "Must be a valid email address";
            }
        }

   if (empty($_POST["comment"]))
     {$comment = "";}
   else
     {$comment = test_input($_POST["comment"]);}
//tests to see if the need for the server has been selected
   if (empty($_POST["serv"]))
     {$servErr = "Need for server is required";}
   else
     {$serv = test_input($_POST["serv"]);}


    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
        {
        //send email
        $email = $_REQUEST['email'] ;
        $message = $_REQUEST['serv'];
        mail("[email protected]", $subject, $message, "From:" . $email);
            if($send_contact)
            {
                header('Location:./sent.php');
            }
            else 
            {
            echo "ERROR";
            }
        }
}

function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>

<

    h2>Request Form</h2>
    <p><span class="error">* required</span></p>
    <form name= " " method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
        Username: <input type="text" name="username">
       <span class="error">* <?php echo $usernameErr;?></span>
       <br><br>
        E-mail:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="email">
       <span class="error">* <?php echo $emailErr;?></span>
       <br><br>
       Primary need:
       <input type="radio" name="serv" value="personal">Personal
       <input type="radio" name="serv" value="class">Class
       <span class="error">* <?php echo $servErr;?></span>
       <br><br>
       Comment:<br /> <textarea name="comment" rows="5" cols="40"></textarea>
       <br><br>

       <input type="submit" name="submit" value="Submit Request"> 
       <input type="reset" name="reset" value="Reset">
    </form>


    <?php

    //test output to show what it collects from form
    print "<h2>Output:</h2>";
    print "You have a new request from the user: ";
    print $username;
    print "<br /><br />";
    print "Please email the user at: ";
    print $email;
    print " when the request has been fulfilled.";
    print "<br /><br />";
    print $serv;
    print "<br />";
    print $comment;
    ?>

    </body>
    </html>

Upvotes: 0

Views: 78

Answers (1)

Pateta
Pateta

Reputation: 429

There a simple function mail($to, $subject, $message);

https://www.php.net/manual/de/function.mail.php

But you should be aware of server configurations because of spam and so...

Upvotes: 1

Related Questions