How to build a contact form in PHP

18 Jan 2025

Alright, everything is set, you just finished your website. It looks polished, very professional. Everything until now as been about design and front-end. You now need to setup a contact form to receive your first enquiries from customers or users.

How contact forms works

Contact forms are basic forms that sends their content by mails or any platform to the website owner. So the website owner can then contact back people.

Why contact forms?

You cannot directly put your email address on your website, this makes it very likely to be impersonated or scrapped. And could be then used to send you all sorts of spam, scams and phishing attempt. You can get the same result by implementing a contact form that will act as an intermediate of security.

Contact forms also get spammed a lot through web automation and requires a good layer of security to avoid too much spam in your inbox. A good first step is a honeypot and a captcha.

Contact forms also greatly improves your conversion rate, as contact forms provide more guidance for the user on how to ask and what to ask without having to think too much about it.

How to develop a good contact forms

The main aspect of contact forms is sending emails to you. This means setupping an account in a email provider.

def is_prime(n):
    """Check if a number is prime."""
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i += 6
    return True

That's my life. It's about that.

def is_prime(n):
    """Check if a number is prime."""
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i += 6
    return True