Some mailers won't relay; that is, they won't send if the "From" address isn't for a domain they service. Some mailers won't accept relayed e-mails. Use an e-mail address of your own for "From" and the user's e-mail just in the "Reply-To" header. If you have control over your domain records, set appropriate SPF records. Don't forget to check your junk e-mail folder.
Check the result of the call to mail—though note that this won't tell you if the e-mail was delivered successfully, just that it was accepted it for delivery. If mail returns false, output a notice that the e-mail couldn't be sent. If you need more information, use a more featureful mail extension or package, such as imap or PEAR's Mail.
Don't use die when outputting HTML. It will result in invalid HTML. Instead, you can use an if statement within the <body> to check whether there is an error message to output.
The POSIX regex functions are deprecated. Use PCRE (or the filter functions) instead.
<br/> and <center> aren't semantic. Use structurally appropriate elements for HTML and use CSS for presentation.
Use [php] or [html] BBCode tags rather than [code] to mark-up PHP or HTML, respectively.
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"
>
<html>
<head>
<title>Sending Message</title>
</head>
<body>
<?php
if(isset($_POST['email'])) {
# edit the 3 variables below as required
$from="e-mail address in server's domain";
$recipient = 'my email goes here';
$subject = empty($_POST['subject'])
? 'Contact Form' // edit here
# could also remove matching characters
: preg_replace_callback('/[^\p{L} -\[\]-~]+/u', 'urlencode', trim($_POST['subject']));
$fields = array('name' => array(
'filter' => FILTER_VALIDATE_REGEXP,
'options' => array('regexp' => "/^[\p{L} .,'-]+\$/i"),
'description' => "Valid characters are letters, periods, spaces, dashes, apostrophes, commas."),
),
'email' => FILTER_VALIDATE_EMAIL,
'comments' => array(
'filter' => FILTER_VALIDATE_REGEXP,
'options' => array('regexp' => '/(\S{3}\s+){3}/'),
'description' => 'Must contain at least three words at least three letters long.')
# or: 'description' => 'Must be longer.'
);
# filter ensures name & email are safe from injection
$data = filter_var_array(array_map('trim', $_POST), $fields);
$message[] = "Form details below.\n";
foreach ($data as $field => $value) {
if (is_null($value)) {
$errors[$field] = "Field '$field' is empty.";
} elseif ($value === False) {
$value = htmlspecialchars($_POST[$field]);
$message = "The value '$value' for field '$field' is invalid.";
if (isset($fields[$name]['description'])) {
$message .= ' ' . $fields[$name]['description'];
}
$errors[$field] = $message;
} else {
$message[] = ucfirst($field) . ': ' . $value;
}
}
if ($errors) {
if (count($errors) > 1) {
$plural = 's';
} else {
$plural = '';
}
?>
<p>We are very sorry, but your submission had the following error<?php echo $plural ?>:
<ul>
<?php foreach ($errors as $error) { ?>
<li><?php echo $error; ?></li>
<?php } ?>
</ul>
Please go back and fix <?php echo ($plural ? 'them' : 'it') ?>.
</p>
<?php
} else {
// create email headers
$headers = "From: $from\r\nReply-To: $data[email]\r\nContent-type: text/plain\r\nX-Mailer: PHP/" . phpversion();
$message = implode("\n", $message);
if (mail($recipient, $subject, $message, $headers);) {
?><p>Your message was accepted for delivery.</p><?php
} else {
?><p>Your message wasn't accepted by the e-mail server.</p><?php
}
}
}
?>
</body>
</html>