I’ll show you how to make this Bootstrap form send emails using SMTP.
We’ll do this in PHP with PHPMailer, because:
-
It’s reliable
-
Supports SMTP authentication
-
Works with Gmail, Outlook, and custom mail servers
Install PHPMailer
You can either:
-
Via Composer (recommended)
- composer require phpmailer/phpmailer
-
Or download from: PHPMailer GitHub and include it manually.
HTML Form
<form action="sendmail.php" method="post" autocomplete="off"> <div class="mb-3"> <label for="name" class="form-label">Full Name</label> <input type="text" name="name" class="form-control" id="name" required autocomplete="off"> </div> <div class="mb-3"> <label for="email" class="form-label">Email address</label> <input type="email" name="email" class="form-control" id="email" required autocomplete="off"> </div> <div class="mb-3"> <label for="message" class="form-label">Message</label> <textarea name="message" class="form-control" id="message" rows="3" required></textarea> </div> <button type="submit" class="btn btn-primary">Send Email</button> </form>
PHP: sendmail.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; // If using Composer
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
$mail = new PHPMailer(true);
try {
// SMTP Configuration
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Change to your SMTP server
$mail->SMTPAuth = true;
$mail->Username = 'your-email@gmail.com'; // Your email
$mail->Password = 'your-app-password'; // App password, not normal password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// From & To
$mail->setFrom('your-email@gmail.com', 'Website Contact');
$mail->addAddress('your-email@gmail.com', 'Your Name'); // Where to send
// Email Content
$mail->isHTML(true);
$mail->Subject = 'New Contact Form Submission';
$mail->Body = "
<h3>New Message from Website</h3>
<p><strong>Name:</strong> {$name}</p>
<p><strong>Email:</strong> {$email}</p>
<p><strong>Message:</strong><br>{$message}</p>
";
// Send Email
$mail->send();
echo "Message sent successfully!";
} catch (Exception $e) {
echo "Message could not be sent. Error: {$mail->ErrorInfo}";
}
}
Notes for Gmail SMTP
-
You can’t use your normal Gmail password — you need an App Password.
-
Steps:
-
Enable 2-Step Verification in Google Account.
-
Go to Google App Passwords.(https://myaccount.google.com/apppasswords)
-
Generate one for “Mail” and copy it.
-
Use it in
$mail->Password.
-
