关于html:PHP联系人表格通过发件人地址发送消息

PHP contact form sends message through senders address

最近,我的PHP联系人表格遇到了问题。 它运行了大约两年,效果很好,而且我什么都没做,所以我真的不明白问题出在哪里。 这是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php

        // Check for header injections
        function has_header_injection($str) {
            return preg_match ("/[\
\
]/"
, $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>Go back and try again';
                exit;

            }  

            // add the recipient email to a variable
            $to ="[email protected]";

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

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



            $message = wordwrap(message, 72);

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

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

        ?>

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

        <!-- Show Success message -->
        Thanks for contacting Us!
        <p align="center">Please allow 24 hours for a response
</p>
        <p>
&laquo; Go to Home Page
</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>
           

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




        </form>

        <?php } ?>

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

致:Web开发人员

来自:鲍勃·史密斯888-888-8888,星期一,星期三,星期五

主旨:鲍勃·史密斯(Bob Smith)给您发送了电子邮件!

身体:

X优先级:1X-MSMail优先级:高

信息

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


您将所有这些信息添加到" from"标题中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$headers .="\
\
From:"
. $name ." \
\
\
\
"
. $tel ." \
\
\
\
"
. $msg ."\
\
\
\
<"
. $email ."> \
\
\
\
"
;

将标题更改为此:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$headers ="MIME=Version: 1.0\
\
"
;
$headers .="Content-type: text/plain; charset=iso-8859-1\
\
"
;
$headers .="From: {$name} <{$email}>\
\
"
; // Removed all extra variables
$headers .="X-Priority: 1\
\
"
;
$headers .="X-MSMail-Priority: high\
\
"
;

它应该工作。

您已经在发送$message,并且体内也包含上述所有数据。

为什么您以前从未经历过这是一个谜。

注意:每个标头后只需要一个\
\

您还应该更改此行:

1
$message = wordwrap(message, 72);

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