PHP电子邮件表单拍摄空白电子邮件 [英] PHP email form shooting blank emails

查看:99
本文介绍了PHP电子邮件表单拍摄空白电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为网站整理了一个简单的PHP电子邮件表格,但是它总是每隔一段时间发送一次空白电子邮件。大多数字段都是必填字段,我使用了验证码系统已有一段时间,但是空白电子邮件不断出现。

I put together a simple PHP email form for a website, but it keeps sending blank emails every so often. Most of the the fields are "required" and I was using a captcha system for a while, but the blank emails kept coming.

HTML标记:

<form action="mail_send.php" method="post">

<input name="name" type="text" required="required" size="40" />

<input name="email" type="text" required="required" size="40" />

<input name="company" type="text" size="40" />

<textarea name="message" cols="80" rows="7" required="required"></textarea>

<input type="submit" value="Submit" />

</form>

PHP:

$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$message = $_POST['message'];
$formcontent=" FROM:\n $name \n\n COMPANY:\n $company \n\n MESSAGE:\n $message";
$recipient = "email address";
$subject = "Subject";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

echo "<script>window.location = 'confirmation.php'</script>";

我测试时一切正常,从表格中收到的电子邮件没有任何问题,但是由于某些原因,我经常会收到空白电子邮件(可能是来自机器人)。

Everything works fine when I test it, I receive the emails from the form with no problems at all, but for some reason I keep getting blank emails often (possibly from robots).

有什么想法吗?

谢谢!

推荐答案

如果您的HTML表单和PHP位于同一文件中,而您不检查其中的任何一个,则可能会发生这种情况输入是否为空。而且,如果不在同一个文件中,则不检查是否为空仍然适用。

That could happen if your HTML form and PHP are inside the same file while you're not checking if any of those inputs are empty or not. And if not in the same file, not checking for emptyness, still applies.

您可能是僵尸程序的受害者,或者是一些小丑经常访问您的网站,只是为了打勾你。

You could be the victim of bots, or some joker visiting your site ever so often just to tick you off.

或者某人或某物直接访问表单的方法的URL,这在我看来可能是问题所在,因为您确实有需要作为输入。

Or that the form's method's URL is being accessed directly by someone or something, which is what I feel may be the issue here, since you do have required for your inputs.


  • 检查访问日志。

因此,对所有输入内容使用条件!empty()

So, use a conditional !empty() against all your inputs.

  • http://php.net/manual/en/function.empty.php

即:

旁注: || 检查是否有一个为空。

Sidenote: || checks to see if one or any are empty.

if( !empty($_POST['name']) || !empty($_POST['email']) ){

   $name = $_POST['name'];
   $email = $_POST['email'];

   // process mail

}

您可以在其中添加其他内容。

You can add the other ones in.

或为您的提交添加名称属性:

Or give your submit a name attribute:

<input name="submit" type="submit" value="Submit" />

然后检查按钮是否已设置并且输入内容是否为空:

Then check if the button is set and that the inputs are not empty:

if(isset(_POST['submit'])){

    if(!empty($_POST['name']) || !empty($_POST['email']) ){

       $name = $_POST['name'];
       $email = $_POST['email'];

       // process mail

    }

}






对于电子邮件输入,还应该使用过滤器:


You should also use filters, for the email input:

  • http://php.net/manual/en/function.filter-var.php
  • http://php.net/manual/en/filter.examples.validation.php

如果您决定要在以后使用单选框,请对它们使用 isset()

Plus, if you decide to use radios/checkboxes later on, use isset() against those.

侧注:

您可以在表单中添加一个复选框,以检查是否已选中该复选框,并使用条件语句进行处理。

You could add a checkbox to your form to check if it was checked or not, and handle it with a conditional statement.

脚注:


字段为必填,我使用了验证码系统一段时间,但空白电子邮件不断到来。

您的问题中没有任何验证码支持此操作。

There isn't any captcha code in your question to support this.

注意: / strong>

N.B.:

必填属性仅在受HTML5支持的浏览器中有效。因此,如果您网站上的所有漫游器或访问者使用的浏览器不支持HTML5或可以绕过它的技术,那么这也可能是另一个(促成因素)。

The required attribute only works in HTML5 supported browsers. Therefore, if any of those bots or visitors to your site are using a browser that doesn't support HTML5, or technology that can bypass it, then that too could be another (contributing) factor.

这篇关于PHP电子邮件表单拍摄空白电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆