Payne's PHP Notes

Back to Payne's PHP Notebook.

8. E-Mail and Error Handling

E-Mail:

Syntax / Example Result
<form action="SendEMail.php" method="post">
To: <input type="text" name="to" /><br />
Subject: <input type="text" name="subject" /><br />
Message: <input type="text" name="message" /><br />
<input type="submit" />
</form>
To:
Subject:
Message:
<?php
// SendEMail.php
$to = $_REQUEST['to'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];

echo $to . "<br />";
echo $subject . "<br />";
echo $message . "<br />";

mail($to, $subject, $message);
echo "E-mail sent!";
?>
// Input field check.
filter_var($inputField, FILTER_SANITIZE_EMAIL);
filter_var($inputField, FILTER_VALIDATE_EMAIL);

Error Handling:

Syntax / Example Result
errorFunction(level, message, file, line, context);

die(message);
 
<?php

function errorHandle($level, $message)
{
  echo "Error[$level]: $message<br />";
  // Do something!
}

set_error_handler("errorHandle");

echo $unknownVariable . "<br />";

fopen("NonexsitedFile.txt", "r");

?>
Error[8]: Undefined variable: unknownVariable

Error[2]: fopen(NonexsitedFile.txt) [function.fopen]: failed to open stream: No such file or directory
<?php

function errorHandle2($level, $message)
{
  echo "Error[$level]: $message<br />";
}

set_error_handler("errorHandle2", E_USER_WARNING);

trigger_error("Error triggered!", E_USER_WARNING);
?>
Error[512]: Error triggered!