I have successfully tested both PHP's and SMTP's functionality - both are functional.
However, I am trying to use a contact form on a webpage. When I test the form with its Submit button, the form just results in a HTTP 500 error, without any other information.
Here is the two scripts - the PHP where the form resides, and the PHP that is the form's action.
- Code: Select all
<?php
session_start();
function paint_value($field)
{
if (!empty($_SESSION["post"][$field]))
{
echo $_SESSION["post"][$field];
}
else { echo ""; }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body >
<p>Use the form below to route your email to the desired dept.<br />
* = required.<br />
</p>
<?php
//Check if errors were sent
if (!empty($_GET["error"]))
{
switch ($_GET["error"])
{
case "incomplete":
$field_labels = array("name" => "Name", "email" => "E-mail address", "comments" => "Comments");
$incomplete = explode(",", $_GET["fields"]);
?><div class="error">All required fields (*) were not completed. Please check the following fields:
<ul>
<?php foreach ($incomplete as $field) {
if (isset($field_labels[$field])) { echo "<li>" . $field_labels[$field] . "</li>"; }
} ?>
</ul></div><?php
break;
case "email_invalid":
?><div class="error">The email address entered does not appear to be a valid format</div><?php
break;
case "runtime_error":
?><div class="error">There was a problem processing your request. Please try again later.</div><?php
break;
}
}
?>
<form action="send.php" method="post">
<table>
<tr>
<td valign="top" >
<label for="destination">Destination *</label> </td>
<td valign="top">
<select name="destination" >
<option selected value="GeneralInfo"> General Info </option>
/**MORE OPTIONS HERE**/
</select> </td>
</tr>
<tr>
<td nowrap valign="top">
<label for="name">Your Name *</label> </td>
<td nowrap valign="top">
<input type="text" name="name" maxlength="50" size="30"> </td>
</tr>
<tr>
<td nowrap valign="top">
<label for="email">Your Email *</label> </td>
<td nowrap valign="top">
<input type="email" name="email" maxlength="80" size="30"> </td>
</tr>
<tr>
<td nowrap valign="top">
<label for="telephone">Telephone </label> </td>
<td nowrap valign="top">
<input type="tel" name="telephone" maxlength="30" size="30"> </td>
</tr>
<tr>
<td nowrap valign="top">
<label for="comments">Comments *</label> </td>
<td nowrap valign="top">
<textarea name="comments" maxlength="1000" cols="35" rows="10" ></textarea> </td>
</tr>
<tr>
<td nowrap colspan="2" style="text-align:right">
<br/> <input type="submit" value="Send Email"> </td>
</tr>
</table>
</form>
</body>
<!-- InstanceEnd --></html>
- Code: Select all
<?php
/** 1 **/
session_start();
/** 2 **/
//See if evil magic_quotes is enabled, and fix problems if it is
$quotes_on = (get_magic_quotes_gpc() || get_magic_quotes_runtime());
if ($quotes_on)
{
foreach ($_POST as $key => $value)
{
$_POST[$key] = stripslashes($value);
}
}
/** 3 **/
$_SESSION["post"] = $_POST;
/** 4 **/
//Load in the required Swift files
require_once "classes/Swift.php";
require_once "classes/Swift/Connection/SMTP.php";
/** 5 **/
//Create an empty array where we can catch any fields which were not filled in
$fields_not_set = array();
//Check if all POST data was sent, redirect with an error if not
if (empty($_POST["user_name"])) $fields_not_set[] = "user_name";
if (empty($_POST["email"])) $fields_not_set[] = "email";
if (empty($_POST["subject"])) $fields_not_set[] = "subject";
if (empty($_POST["comments"])) $fields_not_set[] = "comments";
//If $fields_not_set contains any values, then something wasn't filled in. Time to redirect.
if (!empty($fields_not_set))
{
//Read further down to see how we'll modify form.php to handle the error
header("Location: index.php?error=incomplete&fields=" . implode(",", $fields_not_set));
exit();
}
//Copy the POST data to standard globals
$name = $_POST["name"];
$email = $_POST["email"];
$telephone = $_POST["telephone"];
$comments = $_POST["comments"];
//setup the array for to addresses
$emailLookUp = array (
"GeneralInfo" => "xxx@xxx.com",
//more options here
);
if(isset($_POST['email'])) {
$email_to = $emailLookUp[ $_POST[ 'destination'] ];
/** 6 **/
//This is a RegExp I've adopted for validating email addresses. NOTE that it's NOT RFC compliant.
// Use another regexp at your own choice
$email_re = '(?#Start of dot-atom
)[-!#\$%&\'\*\+\/=\?\^_`{}\|~0-9A-Za-z]+(?:\.[-!#\$%&\'\*\+\/=\?\^_`{}\|~0-9A-Za-z]+)*(?#
End of dot-atom)(?:@(?#Start of domain)[-0-9A-Za-z]+(?:\.[-0-9A-Za-z]+)*(?#End of domain))?';
//Now check if the email address they gave is valid, redirect back to the form if not
if (!preg_match('/^' . $email_re . '$/', $email))
{
header("Location: index.php?error=email_invalid");
exit();
}
/** 8 **/
//Everything looks ok to send an email, create an instance of Swift
$swift = new Swift(new Swift_MailTransport("localhost," "25"));
/** 9 **/
//Now build your message body
$body = "Name: " . $name . "\n" . "Email: " . $email . "\n" . "Telephone:" . $telephone . "\n" . "Comments: " . $comments;
/** 11 **/
//Add the email body
$swift->addPart($body);
//Add Reply to
$swift->replyTo($email);
/** 12 **/
//Try sending the email. Redirect to success page on success, or form on failure
if ($swift->send($email_to, $email, $subject))
{
unset($_SESSION["post"]); //It worked, we have no reason to keep this dat
$swift->close();
header("Location: success.php"); /** 13 **/
exit();
}
else
{
$swift->close();
header("Location: index.php?error=runtime_error"); /** 13 **/
exit();
}
//End of script
?>
Can anyone give me a hand? I am at a loss as to where to start debugging/fixing this.


