Form Installation

Adjusting the PHP & CSS

There are various adjustments you can easily make to optionally personalise your contact form.

Do not edit the HTML apart from labels, if necessary. If you do not, the form may not work!

We strongly recommend that you make at least one copy of your original form document and make any alterations to the copy.

Test altered copies before overwriting your original form document.

1. Form Name in emails

In the PHP section at the top go to Line 252 and change Website Form to something more meaningful, and likewise with New Form Submission in Line 256. If your code editor does not show Line Numbers, you will find these near the bottom of the PHP code.

2. CSS Adjustments

Remove the body section completely in the CSS if you are installing the form into your website's contact page. If you want to use the form 'as is' leave it in, however, you can alter the font family if you wish.

3. Changing Colours

You can edit these as you wish:

  • background-color and background
  • color
  • box-shadow - edit or delete if not wanted

4. Adding Policies & Cookie Scripts Links

For standalone web pages

If you are not embedding this form into an existing web page and using this form as a standalone web page you will need to add links for Policies and Cookie Settings to comply with the law.

The CSS

Add this at the end of your style sheet.


.footer-links {
  font-family: Arial, sans-serif;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 15px 0;
  font-size: .9em;
  max-width: 900px;
  margin: 0 auto 40px;
}

.footer-links a {
  color: #007bff;
  text-decoration: none;
}

.footer-links a:hover {
  text-decoration: underline;
}

The HTML

Add this just before the </body> closing tag making sure it is below this code <?php endif; ?>.


<div class="footer-links">
  <a href="path-to-your-policies.php" target="blank">Policies</a>
  <a href="#" "cookie id or onclick code here">Cookie Settings</a> <!-- Different cookie scripts use different code, refer to supplier's documentation -->
</div>

5. Translating from English


In the HTML section

Change the form field labels.


In the PHP section at the top:

Look for the 'Translate' block comments and change the text with your translation.

Work on a copy of the form document so you don't accidentally overwrite the orignal.

Depending on your skills it may be better not to copy and paste the code below into your original document, but to transfer the translations.

If you are an experienced developer and choose to copy and paste it into your original document don't forget to update the SMTP settings.

Test the form to see if you have the desired results.

When you are satisfied with the test overwrite the original OR if you need an English version as well, rename the document.


    $ipAddress = $_SERVER['REMOTE_ADDR'] ?? 'Unknown';

    $rl = rate_limit_check_and_record($ipAddress);
    if (is_array($rl) && isset($rl['allowed']) && !$rl['allowed']) {
        if ($rl['reason'] === 'burst_60') {
            $message = 'Please wait a moment before sending again.';  /* Translate */
        } elseif ($rl['reason'] === 'burst_120') {
            $message = 'You have sent messages too quickly. Please try again shortly.';    /* Translate */
        } elseif ($rl['reason'] === 'daily_cap') {
            $message = 'You have reached the daily message limit. Please try again tomorrow.';     /* Translate */
        } else {
            $message = 'Rate limit exceeded. Please try again later.';     /* Translate */
        }
        $messageType = 'error';
    }

    foreach (['name', 'email', 'subject'] as $field) {
        if (isset($_POST[$field]) && preg_match("/\r|\n/", $_POST[$field])) {
            die('Invalid input');
        }
    }

    $body = "";
    if (!isset($_POST['name']) || trim($_POST['name']) === '') {
        $message = "Error: 'Name' field is required.";    /* Translate */
        $messageType = 'error';
    } else {
        $body .= "Name: " . trim($_POST['name']) . "\n\n";
    }
    if (!isset($_POST['email']) || !filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL)) {
        $message = "Error: Valid 'Email' field is required.";     /* Translate */
        $messageType = 'error';
    } else {
        $body .= "Email: " . trim($_POST['email']) . "\n\n";
    }
    if (!isset($_POST['phone']) || !preg_match('/^[\+\d\s]+$/', trim($_POST['phone']))) {
        $message = "Error: Valid 'Phone' field is required (only digits, spaces, +).";    /* Translate */
        $messageType = 'error';
    } else {
        $body .= "Phone: " . trim($_POST['phone']) . "\n\n";
    }
    if (!isset($_POST['subject']) || trim($_POST['subject']) === '') {
        $message = "Error: 'Subject' field is required.";    /* Translate */
        $messageType = 'error';
    } else {
        $body .= "Subject: " . trim($_POST['subject']) . "\n\n";
    }
    if (!isset($_POST['message']) || trim($_POST['message']) === '') {
        $message = "Error: 'Message' field is required.";     /* Translate */
        $messageType = 'error';
    } else {
        $body .= "Message: " . trim($_POST['message']) . "\n\n";
    }

    date_default_timezone_set('Europe/London');     /* Translate and change time zone area if necessary*/
    $body .= "Date: " . date('d-m-Y') . " / Time: " . date('H:i:s') . " \n\n";     /* Translate Date and Time*/
    $body .= "IP Address: " . $ipAddress . "\n";    /* Translate IP Address*/

    $combinedForSpam = (isset($_POST['name']) ? $_POST['name'] . ' ' : '') .
                       (isset($_POST['subject']) ? $_POST['subject'] . ' ' : '') .
                       (isset($_POST['message']) ? $_POST['message'] . ' ' : '') .
                       (isset($_POST['email']) ? $_POST['email'] : '');

    if (contains_spam_keywords($combinedForSpam)) {
        $message = "Error: Message appears to contain disallowed content.";    /* Translate */
        $messageType = 'error';
    }

    if (!isset($messageType) || $messageType !== 'error') {
        $mail = new PHPMailer(true);
        try {
            $mail->isSMTP();
            $mail->Host = 'host';  /* Use the credentials from you original form */
            $mail->SMTPAuth = true;
            $mail->Username = 'user';    /* Use the credentials from you original form */
            $mail->Password = 'password';    /* Use the credentials from you original form */
            $mail->SMTPSecure = 'tls';    /* Use the setting from you original form */
            $mail->Port = 587;     /* Use the credentials from you original form */

            $mail->setFrom('from email address', 'Website Form');    /* Use the email address from you original form */
            $mail->addAddress('to email address');    /* Use the email address from you original form */

            $mail->isHTML(false);
            $mail->Subject = 'New Form Submission';     /* Translate New Form Submission*/
            $mail->Body = $body;

            $mail->send();
            $message = "Message sent!";    /* Translate */
            $messageType = 'success';
        } catch (Exception $e) {
            $message = "Mailer Error: {$mail->ErrorInfo}";
            $messageType = 'error';
        }
    }
}
?>