簡體   English   中英

PHP聯系表格空白電子郵件

[英]PHP Contact Form blank emails

看起來send.php正在被訪問並發送空白電子郵件。 需要做到的是send.php不會發送電子郵件,除非在表單上使用了Submit按鈕。 有人告訴我'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";
        }

    }

?>

添加到您的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'])); 

如果您收到空白電子郵件,則表示有人正在從瀏覽器(如www.yousite.com/contactsend.php )運行action="contact.php"腳本

上面的代碼將停止它

您需要在標題行的末尾使用\\r\\n 但是,如果只有一個標頭,則根本不需要它。

您需要確保僅在有發布請求時才執行代碼。 您可以通過將所有內容包裝在其中來實現:

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

如果您要發送郵件,僅當有人點擊“提交”按鈕時,才使用此功能

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

您必須檢查請求方法是否為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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM