【问题标题】:PHP Contact Form blank emailsPHP 联系表格空白电子邮件
【发布时间】:2012-01-15 01:54:57
【问题描述】:

看起来 send.php 正在被访问并发送空白电子邮件。需要使 send.php 不发送电子邮件,除非在表单上使用提交按钮。有人告诉我 'isset $_post' 会帮助我,只是不清楚如何执行它。

这里是 send.php 代码:

    <?php

$recipient  =   'test@test.com';

$email      =   $_REQUEST['Email'];

$subject    =   'Contact Form Submission';

$content    =   "The following has been submitted on your website \n\n";

$redirect   =   'thankyoupage.html';

$user = $_REQUEST['Email'];
$usersubject = "Subject";
$userheaders = "From: test@test.com\n";
$usermessage = "Blah blah blah";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);

foreach($_POST as $k=>$v)
    {
        $content .= "$k: $v\n\n";
    }

mail_it(stripslashes($content), $subject, $email, $recipient);

if (isset($_POST['redirect']))
    {
        header("Location:".$_POST['redirect']);
    }
        else
    {
        header("Location: $redirect");
    }

function mail_it($content, $subject, $email, $recipient)
    {

        $headers .= "From: ".$email."\n";
        $headers .= "Reply-To: ".$email."\n";

        if ($bcc) $headers .= "Bcc: ".$bcc."\n"; 

        $headers .= "X-Priority: 0\n";
        $headers .= "X-Mailer: bjm Formmail \n";
        $message = $content."\n\n";

        if( !mail($recipient, $subject, $message, $headers))
        {
            echo "Sorry - an error occured trying to send the mail";
        }

    }

?>

【问题讨论】:

  • 你是怎么调用 send.php 的?

标签: php forms


【解决方案1】:

您必须检查请求方法是否为 POST。如果是这样,您应该发送电子邮件:

<?php

$recipient  =   'test@test.com';

$email      =   $_REQUEST['Email'];

$subject    =   'Contact Form Submission';

$content    =   "The following has been submitted on your website \n\n";

$redirect   =   'thankyoupage.html';

$user = $_REQUEST['Email'];
$usersubject = "Subject";
$userheaders = "From: test@test.com\n";
$usermessage = "Blah blah blah";


foreach($_POST as $k=>$v)
    {
        $content .= "$k: $v\n\n";
    }

if($_SERVER["REQUEST_METHOD"] == "POST") {
     mail_it(stripslashes($content), $subject, $email, $recipient);
}

if (isset($_POST['redirect']))
    {
        header("Location:".$_POST['redirect']);
    }
        else
    {
        header("Location: $redirect");
    }

function mail_it($content, $subject, $email, $recipient)
    {

        $headers .= "From: ".$email."\n";
        $headers .= "Reply-To: ".$email."\n";

        if ($bcc) $headers .= "Bcc: ".$bcc."\n"; 

        $headers .= "X-Priority: 0\n";
        $headers .= "X-Mailer: bjm Formmail \n";
        $message = $content."\n\n";

        if( !mail($recipient, $subject, $message, $headers))
        {
            echo "Sorry - an error occured trying to send the mail";
        }

    }

?>

这一切都会发生:

if($_SERVER["REQUEST_METHOD"] == "POST") {
     mail_it(stripslashes($content), $subject, $email, $recipient);
}

【讨论】:

    【解决方案2】:

    您需要在标题行的末尾使用\r\n。但是,如果您只有一个标题,则根本不需要它。

    【讨论】:

      【解决方案3】:

      您需要确保仅在有发布请求时才执行代码。您可以通过将所有内容包装在以下内容中来做到这一点:

      if ($_SERVER['REQUEST_METHOD'] === 'POST')
      {
         ...
      }
      

      【讨论】:

      • 试过了,不行。认为它会因为其他 if 语句而导致问题。尝试做: 在顶部,但不起作用。
      【解决方案4】:

      如果你想发送邮件,只有当有人点击提交按钮时,只需使用这个

      if (isset($_POST['submit'])){
         send_mail();      //and define send mail somewhere else 
      }
      

      【讨论】:

        【解决方案5】:

        将此添加到您的 php 脚本中:

        if ($Name == '')
        {
            die("<p align='center'><font face='Arial' size='6' color='#FF0000'>Please use our<a href=''> contact form</a> to send a message to us</font></p>");
        }
        

        在后面添加:

        $Name = Trim(stripslashes($_POST['Name'])); 
        $Email = Trim(stripslashes($_POST['Email`enter code here`'])); 
        $Tel = Trim(stripslashes($_POST['Tel'])); 
        $Message = Trim(stripslashes($_POST['Message'])); 
        

        如果您收到空白电子邮件,则表示有人正在从浏览器运行action="contact.php" 脚本,例如www.yousite.com/contactsend.php

        上面的代码会阻止它

        【讨论】:

          猜你喜欢
          • 2013-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-13
          • 2013-11-02
          相关资源
          最近更新 更多