duankong8998 2016-02-24 21:12
浏览 35
已采纳

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 markup:

<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:
 $name 

 COMPANY:
 $company 

 MESSAGE:
 $message";
$recipient = "email address";
$subject = "Subject";
$mailheader = "From: $email 
";
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).

Any ideas?

Thanks!

  • 写回答

4条回答 默认 最新

  • doudi8829 2016-02-24 21:21
    关注

    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.

    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.

    • Check your access logs.

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

    I.e.:

    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:

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

    Sidenote:

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


    Footnotes:

    "Most of the the fields are "required" and I was using a captcha system for a while, but the blank emails kept coming."

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

    N.B.:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址