【问题标题】:Script for sending mail from contact form not working [closed]从联系表单发送邮件的脚本不起作用[关闭]
【发布时间】:2022-01-01 05:15:56
【问题描述】:

我正在尝试使用 PHP 脚本创建一个简单的联系表单来发送电子邮件。但是,与 index.html 位于同一文件夹中的以下脚本不起作用。它引导我进入“此页面无法正常工作”页面。在这个 URL 上:ipaddress/contact-form.php。可能是我放错了文件,但似乎没有掌握必须做什么。

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
}
?>

有什么想法吗? :)

【问题讨论】:

  • “此页面无法正常工作” ...消息中是否有附带的 HTTP 状态代码?例如404、500 之类的?
  • 不要把@放在函数名前面,例如在您的 @mail... 中。这只是抑制了实际上可能对诊断问题有用的错误。
  • 什么确切不起作用?您尝试过什么来解决问题?您的消息是否正确构建?呼叫mail 工作正常吗?是否有任何关于此的信息写入您的服务器的错误日志,或者可能写入服务器的邮件日志?
  • 当我点击提交按钮时,它会直接将我带到一个错误页面。无法回答您的具体问题,因为这个 PHP 和表单对我来说是新的。浏览器中的控制台显示有关以下内容的错误;服务器以 405 状态响应。
  • Small Point 虽然在 IF 中创建函数是合法的,但它并不是一个好的做法。原因,如果您不在 IF 中,则不会创建该函数,您可能不会发现该函数依赖于 IF。还有龙

标签: php html forms email


【解决方案1】:

当浏览器提交表单时,它们会将数据编码为一系列 name=value 对,不是 id=value 对。

您使用了错误的键(例如,$_POST['Email'] 应该是 $_POST['email'],因为您有 name="email"


服务器响应状态为 405

405 表示方法不允许。

这意味着您的服务器(无论可能是什么服务器)配置为不接受针对该 URL 的 POST 请求。

可能是服务器根本不支持PHP。

也许你的网址有误。

也许它的安全设置被设置为限制请求类型。

【讨论】:

    【解决方案2】:
    1. 请记住,所有 $_POST => $_POST['Name'] 等都区分大小写。
    2. 您忘记在使用前声明 var $phone => $email_message .= "Phone:" 。 clean_string($phone) 。 “\n”
    3. 错误 405 看起来像 API 响应。您正在使用 API 或框架吗?

    我在我的电脑上重现了代码并且工作正常。

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

    如果你对 php 邮件有问题,也许你可以试试 https://github.com/PHPMailer/PHPMailer

    html 未编辑。 php 代码只设置 $_POST 小写并添加 $phone 变量

        <?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'];
        $phone = $_POST['phone']; 
    
        $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
    }
    
    ?>
    

    【讨论】:

    • Error 405 looks like API respons...其实这是一个标准的HTTP状态码。没有特定于 API 的内容。
    • 它是正确的。这个想法是知道是否在平面 PHP 中工作。单个平面 php 页面不响应 405。
    • 如果该方法不被允许,可能。你似乎并不真正了解它是如何工作的。无论如何,什么是“平面”PHP 页面 - 这不是一个有用或难以理解的技术术语。
    • 真的吗?您需要从 Web 服务器阻止它。 symfony.com/doc/current/introduction/…
    • 嗯,也许就是这样。您似乎在说这是不可能的,但是如果有人这样做的话,这是可能的。而且我们不知道 OP 是否在使用 symfony。
    猜你喜欢
    • 2020-05-10
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多