通过PHP向电子邮件发送表单

时间:2019-12-08 13:58:22

标签: php html

我试图通过PHP将表单发送到我的邮件,所以我在纸上创建了一个简单的联系表单并获得了PHP代码的帮助(我仍然是php上的新手...),看来一切都很好,但是 当我检查发送给我的邮件的表单是否无效时,我很乐意寻求帮助。

这是html

<form id="sign-form" method="post" action="#">
                                    <input type="submit" value="submit" style="margin-right: 10px; font-size: 16px;">
                                    <input type="email" name="email" placeholder="mail" class="form-control input-box">
                                    <input type="text" name="phone" placeholder="phone" class="form-control input-box">
                                    <input type="text" name="name" placeholder="name" class="form-control input-box">
                                    <div id="signup-error"></div>
                                </form>

和继承人的PHP


<?php

$name = '';
$email = '';
$msg = '';
$subject = "message has been send from site";

if($_POST) {
    // collect all input and trim to remove leading and trailing whitespaces
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $msg = trim($_POST['message']);  
    $ip = $_SERVER['REMOTE_ADDR'];

    $to = "mymail@gmail.com";

    // Prepare message
    $message = "You have received email from: ".$name.", ".$email.".<br />";
    $message .= "Message: <br />".$msg."<br /><br />";
    $message .= "IP: ".$ip."<br />";
    $headers = "From: $email \n";
    $headers .= "Reply-To: $email \n";
    $headers .= "MIME-Version: 1.0 \n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";

    // Email Sent
    if(mail($to, $subject,$message, $headers)){
        echo "ok";
    }
    // Error Message
    else{ 
        echo "error";
    }

}

?>

0 个答案:

没有答案