diff --git a/README.md b/README.md index 8b53e82..8d14d88 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # PHP Contact Form with HTML Emails & Optional Captcha (Anti-Spam) Simple PHP contact form with HTML emails and an optional, math based captcha. +2022-04-11 : Updated to work with PHP 8 + ## Instructions [See blog article.](https://www.aaronfagan.ca/blog/2015/php-contact-form-with-html-emails-optional-captcha-anti-spam/) diff --git a/contact-form.php b/contact-form.php index bab6b70..b4c1455 100644 --- a/contact-form.php +++ b/contact-form.php @@ -2,49 +2,71 @@ // BEGIN CONFIGURATION //////////////////////////////////////////////// define('EMAIL_TO', 'your-email@address.com'); -define('EMAIL_SUBJECT', 'Test Subject'); -define('CAPTCHA_ENABLED', '1'); // 0 - Disabled, 1 - Enabled +define('EMAIL_SUBJECT', 'Test Form'); +define('CAPTCHA_ENABLED', '1'); // 0 - Disabled, 1 - Enabled // END CONFIGURATION //////////////////////////////////////////////// define('CAPTCHA1', rand(1,9)); define('CAPTCHA2', rand(1,9)); +if (empty($name)) $name = ''; +if (empty($email)) $email = ''; +if (empty($message)) $message = ''; +if (empty($msg)) $msg = ''; + if ($_POST) { - $name = $_POST['name']; - $email = $_POST['email']; - $message = $_POST['message']; - $captcha = $_POST['captcha']; + $name = $_POST['name']; + $email = $_POST['email']; + $message = $_POST['message']; + $captcha = $_POST['captcha']; $captcha1 = $_POST['captcha1']; $captcha2 = $_POST['captcha2']; -// If captcha disabled, set variable values to avoid error + // If captcha disabled, set variable values to avoid error. if (CAPTCHA_ENABLED == '0') { $captcha1 = '1'; $captcha2 = '1'; $captcha = '2'; } -// Error handling - if (empty($name) || empty($email) || empty($message)) { $msg = 'One or more fields is blank!'; } - else if (!$email == '' && (!strstr($email,'@') || !strstr($email,'.'))) { $msg = 'Your email address is not formatted correctly!'; } - else if (($captcha1 + $captcha2) != $captcha) { $msg = 'Anti-spam incorrect! Please try again.'; } + // Error handling. + if (empty($name) || empty($email) || empty($message)) { + $msg = 'One or more fields were left empty!'; + } + else if (!$email == '' && (!strstr($email, '@') || !strstr($email, '.'))) { + $msg = 'Your email address is not formatted correctly!'; + } + else if (($captcha1 + $captcha2) != $captcha) { + $msg = 'Your anti-spam answer is incorrect! Please try again.'; + } -// Build email headers + // Build email headers. else { - $headers = "From: ".$name." <".$email.">\r\n"; - $headers .= "Reply-To: ".$name." <".$email.">\r\n"; - $headers .= "MIME-Version: 1.0\r\n"; - $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; + $headers['MIME-Version'] = '1.0'; + $headers['Content-type'] = 'text/html;charset=UTF-8'; + $headers['Return-Path'] = EMAIL_TO; + $headers['From'] = $name.' <'.$email.'>'; + $headers['Reply-To'] = $name.' <'.$email.'>'; -// Build email body + // Build email body. $body = ' - + + +
- - - + + + + + + + + +
'.$email_subject.'
Name:'.$name.' ('.$email.')
Message:'.$message.'
'.EMAIL_SUBJECT.'
Name:'.$name.' ('.$email.')
Message:'.$message.'
- +
+ + '; -// Send the email, reset text boxes on form, and show success message + // Send the email, reset text boxes on form, and show success message. mail(EMAIL_TO, EMAIL_SUBJECT, $body, $headers); $name = ''; $email = ''; @@ -63,16 +85,24 @@
+

Name:

+

Email:

+

Message:

+ -

+ = ?

+ +

+ = ?

+ +

+
- \ No newline at end of file +