2014-06-06 71 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

相关问题