如何通过电子邮件发送表格数据

时间:2020-01-26 14:22:02

标签: php html forms

我正在尝试通过电子邮件在本地服务器上发送表单数据,但始终收到以下错误消息: 警告:mail():无法通过“ localhost”端口25连接到邮件服务器,无法验证php.ini中的“ SMTP”和“ smtp_port”设置,或在C:\ Program Files \ Ampps \ www \ test1中使用ini_set()。 138行上的php 这是我的代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title> A quick test </title>
        <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed&display=swap" rel="stylesheet">
        <style>
            body {
                background-color:#5F9EA0;
                font-family: 'Roboto Condensed', sans-serif;
                color: white;
            }
            div.header{
                position:absolute;
                top: 0;
                left:0;
                padding: 5px;
                background-color: white;
                border: 2px solid Aliceblue;
                width:99%;
            }
            div.wrapper{
                position:relative;
            }
            div.topleft{
                position:absolute;
                left:20.5%;
                top:45px;
                width:auto;
                text-align: left;
            }
            div.content{
                position:absolute;
                left:21%;
                top:18%;
                width:auto;
                text-align: left;
                font-size: 16px;
                color:aliceblue;
            }
            div.form{
                position:absolute;
                left:21%;
                top:30%;
                width:50%;
                text-align: left;
                font-size:16px;
                color:aliceblue;
            }
            input{
                padding:7.5px 15px;
                margin:8px 0;
                border: 2px solid aliceblue;
                border-radius: 5px;
            }
            textarea{
                padding:7.5px 15px;
                margin:8px 0;
                border:2px solid aliceblue;
                border-radius:5px;
            }
            .error {
                color: #FF0000;
                }
        </style>
    </head>
        <body>
            <?php 
        $FirstName = $LastName = $email = $phone = $phone1 = $phone2 = $subject = $message = "";
        $FirstNameErr = $LastNameErr = $emailErr = $phoneErr = $phone1Err = $phone2Err = $subjectErr = $messageErr = "";

        function test_input($data){
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }

        if ($_SERVER["REQUEST_METHOD"] == "POST"){
            if (empty($_POST["FirstName"])){
                $FirstNameErr = "*Your&nbsp;first&nbsp;name&nbsp;is&nbsp;required.";
            } else {
                $FirstName = test_input($_POST["FirstName"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $FirstName)){
                    $FirstNameErr = "*Only&nbsp;letters&nbsp;and&nbsp;whitespace&nbsp;allowed.";
                }
            }
            if (empty($_POST["LastName"])){
                $LastNameErr = "*Your&nbsp;last&nbsp;name&nbsp;is&nbsp;required.";
            } else {
                $LastName = test_input($_POST["LastName"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $LastName)){
                    $LastNameErr = "*Only&nbsp;letters&nbsp;and&nbsp;whitespace&nbsp;allowed.";
                }
            }
            if (empty($_POST["email"])){
                $emailErr = "*Your&nbsp;email&nbsp;is&nbsp;required.";
            } else {
                $email = test_input($_POST["email"]);
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
                    $emailErr = "*Invalid&nbsp;email&nbsp;format.";
                }
            }
            if (empty($_POST["phone"])){
                $phoneErr = "*Your&nbsp;phone&nbsp;number&nbsp;is&nbsp;required.";
            } else {
                $phone = test_input($_POST["phone"]);
                if (!preg_match("/^[0-9 ]*$/", $phone)){
                    $phoneErr = "*Invalid&nbsp;phone&nbsp;number.";
                }
            }
            if (empty($_POST["subject"])){
                $subjectErr = "*The&nbsp;subject&nbsp;of&nbsp;your&nbsp;message&nbsp;is&nbsp;required.";
            } else {
                $subject = test_input($_POST["subject"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $subject)){
                    "*Only&nbsp;letters&nbsp;and&nbsp;whitespace&nbsp;may&nbsp;be&nbsp;used&nbsp;in&nbsp;the&nbsp;subject.";
                }
            }
            if (empty($_POST["message"])){
                $messageErr = "*You&nbsp;must&nbsp;input&nbsp;a&nbsp;message.";
            } else {
                $message = test_input($_POST["message"]);
                if (!preg_match("/^[a-zA-Z0-9]*$/", $message)){
                    "*Only&nbsp;letters,&nbsp;numbers,&nbsp;and&nbsp;whitespace&nbsp;allowed.";
                }
            }
        }
?>
<?php
    $FirstName = $_POST["FirstName"];
    $LastName = $_POST["LastName"];
    $email = $_POST["email"];
    $phone = $_POST["phone"];
    $subject = $_POST["subject"];
    $message = $_POST["message"];

    $headers = "From: $email\n";
    $sent = mail('christina.adkisson@mail.com', 'Feedback Form Submission', $message, $headers);

    if ($sent) {

        echo "<html>
        <head>
        <title>Thank You</title>
        </head>
        <body>
        <h1>Thank You</h1>
        <p>Thank you for your feedback.</p>
        </body>
        </html>";

        } else {

        echo "<html>
        <head>
        <title>Something went wrong</title>
        </head>
        <body>
        <h1>Something went wrong</h1>
        <p>We could not send your feedback. Please try again.</p>
        </body>
        </html>";
        }
?>
        <div class="header">
        <img src="http://localhost/images/pink.png" height="15px" width="15px">
        <img src="http://localhost/images/blue.png" height="15px" width="15px">
        <img src="http://localhost/images/green.png" height="15px" width="15px">
        </div>
        <div class="wrapper">
            <div class="topleft">
                <h1>Contact Form</h1>
            </div>
        </div>
        <div class="content"> 
        <p>Please fill in your information and we'll contact you in no time.</p>
        </div>
        <div class="form">
            <form method="post" id="form"> 
                First name: <input type="text" name="FirstName" value="<?php echo $FirstName;?>" size="65">
                <span class="error"><?php echo $FirstNameErr;?></span>
                <br>
                 Last name: <input type="text" name="LastName" value="<?php echo $LastName;?>"size="65">
                 <span class="error"><?php echo $LastNameErr;?></span>
                 <br>
                 Your email: <input type="email" name="email" value="<?php echo $email;?>" size="65">
                 <span class="error"><?php echo $emailErr;?></span>
                 <br>
                 Phone number: <input type="text" name="phone" value="<?php echo $phone;?>" size="61">
                 <span class="error"><?php echo $phoneErr;?></span>
                 <br>
                 Message Subject: <input type="text" name="subject" value="<?php echo $subject;?>" size="59">
                 <span class="error"><?php echo $subjectErr;?></span>
                 <br>
                 Message:  
                 <br>
                 <textarea name="message" rows="7" cols="78"><?php echo $message;?></textarea>
                 <span class="error"><?php echo $messageErr;?></span>
                 <br>
                 <input type="submit" value="Send email"/><hr>
            </form>
        </div>
    </body> 
</html>   

我是php的新手,所以我不确定自己在做什么错。

0 个答案:

没有答案