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 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题