【问题标题】:PHP contact form sends message through senders addressPHP 联系表单通过发件人地址发送消息
【发布时间】:2016-08-30 04:38:57
【问题描述】:

最近我的 PHP 联系表单出现问题。它工作了大约两年,而且我没有改变任何东西,所以我真的不明白问题是什么。代码如下:

<?php 

        // Check for header injections
        function has_header_injection($str) {
            return preg_match ( "/[\r\n]/", $str );
        }

        if(isset ($_POST['contact_submit'])) {

            $name   = trim($_POST['name']);
            $email  = trim($_POST['email']);
            $tel    = trim($_POST['tel']);
            $msg    = $_POST['message'];

            // check to see if name or email have header injections
            if (has_header_injection($name) || has_header_injection($email)){

                die();          

            }

            if ( !$name || !$email || !$msg ) {

                echo '<h4 class="error">All Fields Required</h4><a href="page.php"  target="_blank" class="button link">Go back and try again</a>';
                exit;

            }   

            // add the recipient email to a variable
            $to = "example@example.net";

            // Create a subject
            $subject = "$name sent you an email";

            // construct your message
            $message .= "Name: $name sent you an email\r\n";
            $message .= "Telephone: $tel\r\n";
            $message .=  "Email: $email\r\n\r\n";
            $message .=  "Message:\r\n$msg";



            $message = wordwrap(message, 72);

            // set the mail header
            $headers = "MIME=Version: 1.0\r\n";
            $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
            $headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
            $headers .= "X-Priority: 1\r\n";
            $headers .= "X-MSMail-Priority: high\r\n\r\n";

            // Send the Email
            mail( $to, $subject, $message, $headers );      

        ?>

        <!--- END PHP CONTACT FORM -->

        <!-- Show Success message -->
        <h2>Thanks for contacting Us!</h2>
        <p align="center">Please allow 24 hours for a response</p>
        <p><a href="index.php" class="button block">&laquo; Go to Home Page</a></p>

        <?php } else { ?>

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

            <label for="name">Your Name</label>
            <input type="text" id="name" name="name">

            <label for="tel">Your Phone Number</label>
            <input type="tel" id="tel" name="tel">

            <label for="email">Your Email</label>
            <input type="email" id="email" name="email">

            <label for="message">the date/time you wish to sign up for</label>
            <textarea id="message" name="message"></textarea>
            <br>

            <input type="submit" class="button next" name="contact_submit" value="Sign Up">




        </form>

        <?php } ?>

但是,当提交联系表单时,它不会将信息发送到电子邮件正文,而是将其发送到电子邮件的“发件人”部分。例如,电子邮件可能会说:

致:Web 开发人员

发件人:Bob Smith 888-888-8888 周一、周三周五

主题:Bob Smith 给你发了一封电子邮件!

身体: X-Priority: 1X-MSMail-Priority: 高

消息

我真的不知道发生了什么,所以任何帮助将不胜感激!

【问题讨论】:

  • $headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n &lt;" . $email . "&gt; \r\n\r\n"; this line have all your POST data

标签: php html forms email post


【解决方案1】:

您将在“发件人”标题中添加所有这些信息。

$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";

将标题更改为:

$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$email}>\r\n"; // Removed all extra variables
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n";

它应该可以工作。

您已经发送$message,正文中也包含上述所有数据。

然而,为什么你以前没有经历过这种情况是个谜。

注意:您只需要在每个标题后有一个\r\n

您还应该更改此行:

$message = wordwrap(message, 72);

$message = wordwrap($message, 72); // Adding $ in front of the variable.

【讨论】:

  • 这消除了发件人栏中的所有信息(我不知道为什么我首先将其包含在其中),但消息仍然没有发送。在电子邮件的正文中,它只显示“消息”一词
  • 更新了我的答案。你有$message = wordwrap(message, 72);,而它应该是$message = wordwrap($message, 72);。关于“发件人”字段......它应该只包含姓名和电子邮件,而不是完整的消息,所以是的......这将删除它。 :)
  • 它是不发送还是被发送而只是在正文中带有“消息”?如果是后者,更新应该可以解决。
猜你喜欢
  • 2012-09-16
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多