简体   繁体   English

从联系表单发送邮件的脚本不起作用[关闭]

[英]Script for sending mail from contact form not working [closed]

I am trying to create a simple contact form with a PHP script to send an email. But the following script, located in the same folder as the index.html, is not working.我正在尝试使用 PHP 脚本创建一个简单的联系表单以发送 email。但是与 index.html 位于同一文件夹中的以下脚本无法正常工作。 It leads me to an 'This page is not working' page.它引导我进入“此页面不工作”页面。 On this URL: ipaddress/contact-form.php.在这个 URL 上:ipaddress/contact-form.php。 Could be I am misplacing files, but do not seem to grasp what must be done.可能是我放错了文件,但似乎不明白必须做什么。

The HTML HTML

<form method="POST" action="contact-form.php" class="contact-form">
            <label for="name" class="form-label">Volledige naam*</label>
            <input
              type="text"
              name="name"
              id="Name"
              placeholder="Your full name"
              required
            />
            <label for="email" class="form-label">Email*</label>
            <input
              type="email"
              name="email"
              id="Email"
              placeholder="voorbeeld@hotmail.com"
              required
            />
            <label for="phone" class="form-label">Telefoonnummer</label>
            <input
              type="tel"
              name="phone"
              id="Phone"
              placeholder="06-12345667"
            />
            <label for="message" class="form-label">Bericht*</label>
            <textarea
              type="text"
              name="message"
              id="Message"
              placeholder="Uw bericht.."
              required
            ></textarea>
            <small class="required">* verplicht</small>
            <button type="submit" class="submit">Verstuur</button>
          </form>
<?php
if (isset($_POST['Email'])) {

    $email_to = "xxxxxxxxx@gmail.com";
    $email_subject = "Een nieuw contactbericht";

    function problem($error)
    {
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br><br>";
        echo $error . "<br><br>";
        echo "Please go back and fix these errors.<br><br>";
        die();
    }

    if (
        !isset($_POST['Name']) ||
        !isset($_POST['Email']) ||
        !isset($_POST['Phone']) ||
        !isset($_POST['Message'])
    ) {
        problem('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $name = $_POST['Name']; 
    $email = $_POST['Email']; 
    $message = $_POST['Message']; 

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if (!preg_match($email_exp, $email)) {
        $error_message .= 'The Email address you entered does not appear to be valid.<br>';
    }

    $string_exp = "/^[A-Za-z .'-]+$/";

    if (!preg_match($string_exp, $name)) {
        $error_message .= 'The Name you entered does not appear to be valid.<br>';
    }

    if (strlen($message) < 2) {
        $error_message .= 'The Message you entered do not appear to be valid.<br>';
    }

    if (strlen($error_message) > 0) {
        problem($error_message);
    }

    $email_message = "Form details below.\n\n";

    function clean_string($string)
    {
        $bad = array("content-type", "bcc:", "to:", "cc:", "href");
        return str_replace($bad, "", $string);
    }

    $email_message .= "Name: " . clean_string($name) . "\n";
    $email_message .= "Email: " . clean_string($email) . "\n";
    $email_message .= "Phone: " . clean_string($phone) . "\n";
    $email_message .= "Message: " . clean_string($message) . "\n";


    $headers = 'From: ' . $email . "\r\n" .
        'Reply-To: ' . $email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
?>

    Bedankt voor uw bericht! We zullen zo spoedig mogelijk contact met u opnemen.

<?php
}
?>

Any thoughts?有什么想法吗? :) :)

When browsers submit forms, they encode the data as a series of name=value pairs, not id=value pairs.当浏览器提交 forms 时,它们会将数据编码为一系列name=value对,而不是id=value对。

You're using the wrong keys (eg $_POST['Email'] should be $_POST['email'] because you have name="email" )你使用了错误的键(例如$_POST['Email']应该是$_POST['email']因为你有name="email"


The server responded with a status of 405服务器响应状态为 405

405 means Method Not Allowed. 405 表示方法不允许。

This means that your server (whatever server that might be) is configured not to accept POST requests for that URL.这意味着您的服务器(可能是任何服务器)配置为不接受针对该 URL 的 POST 请求。

Maybe it is a server that doesn't support PHP at all.也许它是一个根本不支持 PHP 的服务器。

Maybe you have the wrong URL.也许你有错误的 URL。

Maybe the security settings for it are set to restrict request types.也许它的安全设置被设置为限制请求类型。

  1. Remember respect case sensitive in all $_POST => $_POST['Name'] etc.请记住在所有 $_POST => $_POST['Name'] 等中都区分大小写。
  2. You forget declare var $phone before use => $email_message.= "Phone: ".您忘记在使用前声明 var $phone => $email_message.= "Phone:"。 clean_string($phone).干净的字符串($电话)。 "\n" "\n"
  3. Error 405 looks like API respons.错误 405 看起来像 API 响应。 Are you working with an API or Framework?您使用的是 API 还是框架?

I reproduce the code on my PC and works fine.我在我的电脑上重现了代码并且工作正常。

https://turendu.com.ar/Jorik/ https://turendu.com.ar/Jorik/

If you have problem with php mail, maybe you can try with https://github.com/PHPMailer/PHPMailer如果您对 php 邮件有疑问,也许您可以尝试使用https://github.com/PHPMailer/PHPMailer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM