Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8 Changes #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/)

Expand Down
80 changes: 55 additions & 25 deletions contact-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,71 @@
// BEGIN CONFIGURATION ////////////////////////////////////////////////

define('EMAIL_TO', '[email protected]');
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 = '
<html><body>
<html>
<body>
<div style="max-width:800px;">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr><td style="border-bottom: solid 1px #CCC; font-size:18px; font-weight:bold; padding:10px;" colspan="2">'.$email_subject.'</td></tr>
<tr><td valign="top" style="padding:10px; border-bottom: solid 1px #CCC;" valign="top"><b>Name:</b></td><td style="padding:10px; border-bottom: solid 1px #CCC;">'.$name.' ('.$email.')</td></tr>
<tr><td valign="top" style="padding:10px; border-bottom: solid 1px #CCC;" valign="top"><b>Message:</b></td><td style="padding:10px; border-bottom: solid 1px #CCC;">'.$message.'</td></tr>
<tr>
<td style="border-bottom: solid 1px #CCC; font-size:18px; font-weight:bold; padding:10px;" colspan="2">'.EMAIL_SUBJECT.'</td>
</tr>
<tr>
<td valign="top" style="padding:10px; border-bottom: solid 1px #CCC;" valign="top"><b>Name:</b></td><td style="padding:10px; border-bottom: solid 1px #CCC;">'.$name.' ('.$email.')</td>
</tr>
<tr>
<td valign="top" style="padding:10px; border-bottom: solid 1px #CCC;" valign="top"><b>Message:</b></td><td style="padding:10px; border-bottom: solid 1px #CCC;">'.$message.'</td>
</tr>
</table>
</body></html>
</div>
</body>
</html>
';

// 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 = '';
Expand All @@ -63,16 +85,24 @@
<?php echo $msg; ?>

<form method="post">

<p>Name:<br><input type="text" name="name" value="<?php echo $name; ?>" /></p>

<p>Email:<br><input type="text" name="email" value="<?php echo $email; ?>" /></p>

<p>Message:<br><textarea name="message" rows="5" cols="40" /><?php echo $message; ?></textarea></p>

<?php if (CAPTCHA_ENABLED != '0') { ?>
<p><?php echo CAPTCHA1; ?> + <?php echo CAPTCHA2; ?> = ?<br><input type="text" name="captcha" /></p>

<p><?php echo CAPTCHA1; ?> + <?php echo CAPTCHA2; ?> = ? <br><input type="text" name="captcha" /></p>

<input type="hidden" name="captcha1" value="<?php echo CAPTCHA1; ?>" />
<input type="hidden" name="captcha2" value="<?php echo CAPTCHA2; ?>" />
<?php } ?>

<p><input type="submit" value="Submit" /></p>

</form>

</body>
</html>
</html>