2014-06-06 75 views
0

我在網上搜索了一個腳本讓它發送一個表格到你的郵箱。但是當我按下提交按鈕時,我會來到感謝頁面。所以它應該發送。但事實並非如此。 有人可以幫我嗎?爲什麼我的表單郵件腳本不起作用?

發送腳本:

<?php 
/* 
This first bit sets the email address that you want the form to be submitted to. 
You will need to change this value to a valid email address that you can access. 
*/ 
$webmaster_email = "[email protected]"; 

/* 
This bit sets the URLs of the supporting pages. 
If you change the names of any of the pages, you will need to change the values here. 
*/ 
$feedback_page = "feedback_form.html"; 
$error_page = "error_message.html"; 
$thankyou_page = "thank_you.html"; 

/* 
This next bit loads the form field data into variables. 
If you add a form field, you will need to add it here. 
*/ 
$naam = $_REQUEST['naam'] ; 
$wedstrijd1 = $_REQUEST['wedstrijd1'] ; 
$wedstrijd2 = $_REQUEST['wedstrijd2'] ; 
$wedstrijd3 = $_REQUEST['wedstrijd3'] ; 
$wedstrijd4 = $_REQUEST['wedstrijd4'] ; 

/* 
The following function checks for email injection. 
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list. 
*/ 
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; 
} 
} 

// If the user tries to access this script directly, redirect them to the feedback form, 
if (!isset($_REQUEST['naam'])) { 
header("Location: $feedback_page"); 
} 

// If the form fields are empty, redirect to the error page. 
elseif (empty($naam) || empty($wedstrijd1) || empty($wedstrijd2) || empty($wedstrijd3) || empty($wedstrijd4)) { 
header("Location: $error_page"); 
} 

// If email injection is detected, redirect to the error page. 
elseif (isInjected($naam)) { 
header("Location: $error_page"); 
} 

// If we passed all previous tests, send the email then redirect to the thank you page. 
else { 
mail("$webmaster_email", "Nieuwe voorspelling ingestuurd!", 
$naam, 
$wedstrijd1, 
$wedstrijd2, 
$wedstrijd3, 
$wedstrijd4); 
header("Location: $thankyou_page"); 
} 
?> 

回答

1

PHP的郵件方法在manual作爲

郵件(字符串$到,字符串$主題,字符串$消息[,字符串$ additional_headers [,字符串$ additional_parameters]])

定義

它映射到你的代碼爲

$到= $ webmaster_email

$主題= NIEUWE voorspelling ingestuurd!

$消息= $ NAAM

$ additional_headers = $ wedstrijd1

$ additional_parameters = $ wedstrijd2

,我猜你想$ wedstrijd1等成爲消息的一部分

因此,我認爲最好的方式來從返回的信息將是將所有變量的格式化消息發送

$message = "Name = {$naam}\n" 
    ."Competition 1 = {$wedstrijd1}\n" 
    ."Competition 2 = {$wedstrijd2}\n" 
    ."Competition 3 = {$wedstrijd3}\n" 
    ."Competition 4 = {$wedstrijd4}\n"; 

mail($webmaster_email, "Nieuwe voorspelling ingestuurd!", $message); 

的additional_headers傳統上用於設置從,抄送喜歡,和密件抄送

0

您濫用郵件命令。如果你看一下documentation for mail,你可以看到你現在正在重寫額外的頭文件。如果您沒有將其添加到附加標題中的FROM地址,則它將無法發送。沒有額外的參數,它會使用php配置文件中的任何內容作爲默認參數。

看來您傳遞的參數是作爲電子郵件的郵件正文的一部分,但是郵件命令僅將單個字符串作爲郵件正文。您必須在調用郵件之前連接字符串輸入,並將整個主題作爲單個參數傳遞。

<?php 
/* 
This first bit sets the email address that you want the form to be submitted to. 
You will need to change this value to a valid email address that you can access. 
*/ 
$webmaster_email = "[email protected]"; 

/* 
This bit sets the URLs of the supporting pages. 
If you change the names of any of the pages, you will need to change the values here. 
*/ 
$feedback_page = "feedback_form.html"; 
$error_page = "error_message.html"; 
$thankyou_page = "thank_you.html"; 

/* 
This next bit loads the form field data into variables. 
If you add a form field, you will need to add it here. 
*/ 
$naam = $_REQUEST['naam'] ; 
$wedstrijd1 = $_REQUEST['wedstrijd1'] ; 
$wedstrijd2 = $_REQUEST['wedstrijd2'] ; 
$wedstrijd3 = $_REQUEST['wedstrijd3'] ; 
$wedstrijd4 = $_REQUEST['wedstrijd4'] ; 

/* 
The following function checks for email injection. 
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list. 
*/ 
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; 
} 
} 

// If the user tries to access this script directly, redirect them to the feedback form, 
if (!isset($_REQUEST['naam'])) { 
header("Location: $feedback_page"); 
} 

// If the form fields are empty, redirect to the error page. 
elseif (empty($naam) || empty($wedstrijd1) || empty($wedstrijd2) || empty($wedstrijd3) || empty($wedstrijd4)) { 
header("Location: $error_page"); 
} 

// If email injection is detected, redirect to the error page. 
elseif (isInjected($naam)) { 
header("Location: $error_page"); 
} 

// If we passed all previous tests, send the email then redirect to the thank you page. 
else { 
    $message = $naan . $wedstrijd1 . $wedstrijd2 . $wedstrijd3 . $wedstrijd4; 
    if(mail("$webmaster_email", "Nieuwe voorspelling ingestuurd!", $message)) 
    { 
     header("Location: $thankyou_page"); 
    } 
    else 
    { 
     header("Location: $error_page"); 
    } 
} 
?> 
+0

但是,當我刪除所有「wedstrijd」 1,2,3和4的郵件將發送。我在談論的代碼://如果我們通過所有以前的測試,發送電子郵件然後重定向到謝謝頁面。 其他{ 電子郵件( 「$ webmaster_email」, 「NIEUWE voorspelling ingestuurd!」, $ NAAM, $ wedstrijd1, $ wedstrijd2, $ wedstrijd3, $ wedstrijd4); header(「Location:$ thankyou_page」); } – change1001

+0

@ change1001您是否使用開發服務器從您家發送它? –

+0

@AJHenderson那麼我該如何解決這個問題? (回覆您的編輯) – change1001

相關問題