试图通过表单发送电子邮件

时间:2013-10-30 19:56:16

标签: php email

嘿我正在尝试创建此表单,该表单将验证电子邮件地址是否来自某个域并确保填写必填字段,然后向管理员发送电子邮件,其中包含他们需要的所有信息比如

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>

1 个答案:

答案 0 :(得分:1)

有一个简单的函数mail($to, $subject, $message);

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

但是你应该知道服务器配置是因为垃圾邮件等等......