我正在使用php为我的网站制作一个反馈表单,我认为它正常工作,但后来它用了箭头代码。
无论如何,表单有4个字段:“name”,“email”,“subject”和“message”。
如果所有四个字段都有内容,我会收到“消息失败”警告。
在这种情况下,我在4个字段中的3个中测试了内容,这给了我4种组合:
combo 1 - "name", "email", "subject"
combo 2 - "name", "email", "message"
combo 3 - "name", "subject", "message"
combo 4 - "email", "subject", "message"
结果如下:
combo 1 - "Thank you for your message."
combo 2 - "Thank you for your message."
combo 3 - "Message failed."
combo 4 - "Thank you for your message."
这是我正在使用的html代码:
<form action="contact.php" method="post">
<input type="hidden" name="page" value="contact" />
<input type="hidden" name="req" value="submit" />
Your Name: <input type="text" name="name" />
<br/>
Your Email: <input type="text" name="email" />
<br/>
Subject: <input type="text" name="subject" size="69" />
<br/>
Message:
<br/>
<textarea cols="63" rows="8" name="message"></textarea>
</br>
<input type="submit" value="Send" />
<input type="reset" value="Clear" />
</form>
这是contact.php:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
$mail_to = 'email@my_website.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '."\n";
$body_message .= $field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for your message.');
window.location = './contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = './contact.html';
</script>
<?php
}
?>
代码有什么问题吗?我不明白为什么会这样做。
答案 0 :(得分:3)
Anton给出了很好的答案,但我想再深入研究一下这个问题并扩展他的答案。
如果你想确保有人发送了电子邮件,要求用硬编码用php(而html5也很酷,但这可以避免,所以你仍然需要php)。我也删除了javascript位,因为它感觉无关紧要。这是我会尝试的,因为它很短,我会在同一页面中完成所有操作。有许多微妙的变化:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
//If all required fields are filled
if (!empty($field_name)&&!empty($field_email)&&!empty($field_message))
{
$mail_to = 'email@my_website.com';
$subject = 'Message from a site visitor: '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '."\n";
$body_message .= $field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
echo "Thank you for your message.";
}
//If not all required fields are filled display form again.
else
{ ?>
Please fill all the required fields.<br>
<form action="" method="post">
<input type="hidden" name="page" value="contact" />
<input type="hidden" name="req" value="submit" />
<?php //The php bits are to retrieve the valid fields ?>
Your Name*: <input required type="text" value="<?php echo $field_name; ?>" name="name"/>
<br/>
Your Email*: <input required type="email" value="<?php echo $field_email; ?>" name="email" />
<br/>
Subject: <input type="text" name="subject" value="<?php echo $field_subject; ?>" size="69" />
<br/>
Message*:
<br/>
<textarea required cols="63" rows="8" name="message"><?php echo $field_message; ?></textarea>
</br>
<input type="submit" value="Send" />
<input type="reset" value="Clear" />
</form>
<?php
}
?>
否则,如果你不关心谁发送它,你可以放一些这样的东西
$field_name = $_POST['name'];
$field_email = $_POST['email'];
if (empty($field_email)) $field_email="anonymous@MYPAGE.com";
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
最后,我希望这只是一个伪代码,因为它几乎不符合任何html规则。你需要&lt; html&gt;标签,&lt;身体&gt;等...
刚刚测试过并且效果很好(;
答案 1 :(得分:1)
问题可能是这样的:
来自http://php.net/manual/en/function.mail.php
发送邮件时,邮件必须包含From标头。这可以 使用additional_headers参数设置,或者可以设置默认值 php.ini中。
如果不这样做,将导致类似于警告的错误消息: mail():“sendmail_from”未在php.ini中设置或自定义“From:”标题 失踪。 From标头还设置了Windows下的Return-Path。
换句话说,如果您没有包含正确“发件人:adress@email.com”的标题,则会发生错误。