0

電子メールアドレスが特定のドメインからのものであることを検証し、必須フィールドが入力されていることを確認してから、必要なすべての情報を含む電子メールを管理者に送信するこのフォームを作成しようとしています。

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

唯一の問題は、すべてが検証された後にメールを送信する方法がわからないことです...私はPHPに関しては初心者なので、これを作成するスクリプトを作成する方法がわかりません機能性..

    <!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("me@example.com", $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>
4

1 に答える 1

1

簡単な機能がありますmail($to, $subject, $message);

http://us2.php.net/manual/de/function.mail.php

ただし、スパムなどの理由でサーバー構成に注意する必要があります...

于 2013-10-30T20:29:47.967 に答える