Simple HTML Contact Form Using PHPMailer

This is a very basic example of an HTML contact form using PHPMailer to send an email with data registered in the form.

You can freely edit it as needed to fully accomplish your requirements.

In this example, PHPMailer sending parameters are set in order to user StarterMail service: if you want touse different SMTP services, you can edit it accordingly).

 

1 -install PHPMailer ( https://github.com/PHPMailer/PHPMailer )

 

2 - Create the following testform.php file (customizing Username, Password, setFrom and header location as needed):

(note: it may be necessary to edit the "require" statements, accordingly to the path where you installed PHPMailer) 

 

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

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

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = '156.unihost.it';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = 'MyStrongPassword!';                           // SMTP password
    $mail->SMTPSecure = 'STARTTLS';                           
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]');
    $mail->addAddress($_POST['mail']);     // Add a recipient



    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = $_POST['subject'];
    $mail->Body    = $_POST['text'];

    $mail->send();
    header('Location: http://www.example.net/contact.php');
    exit();
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

 

3 - Create the following testform.html file:

(note: in this file we use few Bootstrap CSS classes: if you don't use Bootstrap you can ignore them, if you use Bootstrap you can complete <head></head> section accordingly). 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

</head>
<body>
<!-- Contact Form -->
<div class="row">
    <div class="col-md-8 col-md-offset-2">

        <form action="testform.php" method="POST">
            <div class="form-group">
                <input type="text" name="mail" class="form-control" placeholder="Email Address">
            </div>
            <div class="form-group">
                <input type="text" name="subject" class="form-control" placeholder="Subject">
            </div>
            <div class="form-group">
                <textarea class="form-control" name="text" rows="3" placeholder="Your Message"></textarea>
                <button class="btn btn-default" type="submit">Send Message</button>
            </div>
        </form>
    </div>
</div>
<!-- End Contact Form -->
</body>
</html>

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

What do I need to do to put Flash on my website?

Flash is a client side feature - it runs on the user's computer, not on your web host. As long as...

What are private nameservers and how do I set them up?

Private nameservers are where your domain points to our nameservers so that your customers do not...

Static IP vs. Dynamic IP Address

A static IP address is one that remains fixed and never changes. The PC always sees the same...

Protecting Yourself Against Viruses & Data Loss

It is vital to ensure your computer is protected from viruses and trojans. As well as...

How to Upgrade perl?

Run the following command. cd /usr/local/src ; wget...