Php post 上的简单电子邮件表单打开代码

Php simple e-mail form on post opens code

我正在学习 php,现在我正在尝试制作简单的表单,以便在提交时将电子邮件发送到我的邮箱。我在本地 PC 上使用 Vamp 服务器。当我按下提交按钮时,它只会从 PHP 文件中打开以下代码并将其显示在屏幕上。 没有其他行动,有人可以向我解释发生了什么以及我做错了什么我只想提交并查看我的 gmail 帐户上的邮件,所以我知道它有效。 目录包括:form-page.html、form-to-mail-php 和 thank_you html。 在表单页面中,您有输入和提交,在下面的 php 代码中,它会将您重定向到感谢页面。

            <?php
    if(!isset($_POST['submit']))
    {
      //This page should not be accessed directly. Need to submit the form.
      echo "error; you need to submit the form!";
    }
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];

    //Validate first
    if(empty($name)||empty($visitor_email)) 
    {
        echo "Name and email are mandatory!";
        exit;
    }

    if(IsInjected($visitor_email))
    {
        echo "Bad email value!";
        exit;
    }

    $email_from = 'l.lawliet46@gmail.com';//
    $email_subject = "New Form submission";
    $email_body = "You have received a new message from the user $name.\n".
        "Here is the message:\n $message".

    $to = "l.lawliet46@gmail.com";
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    //Send the email!
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: thank-you.html');


    // Function to validate against any email injection attempts
    function IsInjected($str)
    {
      $injections = array('(\n+)',
                  '(\r+)',
                  '(\t+)',
                  '(%0A+)',
                  '(%0D+)',
                  '(%08+)',
                  '(%09+)'
                  );
      $inject = join('|', $injections);
      $inject = "/$inject/i";
      if(preg_match($inject,$str))
        {
        return true;
      }
      else
        {
        return false;
      }
    }



    ?> 



<form method="post" name="myemailform" action="form-to-email.php">
    <p>
        <label for='name'>Enter Name: </label><br>
        <input type="text" name="name">
    </p>
    <p>
        <label for='email'>Enter Email Address:</label><br>
        <input type="text" name="email">
    </p>
    <p>
        <label for='message'>Enter Message:</label> <br>
        <textarea name="message"></textarea>
    </p>
    <input type="submit" name='submit' value="submit">
</form>

首先,显示的代码而不是服务器执行的操作表明缺少 PHP 模块。在新文件中尝试 运行 一个简单的 phpinfo() 脚本并查看返回的内容。例如,

<?php
phpinfo(INFO_MODULES);
?>

将输出您机器上安装的模块。使用 WAMP,存在一个更简单的选项来修改 php.ini 文件。见下文,

Edit php.ini file

您应该注意,其中包含 PHP 脚本的 .html 文件需要为 PHP 脚本修改 PHP 处理程序。见下文,

Wamp Server isn't executing php code

您可以使用 $.ajax 将表单值从 form-page.html 传递到 form-to-mail.php 以规避修改 PHP 上一步中的处理程序文件。

接下来,考虑这些:

这是关于如何配置 WAMP 以发送邮件的非常基础的教程。

http://roshanbh.com.np/2007/12/sending-e-mail-from-localhost-in-php-in-windows-environment.html

此外,您的 Google 帐户(我认为)不允许您在未经身份验证的情况下发送邮件。如果不是这样,请纠正我。

How to configure WAMP (localhost) to send email using Gmail?

在 Closing 中,最好在输入中使用 mysqli_real_escape_string()