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>