如何在用户提交表单时通过电子邮件发送联系表单

时间:2016-10-13 12:42:46

标签: php jquery html forms

我创建了一个联系表单,我现在尝试在用户点击提交按钮后将联系表单作为电子邮件发送。但是一旦用户点击提交,我就不会收到任何电子邮件。我已经包含了我的HTML表单。我知道我需要PHP和javascript代码,以使电子邮件工作,但我不知道如何开始这个过程。

我可以帮忙。

<div id="details">
        <form method="post" name="Products" action="http://www.shop4pop.nl/products/">
        Please leave your name: <br>
        <input type="text" name="firstname"> <br>
        Email: <br>
        <input type ="email" name ="email">
        <br>


        <div id="detailsSecond">
        Please leave us a description of your artwork brief: <br>
        <textarea name = "textarea" rows="10" cols="50">Write something here</textarea>



        </div>

        <input type="submit" value="Submit">
        </form>

        </div>

2 个答案:

答案 0 :(得分:0)

<div id="details">
    <form method="post" name="Products" action="">
        Please leave your name: <br>
        <input type="text" name="firstname"> <br>
        Email: <br>
        <input type ="email" name ="email">
        <br>


        <div id="detailsSecond">
        Please leave us a description of your artwork brief: <br>
        <textarea name = "textarea" rows="10" cols="50">Write something here</textarea>



        </div>

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

if(isset($_POST['submit']))
{
    if($_POST['submit']=="Submit")
    {
        $name = $_POST['firstname'];
        $detail = $_POST['textarea'];
        $email = $_POST['email'];


         $msg = "Hello yourname,<br /> $name tries to contact your from     following details. <br/><br/><br/><br/>Email : $email, <br /> Message : $detail";

        if(@mail("youremail@domain.com", $email,$msg, $headersifapplicable)){
            echo "Thank you for contacting.";
        }
        else
        {
            echo "There is some problem in contacting";
        }
    }
}

答案 1 :(得分:0)

试试这个它应该完美。

<?php



        $to="Your email address";

        //Errors
        $nameError="";
        $emailError="";
        $errMsg="";


        $errors="";//counting errors


        $name="";
        $email="";
        $message="";
        if(isset($_POST['send'])){


                if(empty($_POST['yourname'])){ //name field empty

                        $nameError="Please enter your name";
                        $errors++; // increament errors
                }else{

                        $name= UserInput($_POST['yourname']);

                        if(!preg_match("/^[a-zA-Z ]*$/", $name)){

                                $nameError="Only letters and white space accepted";
                                $errors++;
                        }

                }

                if(empty($_POST['email'])){

                        $emailError="Enter email";
                        $errors++;
                }else{

                        $email = UserInput($_POST['email']);

                        if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){

                                $emailError="Invalid Email";
                                $errors++;
                        }
                }


                if(empty($_POST['msg'])){

                        $errMsg="Enter message";
                        $errors++;
                }else{

                        $message=UserInput($_POST['msg']);
                }


                if($errors <=0){//No errors lets setup our email and send it

                         $headers = "MIME-Version: 1.0" . "\r\n";
                         $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                         $headers .= 'From: <' . $email . '>' . "\r\n";


                        $text  = "<p>New Message from $name </p>";
                        $text .= "<p>Name : $name</p>";
                        $text .= "<p>Email : $email</p>";
                        $text .= "<p>Message : $message</p>";


                        mail($to, "Website Contact", $text, $headers);
                        $success="Thank your message was submitted";
                        $_POST= array(); //clearing inputs fields after success

                }


        }





 //Filter user input
function UserInput($data){

        $data = trim($data);
        $data = stripcslashes($data);
        $data = htmlspecialchars($data);
        return $data;

}

?>



        <?php echo $success;?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SEL"]);?>">

        <span style="color:#f00;"><?php echo $nameError;?></span>
        <input type="text" name="yourname" placeholder="enter your name" <?php if(!empty($_POST['yourname'])){echo "value=\"".$_POST['yourname']."\"";}?>><br>

        <span style="color: #f00;"><?php echo $emailError;?></span>
        <input type="email" placeholder="your email" name="email" <?php if(!empty($_POST['email'])){echo "value=\"".$_POST['email']."\"";}?>><br>


        <span style="color: #f00;"><?php echo $errMsg;?></span>
        <textarea name="msg"><?php if(!empty($_POST['msg'])){echo $_POST['msg'];}?></textarea><br>

        <input type="submit" name="send" value="send">

</form>