发送电子邮件不起作用的代码

时间:2010-03-18 13:27:08

标签: php

当我尝试执行以下代码以通过电子邮件发送联系表单详细信息时,它无法正常执行。相反,当单击联系表单的“提交”按钮时,它只会在浏览器中显示以下源代码。怎么了?

<?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 = "abc@example.com";
    $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._%-]+@[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);  
?>

3 个答案:

答案 0 :(得分:4)

如果显示源代码,则表示服务器无法将此文件识别为PHP代码。原因可能不同。首先(最有可能)文件的扩展名无法识别。第二个选项 - 服务器不允许PHP

答案 1 :(得分:2)

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

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

<强>更新

如果您确实只是在本地计算机上测试这些文件,那么PHP无法运行。它是一种服务器端语言;也就是说,它需要服务器来解释文件的PHP部分并将输出发送到您的浏览器。您需要安装XAMPPWAMP或找到允许您上传PHP文件的第三方托管服务。

更新2

我们无法使用有关错误的更多信息修复您的脚本。开始逐步降低脚本的复杂性,直到错误得到解决,然后开始以小步骤重新引入功能,直到再次遇到错误。然后你会知道出了什么问题,并希望如何解决它。

一旦我注意到,函数error_notice未定义,也不是内置于PHP。首先评论该行。

答案 2 :(得分:0)

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

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配置......