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 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库