【问题标题】:Contact Form - Send / Receive issues联系表 - 发送/接收问题
【发布时间】:2017-01-31 07:54:17
【问题描述】:

我编写了一个联系表单,但由于某种原因,我实际上并没有收到电子邮件,但是 php 代码没有错误,并且表单返回了成功消息。我已从此处的代码中删除了我的电子邮件地址。

联系表格

<body>
<div class='formbody'>

<link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>

<form id="contact-form" action="send.php" method="POST">

  <p>Hi</p>
  <p>My
    <label for="your-name">name</label> is
    <input type="text" name="your-name" id="your-name" minlength="3" placeholder="(your name here)" required> and</p>

  <p>my
    <label for="email">email address</label> is
    <input type="email" name="your-email" id="email" placeholder="(your email address)" required>
  </p>

  <p> I have a
    <label for="your-message">message</label> for you,</p>

  <p>
    <textarea name="your-message" id="your-message" placeholder="(your msg here)" class="expanding" required></textarea>
  </p>
  <p>
    <button type="submit">
      <svg version="1.1" class="send-icn" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100px" height="36px" viewBox="0 0 100 36" enable-background="new 0 0 100 36" xml:space="preserve">
        <path d="M100,0L100,0 M23.8,7.1L100,0L40.9,36l-4.7-7.5L22,34.8l-4-11L0,30.5L16.4,8.7l5.4,15L23,7L23.8,7.1z M16.8,20.4l-1.5-4.3
    l-5.1,6.7L16.8,20.4z M34.4,25.4l-8.1-13.1L25,29.6L34.4,25.4z M35.2,13.2l8.1,13.1L70,9.9L35.2,13.2z" />
      </svg>
      <small>SEND PLEASE</small>
    </button>
  </p>
</form>
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="assets/js/bs-animation.js"></script>
     <script src="assets/js/index.js"></script>
    </div>
</body>
</html>

send.php

    <?php 
$page='contactus';
include('header.php');
include('navbar.php');

?>

<?php
$to = 'removed';
$name = TRIM (stripslashes($_POST['your-name']));
$email = TRIM (stripslashes($_POST['your-email']));
$message = TRIM (stripslashes($_POST['your-message']));
$subject = 'Message';


$name = 'Name: ';
$email= 'From:';
$message= 'Message: ';





if( mail($to, $subject, $name , $email , $message) ){
        echo 'success';

}

else{

    print('There has been a small error . Sorry!!');
}

?>

【问题讨论】:

  • 邮件功能始终遵循此规则 mail(to,subject,message,headers,parameters);
  • 好的,不太清楚你的意思。?能详细点吗?

标签: php html


【解决方案1】:

所以你没有在这里发送你的标题或添加标题。

因此,我们不是将表单传递给新文件 (send.php),而是将逻辑添加到 send.php 并让表单文件在代码顶部需要它,如下所示:

<?php

require 'send.php';

$send = new sendEmail();
?>

<body>
<div class='formbody'>
    <?php
    if (isset($_POST['sendForm']))
    {
        $send->sendMail($_POST['your-name'], $_POST['your-email'], $_POST['your-message']);
    }
    ?>
    <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>

    <form id="contact-form" action="" method="post">

        <p>Hi</p>
        <p>My
            <label for="your-name">name</label> is
            <input type="text" name="your-name" id="your-name" minlength="3" placeholder="(your name here)" required> and</p>

        <p>my
            <label for="email">email address</label> is
            <input type="email" name="your-email" id="email" placeholder="(your email address)" required>
        </p>

        <p> I have a
            <label for="your-message">message</label> for you,</p>

        <p>
            <textarea name="your-message" id="your-message" placeholder="(your msg here)" class="expanding" required></textarea>
        </p>
        <p>
            <button type="submit" name="sendForm">
                <svg version="1.1" class="send-icn" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100px" height="36px" viewBox="0 0 100 36" enable-background="new 0 0 100 36" xml:space="preserve">
        <path d="M100,0L100,0 M23.8,7.1L100,0L40.9,36l-4.7-7.5L22,34.8l-4-11L0,30.5L16.4,8.7l5.4,15L23,7L23.8,7.1z M16.8,20.4l-1.5-4.3
    l-5.1,6.7L16.8,20.4z M34.4,25.4l-8.1-13.1L25,29.6L34.4,25.4z M35.2,13.2l8.1,13.1L70,9.9L35.2,13.2z" />
      </svg>
                <small>SEND PLEASE</small>
            </button>
        </p>
    </form>
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="assets/js/bs-animation.js"></script>
    <script src="assets/js/index.js"></script>
</div>
</body>
</html>

然后在你要添加的 send.php 文件中:

<?php

class sendEmail
{
    public function sendMail($name, $email, $message)
    {
        if (!empty($name))
        {
            if (!empty($email))
            {
                if (!empty($message))
                {
                    $email_to = 'YOUR EMAIL ADDRESS';
                    $header = 'From: ' . $name ."<noreply@yourdomain.co.uk/com>". "\r\n" .
                        'Reply-To: ' . $email . "\r\n" .
                        'X-Mailer: PHP/' . phpversion();

                    @mail($email_to, 'Enquiry Received', 'Name: ' . $name . "\r\n\r\n". 'Email: ' .$email. "\r\n\r\n". 'Message: ' .$message."\r\n\r\n", $header);

                    echo "Thanks for contacting us";

                } else {
                    echo "Please enter a message";
                }
            } else {
                echo "Please provide your email address.";
            }
        } else {
            echo "Please provide your name.";
        }

    }
}
?>

现在,我添加了条件,如果字段为空,它将返回错误,这仅仅是因为添加到输入字段中的required 可以被绕过,所以设置条件总是好的。

注意:如果您希望将表单查询等存储到您的数据库中,例如从任何 IP 地址发送、发送时间、发件人、内容等,您还可以在此处添加逻辑消息说。

这只是一个基本示例,但您将在阅读代码后了解所做的工作。

【讨论】:

  • OP 在他的问题中没有使用类,
  • OP 不必使用该类,但它是更好的实践路线。在我看来,尝试用 PHP 以正确的方式引导 OP 不值得投反对票。
  • DV 不是我的,我不 dv 回答,而是在需要时添加]
  • 谢谢你的回答,直截了当。感谢帮助。现在都在工作。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2012-03-13
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 2013-10-06
相关资源
最近更新 更多