Powinno działać, nie sprawdzałem. Jeden "bug" - jak jakieś pole będzie puste, to wywali błąd "nie wszystkie pola wypełnione" - ale nie pokaże np. że e-mail jest błędny - łatwo to zmienić - pokmiń.
Właśnie to jest urok wielu if/else - łatwo pomylić "{" lub "}" - i katastrofa gotowa...
Poczytaj o wyjątkach, spróbuj zrobić proste funkcje, które np. sprawdzają poprawność pól etc.
<?php
/**
* A PHP contact form.
* Author: tom@ofzenandcomputing.com
* Last revision: 11/19/2007 01:03
*
* Mail header injection prevention based on comments from:
* <a href="http://www.php.net/mail" target="_blank">http://www.php.net/mail</a>
*
* E-mail validation regex from:
* <a href="http://www.regular-expressions.info/email.html" target="_blank">http://www.regular-expressions.info/email.html</a>
*/
// Replace you@example.com with your own e-mail address.
define('YOUR_EMAIL', 'db4all@gmail.com'); // If the user does not fill in a subject, this will be used.
define('DEFAULT_SUBJ', 'A message from your contact form'); // This is the maximum length of a subject, in characters.
if (isset($_COOKIE['formularz'])) { echo '<u>kolejny wpis za 5min.</u>'; // nie przyjmuj formularza
} elseif (isset($_POST['mail'])) {
if (preg_match('/(%0A|%0D|\n+|\r+)/i', $_POST['subj'])) { $errors[] = 'Your subject contains illegal characters.';
} else {
$subj = DEFAULT_SUBJ;
} else {
$subj = substr($_POST['subj'], 0
, 1000
); }
}
$errors[] = '<u>Nie zostały wypełnione wszystkie wymagane pola.</u>';
} else {
if (!preg_match('/^[a-ząćęłńóśźż]+$/ui', $_POST['imie'])) { $errors[] = '<u>Imię zostało wpisane niepoprawnie.</u>';
}
if (!preg_match('/^([a-z0-9]{1})([^\s\t\.@]*)((\.[^\s\t\.@]+)*)@([a-z0-9]{1})((([a-z0-9-]*[-]{2})|([a-z0-9])*|([a-z0-9-]*[-]{1}[a-z0-9]+))*)((\.[a-z0-9](([a-z0-9-]*[-]{2})|([a-z0-9]*)|([a-z0-9-]*[-]{1}[a-z0-9]+))+)*)\.([a-z0-9]{2,6})([.]?)$/Diu', $_POST['email'])) { $errors[] = '<u>Adres e-mail został wpisany niepoprawnie.</u>';
}
if (!preg_match('/^(http|ftp)([s]{0,1}):\/\/([a-z0-9]{1})((([a-z0-9-]*[-]{2})|([a-z0-9])*|([a-z0-9-]*[-]{1}[a-z0-9]+))*)((\.[a-z0-9](([a-z0-9-]*[-]{2})|([a-z0-9]*)|([a-z0-9-]*[-]{1}[a-z0-9]+))+)*)(\.([a-z0-9]{2,6})){0,1}((:[0-9]){0}|(:[1-9]{1}[0-9]*))\//iu', $_POST['adres'])) { $errors[] = '<u>Adres strony został wpisany niepoprawnie.</u>';
}
if (!preg_match('/^(http|ftp)([s]{0,1}):\/\/([a-z0-9]{1})((([a-z0-9-]*[-]{2})|([a-z0-9])*|([a-z0-9-]*[-]{1}[a-z0-9]+))*)((\.[a-z0-9](([a-z0-9-]*[-]{2})|([a-z0-9]*)|([a-z0-9-]*[-]{1}[a-z0-9]+))+)*)(\.([a-z0-9]{2,6})){0,1}((:[0-9]){0}|(:[1-9]{1}[0-9]*))\//iu', $_POST['subj'])) { $errors[] = '<u>Odsyłacz do statystyk został wpisany niepoprawnie.</u>';
}
if (preg_match('/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i', $_POST['body'])) { $errors[] = 'Your message body contains invalid characters.';
}
}
for ($i = 0; $i < count($errors); $i++) { printf('%s<br />', $errors[$i]); }
} else {
$headers = sprintf("From: %s", $_POST['imie']); $imie = $_POST['imie'];
$adres = $_POST['adres'];
$cialo = $_POST['body'];
$email = $_POST['email'];
$subj = $_POST['subj'];
$temat = 'Reklama w serwisie Psychotechnik';
$wszystko = "Imię: " . $imie . "\nE-mail: " . $email . "\nAdres strony: " . $adres . "\nOdsyłacz statystyk: " . $subj . "\nTreść wiadomości:\n" . $cialo;
if (mail(YOUR_EMAIL
, $temat, $wszystko, $headers)) { print '<p>Your message was sent.</p>'; } else {
print '<p>An error occurred while we were attempting to' . ' send your message. Please try again later.</p>'; }
}
}
?></p>
<form action="
<?php echo $_SERVER['PHP_SELF']; ?>" method="post" accept-charset="utf-8">
*Imię:<br />
<input type="text" name="imie" size="25" /><br />
*E-mail:<br />
<input type="text" name="email" size="25" /><br />
*Adres strony:<br />
<input type="text" name="adres" size="25" /><br />
*Odsyłacz do statystyk strony:<br />
<input type="text" name="subj" size="40" maxlength="1000" /><br />
Treść:<br />
<textarea name="body" cols="32" rows="10" ></textarea><br />
<input type="submit" name="mail" value="Wyślij wiadomość" class="button" />
</form>