2010-03-18 68 views
0

当我尝试执行以下代码以通过电子邮件发送联系人窗体的详细信息时,它不能正确执行。相反,当点击联系表单的Submit按钮时,它会在浏览器中显示下面的源代码。怎么了?要发送电子邮件不工作的代码

<?php 
    error_notice(E_ALL^E_NOTICE); 

    $firstname = $_POST['fname']; 
    $emailaddress = $_POST['eaddress']; 
    $mobile = $_POST['cellno']; 
    $phone = $_POST['landline']; 
    $country = $_POST['ucountry']; 
    $city = $_POST['ucity']; 
    $subjects = $_POST['usubjects']; 
    $message = $_POST['umessage']; 

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = $subjects; 

function died($error) { 
     // your error code can go here 
     echo "We are very sorry, but there were error(s) found with the form your submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if(!isset($firstname) || 
     !isset($emailaddress) || 
     !isset($subjects) || 
     !isset($message)) { 
     died('We are sorry, but there appears to be a problem with the form your submitted.');  
    } 

    $error_message = ""; 
    $email_exp = "^[A-Z0-9._%-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$"; 
    if(!eregi($email_exp,$emailaddress)) { 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 
    $string_exp = "^[a-z .'-]+$"; 
    if(!eregi($string_exp,$firstname)) { 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
    } 

    if(strlen($message) < 2) { 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
    } 
    $string_exp = "^[0-9 .-]+$"; 
    if(!eregi($string_exp,$phone)) { 
    $error_message .= 'The Telphone Number you entered does not appear to be valid.<br />'; 
    } 
    if(strlen($error_message) > 0) { 
    died($error_message); 
    } 
    $email_message = "Form details below.\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "First Name: ".clean_string($firstname)."\n"; 
    $email_message .= "Last Name: ".clean_string($mobile)."\n"; 
    $email_message .= "Email: ".clean_string($emailaddress)."\n"; 
    $email_message .= "Telephone: ".clean_string($phone)."\n"; 
    $email_message .= "City: ".clean_string($city)."\n"; 
    $email_message .= "Telephone: ".clean_string($country)."\n"; 
    $email_message .= "Comments: ".clean_string($message)."\n"; 

    // create email headers 
    $headers = 'From: '.$email_from."\r\n". 
    'Reply-To: '.$email_from."\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    @mail($email_to, $email_subject, $email_message, $headers); 
?> 

回答

4

如果显示源代码,这意味着服务器不会将该文件识别为PHP代码。原因可能不同。首先(很可能)文件的扩展名是无法识别的。第二个选项 - 服务器不允许PHP

+0

扩展名为.PHP。我在本地测试文件,并且它不在任何服务器上。 – RKh 2010-03-18 13:33:47

+2

@RPK如果它不在服务器上,那里可能存在你的问题:-) – richsage 2010-03-18 13:36:21

+1

没错。那么你的机器上是否安装了任何类型的服务器?像Apache或IIS一样? 您如何输入网址?使用http:// localhost/xxxx或只是在浏览器中打开页面文件? – 2010-03-18 13:38:06

2

如果您在浏览器中看到源代码,可能是Apache没有处理该文件,而是将其作为纯文本提供给浏览器。您的文件应该以.php结尾,并且您的Apache服务器必须配置为使用mod_php来处理.php文件。

从这里开始: http://dan.drydog.com/apache2php.html

UPDATE

如果你确实只是为了测试你的本地计算机上的这些文件,PHP不能工作。它是一种服务器端语言;也就是说,它需要服务器来解释文件的PHP部分并将输出发送到您的浏览器。您需要安装XAMPPWAMP或查找第三方托管服务,以便您上传PHP文件。

更新2

我们不能用更多的错误信息,解决您的脚本。开始大量减少脚本的复杂性,直到解决错误,然后开始以小步骤重新引入功能,直到再次遇到错误。然后你会知道什么是错的,并希望如何解决它。

一旦我注意到的功能error_notice没有定义,它也不是内置于PHP。从评论这条线开始。

+0

请看我对Tomasz的评论。 – RKh 2010-03-18 15:11:08

0

1)确保php模块已经编译并运行在你的apache上。

的LoadModule php5_module /usr/lib/apache2-prefork/mod_php5.so

运行的apachectl -t -D DUMP_MODULES:显示所有加载的模块,以查看所有加载的模块。

2)确保处理程序在你的Apache AddHandler的应用程序/ X的httpd - PHP的设置.php4 AddHandler的应用程序/ X的httpd - PHP .php5 AddHandler的应用程序/ X的httpd - PHP .PHP

3)通过在服务器上运行一个简单的脚本来测试。 创建php文件phpinfo.php然后屁股 测试脚本是否运行,如果没有,再次再次检查Apache配置...