How to create a contact form

Here we will do two scenarios. The first is the easier one, and that is through WordPress.
When it comes to wordpress, there is a plugin for that and we won't go into that too much. It's a plugin Contact Form 7.   After installation and setup, you need to create an SMTP connection in your wordpress so that your emails arrive without problems. And you can do that with our second article SMTP and WordPress.

The other part that is "harder" is for HTML sites or those that have a hand-made 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 in the picture below

phpmailer Kako da kreirate kontakt formu

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

cPanel > File Manager > open public_html folder and inside it "upload" option.

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

phpmailer2 Kako da kreirate kontakt formu

Now that we've unpacked that, we need to do our part.
Our example has the following fields:

<form action="/en/kontaktforma.php/" method="post" data-trp-original-action="kontaktforma.php">
    Name:<br>
    <input type="text" id="fname" name="firstname" placeholder="Your name.."><br>
    Last name:<br>
    <input type="text" id="lname" name="lastname" placeholder="Your last name.."><br>
    Email:<br>
    <input type="text" id="email" name="email"><br>
    Text:<br>
    <textarea id="tekst" name="tekst" placeholder="Write something.." style="height:200px"></textarea><br>
    <input type="submit" value="Send"><br>
  <input type="hidden" name="trp-form-language" value="en"/></form>

Your contact form certainly looks different and has different names of input elements and more or less fields, so don't literally copy/paste but mini-settings that are related to your contact form.

The key thing here is the u option

and that is "action="kontaktforma.php" and that means that when "Send" is clicked, the content of the field is sent to that php page in our case kontaktforma.php (it can be the same page or it can be external).

Now it's time to set up SMTP and PHPmailer for the data we sent using the HTML contact form.

PHPMailer looks like this:

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output $mail-&gt;isSMTP(); //Send using SMTP $mail-&gt;Host = &#039;smtp.example.com&#039;; //Set the SMTP server to send through $mail-&gt;SMTPAuth = true; //Enable SMTP authentication $mail-&gt;Username = &#039;user@example.com&#039;; //SMTP username $mail-&gt;Password = &#039;secret&#039;; //SMTP password $mail-&gt;SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption $mail-&gt;Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` //Recipients $mail-&gt;setFrom(&#039;from@example.com&#039;, &#039;Mailer&#039;); $mail-&gt;addAddress(&#039;joe@example.net&#039;, &#039;Joe User&#039;); //Add a recipient $mail-&gt;addAddress(&#039;ellen@example.com&#039;); //Name is optional $mail-&gt;addReplyTo(&#039;info@example.com&#039;, &#039;Information&#039;); $mail-&gt;addCC(&#039;cc@example.com&#039;); $mail-&gt;addBCC(&#039;bcc@example.com&#039;); //Attachments $mail-&gt;addAttachment(&#039;/var/tmp/file.tar.gz&#039;); //Add attachments $mail-&gt;addAttachment(&#039;/tmp/image.jpg&#039;, &#039;new.jpg&#039;); //Optional name //Content $mail-&gt;isHTML(true); //Set email format to HTML $mail-&gt;Subject = &#039;Here is the subject&#039;; $mail-&gt;Body = &#039;This is the HTML message body <b>in bold!</b>'&#039;; $mail-&gt;AltBody = &#039;This is the body in plain text for non-HTML mail clients&#039;; $mail-&gt;send(); echo &#039;Message has been sent&#039;; } catch (Exception $e) { echo &quot;Message could not be sent. Mailer Error: {$mail-&gt;ErrorInfo}&quot;; }

Now a little clarification about this code:
1* – $_POST['email'] is the email address your visitor entered from the contact form this ['email'] is actually depending on your contact form how you named that input entry.
2* - In this line, when you click replay in your email client, the person who receives the reply is always the same as the one who sent it, as in our example.
3* - In this line, enter your email address, that is, who receives the email from the contact form
4* – The title here can also be $_POST[title'], i.e. HTML depending on your contact form. We didn't have a title field in our example.
5* – In this line, the text without html is just plain text.
6* - Here it is also possible to insert HTML into the body of the email itself because it is marked immediately in the next line that the email is HTML (what I mean by this wordpress article can graphically show you HERE)

After this, all that's left is to edit the text "Message sent!" and error in your what you want to write and that's all.

Below you can see how we put both the HTML form and the PHP in one file called kontaktforma.php

SMTPDebug = false; //Enable verbose debug output $mail->isSMTP(); //Send using SMTP $mail->Host = 'mail.domen.tld'; //Set the SMTP server to send through $mail->SMTPAuth = true; //Enable SMTP authentication $mail->Username = 'username@domen.tld'; //SMTP username $mail->Password = 'password from email address above'; //SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption $mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` //Recipients $mail->setFrom($_POST['email'], $_POST['firstname']." ".$_POST['lastname']); $mail->addAddress('username@domen.tld'); //enter the email address to which messages from the contact form will arrive $mail->addReplyTo($_POST['email'], $_POST['firstname']." ".$_POST['lastname']); //Content $mail->isHTML(true); //Set email format to HTML $mail->Subject = "Contact from site"; //if you have a title field then enter here $mail->Body = $_POST['text']; $mail->AltBody = $_POST['text']; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }

A small note, if after setting up the form you receive a large number of spam emails to your address, you need to set up i https://www.google.com/recaptcha/intro/v3.html

Scroll to Top