duanjia2772 2012-01-15 01:54
浏览 35
已采纳

PHP联系表格空白电子邮件

Looks like send.php is being accessed and sending blank emails. Need to make it so send.php doesn't send email, unless the submit button is used on a form. I was told 'isset $_post' would help me, just not clear on how to execute it.

Here's send.php code:

    <?php

$recipient  =   'test@test.com';

$email      =   $_REQUEST['Email'];

$subject    =   'Contact Form Submission';

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

";

$redirect   =   'thankyoupage.html';

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

foreach($_POST as $k=>$v)
    {
        $content .= "$k: $v

";
    }

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."
";
        $headers .= "Reply-To: ".$email."
";

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

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

";

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

    }

?>
  • 写回答

5条回答 默认 最新

  • dpnw86361 2012-01-15 04:16
    关注

    You have to check wether the request method was POST. If so, you should send the email:

    <?php
    
    $recipient  =   'test@test.com';
    
    $email      =   $_REQUEST['Email'];
    
    $subject    =   'Contact Form Submission';
    
    $content    =   "The following has been submitted on your website 
    
    ";
    
    $redirect   =   'thankyoupage.html';
    
    $user = $_REQUEST['Email'];
    $usersubject = "Subject";
    $userheaders = "From: test@test.com
    ";
    $usermessage = "Blah blah blah";
    
    
    foreach($_POST as $k=>$v)
        {
            $content .= "$k: $v
    
    ";
        }
    
    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."
    ";
            $headers .= "Reply-To: ".$email."
    ";
    
            if ($bcc) $headers .= "Bcc: ".$bcc."
    "; 
    
            $headers .= "X-Priority: 0
    ";
            $headers .= "X-Mailer: bjm Formmail 
    ";
            $message = $content."
    
    ";
    
            if( !mail($recipient, $subject, $message, $headers))
            {
                echo "Sorry - an error occured trying to send the mail";
            }
    
        }
    
    ?>
    

    This makes it all happen:

    if($_SERVER["REQUEST_METHOD"] == "POST") {
         mail_it(stripslashes($content), $subject, $email, $recipient);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器