在表格提交上发送电子邮件

时间:2016-08-23 11:48:06

标签: php html

我正在使用这个PHP代码在他们提交表单时收到用户反馈,但遗憾的是它无效。 请帮我这个代码。

/ ********************* HTML代码*********************** /

                   <div class="contact-form bottom">
                    <h2><b>Send a message</b></h2>
                    <form id="main-contact-form" name="contact-form" method="post" action="sendemail.php" class="form-horizontal">
                        <div class="form-group">
                        <span class="col-sm-1"><i class="fa fa-user bigicon"></i></span> 
                        <div class="col-sm-11">
                            <input type="text" name="name" class="form-control" required placeholder="Name">
                        </div>
                        </div> 
                        <div class="form-group">
                        <span class="col-sm-1 text-center"><i class="fa fa-envelope bigicon"></i></span>
                        <div class="col-sm-11">
                            <input type="email" name="email" class="form-control" required placeholder="Email Id">
                        </div>
                        </div>
                        <div class="form-group">
                        <span class="col-sm-1 text-center"><i class="fa fa-phone-square bigicon"> &nbsp;</i></span>
                        <div class="col-sm-11">
                            <input id="phone" name="phone" type="text" placeholder="Phone" class="form-control" required>
                        </div>
                    </div>
                        <div class="form-group">
                        <span class="col-sm-1 text-center"><i class="fa fa-pencil-square-o bigicon"></i></span>
                        <div class="col-sm-11">
                            <textarea name="message" id="message" required class="form-control" rows="8" placeholder="Enter your massage for us here. We will get back to you within 24 Hours."></textarea>
                        </div>  
                        </div>                      
                        <div class="form-group">
                            <input type="submit" name="submit" class="btn btn-submit" value="Submit">
                        </div>
                    </form>
                </div>

/ ********************** PHP代码********************** /

            <?php
             if (isset($_POST["submit"])) {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $email = $_POST['phone'];
            $message = $_POST['message'];
            $from = 'Contact Form'; 
           $to = 'someone@gmail.com'; 
        $subject = 'Message from ';     
    $body = "From: $name\n E-Mail: $email\n Message:\n $message\n Phone: $phone";

    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your Name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid Email address';
    }

    // Check if name has been entered
    if (!$_POST['phone']) {
        $errName = 'Please enter your Phone Number';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your Message';
    }

            // If there are no errors, send the email
           if (!$errName && !$errEmail && !$errMessage && !$errPhone) {
          if (mail ($to, $subject, $body, $from)) {
               $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
             $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again</div>';
            }
               }
                }
         ?>

先谢谢

1 个答案:

答案 0 :(得分:-1)

<?php
class user_alert {
    public $_array = array();

    function add($type = "info", $input){
        $this->_array[] = array($type,$input);
    }
    function get(){
        // remove duplicates
        $this->_array = array_map("unserialize", array_unique(array_map("serialize", $this->_array)));
        $types = array("success","info","warning","danger");
        $current = 0;
        $string = "";
        foreach($this->_array as $key => $value){
            $new_current = array_search($value[0], $types);
            if($new_current>$current){
                $current = $new_current;
            }
            $string .= "{$value[1]}. ";
        }
        if(isset($string)&&(strlen($string)>0)){
            echo "<div class=\"alert alert-{$types[$current]}\" title=\"Close\" onclick=\"this.style.display='none';\">";
            echo "<strong>{$types[$current]}!</strong> ";
            echo "<a href=\"#\" class=\"close\" data-dismiss=\"alert\" aria-label=\"close\">&times;</a> ";
            echo $string;
            echo "</div>";
        }
    }
}
$alert = new user_alert;

// set var
if(isset($_POST["contact"])){
    $contact = $_POST["contact"];
} else {
    $contact = array(
        "name" => null,
        "phone" => null,
        "email" => null,
        "message" => null
    );
}
if(isset($_SESSION["account"]["username"])){$contact["name"] = $_SESSION["account"]["username"];}

// handled post
if(isset($_POST["contact"])) {
    $error = false;

    if(strlen($contact["name"])>0) {$contact["name"] = trim($contact["name"]);} else { $alert->add("warning","Provide a valid name"); $error = true;}

    if(strlen($contact["phone"])>0) {$contact["phone"] = trim($contact["phone"]);} else {$alert->add("warning","Provide a valid phone number"); $error = true;}

    if((strlen($contact["email"])>0)&&(preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $contact["email"]))) {$contact["email"] = trim($contact["email"]);} else {$alert->add("warning","Provide a valid email address"); $error = true;  }

    if(strlen($contact["message"])>10) {
        if(function_exists('stripslashes')) {
            $contact["message"] = stripslashes(trim($contact["message"]));
        } else {
            $message = trim($contact["message"]);
        }
    } else {
        $alert->add("warning","Provide a message longer than 10 charaters");
        $error = true;
    }

    if($error==false) {
        $headers = "From: contact form <{$contact["email"]}>\r\n";
        $headers .= "Reply-To: <{$contact["email"]}>\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $message .= "Name: {$contact["name"]}<br/>";
        $message .= "Phone: {$contact["phone"]}<br/>";
        $message .= "IP address: {$_SERVER['REMOTE_ADDR']}<br/><br/>";
        $message .= "{$contact["message"]}\n";
        mail($instance->website["email"], $contact["phone"], "<html><body>{$message}</body></html>", $headers);
        $alert->add("success","Your message was successfully sent. I will be in contact with you shortly");
        $contact["phone"] = "";
        $contact["message"] = "";
    }
}

echo "<h1 class=\"container center\">Contact</h1>";

echo "<div class=\"container background-white\">";
echo "<div class=\"row\">";
echo "<div class=\"col-md-8\">";
echo "<h2>Message</h2>";
echo "<p>Complete the form below to contact me and I will do everything I can to respond as quickly as possible.</p>";

$alert->get();

echo "<form name=\"contact\" id=\"contact\" method=\"post\" enctype=\"multipart/form-data\">";

echo "<fieldset class=\"form-group\">";
echo "<label for=\"name\">Name<em class=\"required\">*</em></label>";
echo "<input name=\"contact[name]\" id=\"name\" value=\"{$contact["name"]}\" class=\"form-control\" placeholder=\"Enter name\" aria-required=\"true\"/>";
echo "</fieldset>";

echo "<fieldset class=\"form-group\">";
echo "<label for=\"email\">Email address<em class=\"required\">*</em></label>";
echo "<input name=\"contact[email]\" id=\"email\" value=\"{$contact["email"]}\" type=\"email\" class=\"form-control\"  placeholder=\"Enter email\" aria-required=\"true\"/>";
echo "<small class=\"text-muted\">Your email with will not be shared with anyone else.</small>";
echo "</fieldset>";

echo "<fieldset class=\"form-group\">";
echo "<label for=\"phone\">Phone<em class=\"required\">*</em></label>";
echo "<input name=\"contact[phone]\" id=\"phone\" value=\"{$contact["phone"]}\" class=\"form-control\" placeholder=\"Enter phone\" aria-required=\"true\"/>";
echo "</fieldset>";

echo "<fieldset class=\"form-group\">";
echo "<label for=\"Message\">Message<em class=\"required\">*</em></label>";
echo "<textarea name=\"contact[message]\" id=\"message\" class=\"form-control\" placeholder=\"Enter message\" aria-required=\"true\"/>{$contact["message"]}</textarea>";
echo "</fieldset>";

echo "<input type=\"submit\" class=\"btn btn-lg\" name=\"command\" title=\"Send\"/>";
echo "</form>";
echo "</div>";
?>