为什么我的Form邮件脚本不起作用?

时间:2014-06-06 19:53:38

标签: php forms email

我已经在互联网上搜索了一个脚本,让表格发送到您的电子邮箱。但是当我按下提交按钮时,我会来到感谢页面。所以应该发送。但它不是。 有人可以帮助我吗?

发送脚本:

<?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 = "dj-sam@live.nl";

/*
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" );
}
?>

2 个答案:

答案 0 :(得分:1)

PHP邮件方法在manual上定义为

mail(string $ to,string $ subject,string $ message [,string $ additional_headers [,string $ additional_parameters]])

将代码映射为

$ to = $ webmaster_email

$ subject = Nieuwe voorspelling ingestuurd!

$ message = $ naam

$ additional_headers = $ wedstrijd1

$ additional_parameters = $ wedstrijd2

我猜你希望$ wedstrijd1等成为消息的一部分

因此,在我看来,从from返回信息的最佳方法是将所有变量放在格式化的消息中以便发送

$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传统上用于设置From,Cc和Bcc等

答案 1 :(得分:0)

您滥用邮件命令。如果您查看documentation for mail,可以看到您现在覆盖了其他标题。如果您没有在附加标头中提供FROM地址,则无法发送。如果没有附加参数,它将使用php配置文件中的任何内容作为默认值。

您传入的参数似乎是作为电子邮件的邮件正文的一部分,但是mail命令只接受一个字符串作为邮件正文。您必须在调用邮件之前连接字符串输入,并将整个主题作为单个参数传递。

<?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 = "dj-sam@live.nl";

/*
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" );
    }
}
?>