2012-01-15 141 views
0

看起來像正在訪問send.php併發送空白電子郵件。需要這樣做,因此send.php不會發送電子郵件,除非在表單上使用提交按鈕。我被告知'isset $ _post'會幫助我,只是不清楚如何執行它。PHP聯繫表空白電子郵件

這裏的send.php代碼:

<?php 

$recipient = '[email protected]'; 

$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: [email protected]\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"; 
     } 

    } 

?> 
+0

你怎麼打電話send.php? – cbarg 2012-01-15 02:14:20

+0

pepsipaul 2012-01-15 04:04:48

回答

0

你必須請檢查是否請求方法是POST 。如果是這樣,你應該發送電子郵件:

<?php 

$recipient  =   '[email protected]'; 

$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: [email protected]\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); 
} 
+0

太棒了,完美無缺! – pepsipaul 2012-01-15 06:34:40

0

你需要在標題行結束時使用\r\n。但是如果你只有一個頭,你根本不需要它。

0

您需要確保代碼僅在發佈帖子請求時執行。你可以通過包裝都做到這一點:

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

試過,doesn沒有工作。認爲這是由於其他if語句導致的問題。嘗試做:<?php if($ _SERVER ['REQUEST_METHOD'] ==='POST'):?>在頂部,雖然沒有工作。 – pepsipaul 2012-01-15 02:57:15

0

如果你想發送的郵件,只有當有人點擊提交按鈕,只需使用該

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

添加到您的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"上面這段代碼將停止