将PHP表单提交到电子邮件时出现内部服务器错误500

时间:2015-05-04 17:49:14

标签: php html forms internal-server-error

我正在尝试将此表单中的内容发送到电子邮件中。但是,它不起作用,当我提交它时会给我一个内部错误。这是我提交表单时运行的代码。

<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}


$name = $_POST['first_name']; // required

$job = $_POST['job']; // required

$company = $_POST['company']; // required

$email = $_POST['email']; // required

$telephone = $_POST['telephone']; // required

$psn = $_POST['psn']; // required

$un = $_POST['un']; // required

$weight = $_POST['weight']; // not required

$pieces = $_POST['pieces']; // required

$dgpacked = $_POST['dgpacked']; // required

$address = $_POST['address']; // required

$readytime = $_POST['readytime']; // required

$deliverytype = $_POST['deliverytype']; // required


if(IsInjected($email))
{
echo "Bad email value!";
exit;
}

$email_from = 'c.oswald@live.com';//<== update the email address
$email_subject = "New Quote Request submission";
$email_body = "You have received a new message from the $name with $company.\n".
"Here is the message:\n $message".

$to = "c.oswald@live.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";

//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: home.html');


// Function to validate against any email injection attempts
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;
  }
}

?> 

如果有更简单的东西,请帮助我,我只需要一些简单易懂的东西,但不是那么基本我会有安全或注射问题。

2 个答案:

答案 0 :(得分:0)

对于电子邮件验证,您可以轻松使用FILTER_VALIDATE_EMAIL。我认为这比使用你自己的功能要好。

我的代码中没有发现任何其他错误,所以我认为这是你的功能。否则可能导致服务器配置错误,然后我需要错误日志条目进行调查。如果您在Mac上使用MAMP,可以在/Applications/MAMP/logs/php_error.log找到错误日志。

出于邮寄目的,我可以向您推荐PHPMailer

(在第一个陈述中

if(!isset($_POST['submit']))

退出; 缺失。在标题重定向之后,还应该发生退出; 。此外,您应该检查客户端是否传递了所有必要的参数。)

答案 1 :(得分:0)

这将由于以下原因而发生:

代码,语法错误和显式代码中的无错误。

对于发送电子邮件,最好的php类是:https://github.com/PHPMailer/PHPMailer

这是最新的PHPMailer类。

下载此文件后,您可以看到mail.php文件。在那里你必须改变用户名,密码,收件人和消息等。

相关问题