How to create a contact form

Here we will cover two scenarios. The first one is the easier one, which is through WordPress. When it comes to WordPress, there is a plugin for that, and we will not go into much detail. The plugin is Contact Form 7. After installation and setup, you need to set up an SMTP connection in your WordPress so that your emails arrive without any issues. You can do this using our other article SMTP and WordPress.

The second part, which is “harder,” is for HTML websites or those that have a manually created contact page and do not use any CMS.

First of all, you need to download PHPMailer by clicking on “Clone or download” and then on “Download ZIP”.

As shown in the image below:

phpmailer How to create a contact form

After that, you need to upload the zip file to your hosting account.

cPanel > File Manager > open the public_html folder and inside it, click on the “upload” option.

When you have finished uploading, right-click on the zip file and extract, as shown in the image:

phpmailer2 How to create a contact form

Now that we have unpacked it, we need to create our page.
Our example has the following fields:

<form action="contactform.php" method="post">
    <input type="text" name="firstname" placeholder="Your name.."><br>
    <input type="text" name="lastname" placeholder="Your last name.."><br>
    <input type="text" name="email"><br>
    <textarea name="text" placeholder="Write something.." style="height:200px"></textarea><br>
    <input type="submit" value="Send"><br>

Your contact form certainly looks different and has different names for input elements and more or fewer fields, so do not copy/paste literally but make small adjustments related to your contact form.

The key thing here is the option in <form> and that is “action=”contactform.php”” which means that when you click on “Send,” the content of the fields is sent to that PHP page, in our case contactform.php (it can be the same page or an external one).

Now let’s set up SMTP and PHPmailer for the data we sent using the HTML contact form.

PHPMailer looks like this:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                 
    $mail->Host       = 'mail.yourdomain.tld';                     //you need to set your domain with "mail." in front of it
    $mail->SMTPAuth   = true;                                  
    $mail->Username   = '[email protected]';                     //SMTP username 
    $mail->Password   = 'password-of-email-above';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;           
    $mail->Port       = 587;                            

    $mail->setFrom($_POST['email'], $_POST['firstname']." ".$_POST['lastname']);
    $mail->addReplyTo($_POST['email'], $_POST['firstname']." ".$_POST['lastname']);

    $mail->addAddress('[email protected]');               //Name is optional

    $mail->Subject = 'Contact from website';
    $mail->Body    = $_POST['text'];
    $mail->AltBody = $_POST['text'];

    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

After this, all that’s left is to change the text “Message sent!” and the error to what you want to write, and that’s it.

A small note, if after setting up the form, you receive a larger number of spam emails to your address, you need to set up

Scroll to Top