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 = 'info@example.net';                 // SMTP username
    $mail->Password = 'MyStrongPassword!';                           // SMTP password
    $mail->SMTPSecure = 'STARTTLS';                           
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('info@example.net');
    $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>

 

Found this article interesting?
Subscribe to DomainRegister´s newsletter!

You can unsubscribe at any time by simply clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp s privacy practices here.

  • PHPMailer, HTML
  • 8 Users Found This Useful
Was this answer helpful?

Related Articles

 How To Insert a Carriage Return in a PHP String

If you need to insert a Carriage Return in a string in PHP: Carriage return is: "\r"  But...

 How to Create a phpinfo page

The phpinfo() function outputs a huge amount of information about the system you're using, such...

 How To Make a Redirect in PHP

If you want that a page redirects automatically the user to a certain page or site (let's say,...

 How To Configure Easy WP SMTP Plugin To Use Gmail SMTP Service

Easy WP SMTP is a popular and free plugin for WordPress to send email(s) by an authenticated SMTP...

 How to Backup Your MySQL Database Using PHP

This simple PHP script will provide a full dump of your MySQL database.You need to provide...