2012-04-05 113 views
1

我有一個表單,我想用它來收集用戶信息。我有一個PHP獲取信息和存儲在變量中。當用戶點擊提交按鈕表單提交,併成功地重定向到謝謝頁面。 Bu出於某種原因,我沒有按預期收到電子郵件。有人可以解釋我做錯事的地方嗎?使用php和html格式發送電子郵件

下面是我的HTML和PHP文件。我將在稍後添加JavaScript驗證。在這一刻,我只是想讓表單提交工作。

PHP

<?php 

//This page should not be accessed directly. Need to submit the form. 
if(!isset($_POST['submit'])){ 
echo "ERROR. This page cannot be accessed directly"; 
} 

//Variables from the form 
$name    = $_POST['fullname']; 
$ageGroup   = $_POST['ageGroup']; 
$gender    = $_POST['gender']; 
$visit    = $_POST['visit']; 
$purposeOfVisit  = $_POST['purposeOfVisit']; 
$otherReferrer  = $_POST['otherReferrer']; 
$member    = $_POST['member']; 
$socialMedia  = $_POST['socialMedia']; 
$difficulties  = $_POST['difficulties']; 
$easeOfUse   = $_POST['easeOfUse']; 
$whatDifficulties = $_POST['whatDifficulties']; 
$specialCondition = $_POST['specialCondition']; 
$browser   = $_POST['browser']; 
$preferredFormat = $_POST['preferredFormat']; 
$contentsForWebsite = $_POST['contentsForWebsite']; 
$oldContents  = $_POST['oldContents']; 
$expectations  = $_POST['expectations']; 

$message = "Fullname:"; // will compose later 

// Compose email 
$email_from = "[email protected]"; 
$email_subject = "Infinity User Questionnaire"; 
$email_body = "You have received a new message from the user $name.\n". 
"Here is the message:\n $message". 

$to = "[email protected]"; 

$headers = "From: $email_from \r\n"; 

//Send the email! 
mail($to,$email_subject,$email_body,$headers); 

//done. redirect to thank-you page. 
header('Location: thankyou.html'); 


if(IsInjected($visitor_email)) 
{ 
    echo "Bad email value!"; 
    exit; 
} 

// Function to validate against any email injection attempts 
function IsInjected($str) 
{ 
    $injections = array('(\n+)', 
      '(\r+)', 
      '(\t+)', 
      '(%0A+)', 
      '(%0D+)', 
      '(%08+)', 
      '(%09+)' 
     ); 
    $inject = join('|', $injections); 
    $inject = "/$inject/i"; 
    if(preg_match($inject,$str)) 
    { 
    return true; 
    } 
    else 
    { 
    return false; 
    } 
} 
?>  

,如果你願意,你可以下載的index.php here

非常感謝。

+1

是在網站上或您的本地網站上的網站? – Kristian 2012-04-05 23:29:46

+0

確定'$ _POST'中沒有任何內容後,您不會阻止加載代碼。您只能添加警告消息。始終啓用error_reporting。 – PeeHaa 2012-04-05 23:35:00

+0

它在我的本地主機上。 – Subash 2012-04-05 23:36:50

回答

0

的問題是與我的託管服務。我不得不給他們一個電話,他們爲我修好了。

+0

請[將此答案標記爲接受的答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work ),這樣這個問題就不會顯示爲「未答覆」。 – Xenon 2012-04-25 10:24:25

0

我建議你使用的PHPMailer從http://phpmailer.worxware.com/發送郵件。使用PHPMailer,您可以跟蹤錯誤,以HTML和TEXT發送郵件,更改編碼和管理單詞warpers。

而且,有人說,你的腳本並不妨礙它被直接執行,我認爲這頭必須用剛換行字符完成「\ n」,而不是回到馬車「\ r」。

+1

雖然PHPMailer很好,但它並沒有回答原來的問題。此外,PHPMailer仍然使用'mail()'... – Xenon 2012-04-05 23:52:00

3

由於這是在本地機器上,你需要在php.ini首先配置mail()。例如,你可以將其配置爲使用ISP的SMTP服務器:

[mail function] 
SMTP = smtp.isp.net 
smtp_port = 25 
sendmail_from = [email protected] 

欲瞭解更多信息,請參閱PHP Documentation

編輯:我只是測試你對我的一個服務器代碼,並如預期發送的電子郵件。這進一步導致我相信你如何配置mail()函數有什麼問題。