You build a PHP contact form, hit submit, and only one person gets the email. But in real projects, you usually want two emails: one to the visitor, one to yourself or your support inbox.
In this guide, we’ll walk through a small PHP contact form using PHPMailer that sends two emails from one form submission—with simple setup, clear structure, and predictable behavior.
You’ll see how to wire everything together so it’s easy to deploy on your usual web hosting or dedicated server later.
The goal is simple:
User fills in a “Contact us” form.
When they click “Send”, the script:
Sends a confirmation email to the user.
Sends a notification email to you (or your support address) with all their details.
Nothing fancy, just a clean piece of PHP that works every time.
First, in your project folder, install PHPMailer via Composer:
bash
composer require phpmailer/phpmailer
This gives you a solid, modern way to send emails in PHP without fighting with mail() and weird server settings.
Your folder might look like this:
contact.html – the form
contact.css – the styling
process.php – where we send the emails
vendor/ – created by Composer (PHPMailer lives here)
Create contact.html:
html
PHP Contact Form
Full Name
<div class="form-item">
<label for="email">Email</label>
<input type="text" name="email" id="email" />
</div>
<div class="form-item">
<label for="message">Message</label>
<textarea name="message" id="message" rows="5"></textarea>
</div>
<button class="btn-submit" name="submit">Send Message</button>
</div>
</form>
Then add a very simple contact.css so the form doesn’t look like 1999:
css
body {
font-family: Verdana, sans-serif;
}
.form-container {
width: 600px;
padding: 30px;
}
.form-item:not(:first-child) {
margin-top: 20px;
}
.form-item label {
display: inline-block;
font-weight: bold;
margin-bottom: 10px;
}
.form-item input[type="text"],
.form-item textarea {
display: block;
width: 100%;
padding: 0.375rem 0.75rem;
font-size: 1rem;
line-height: 1.5;
color: #495057;
background-color: #fff;
border: 1px solid #ced4da;
border-radius: 0.25rem;
}
.form-item textarea {
resize: none;
}
.btn-submit {
display: inline-block;
font-weight: 400;
text-align: center;
border: 1px solid transparent;
padding: 0.375rem 0.75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: 0.25rem;
background-color: #f23a2e;
color: #fff;
margin-top: 15px;
}
At this point, you can open contact.html in a browser and see a basic, clean contact form.
Now the fun part: process.php.
Create process.php in the same folder:
php
<?php
require_once DIR . '/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Grab the form values
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$message = $_POST['message'] ?? '';
// In real life, validate properly:
// - check empty fields
// - validate email
// - maybe sanitize message
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die('Invalid email address.');
}
// Common SMTP settings
$smtpHost = 'smtp.yourhost.com';
$smtpUser = 'you@example.com';
$smtpPass = 'your_smtp_or_app_password';
$smtpPort = 587; // or 465 for SSL
// 1) Email to the person who filled the form
$mailToUser = new PHPMailer(true);
try {
$mailToUser->isSMTP();
$mailToUser->Host = $smtpHost;
$mailToUser->SMTPAuth = true;
$mailToUser->Username = $smtpUser;
$mailToUser->Password = $smtpPass;
$mailToUser->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // or PHPMailer::ENCRYPTION_SMTPS
$mailToUser->Port = $smtpPort;
// From and To
$mailToUser->setFrom($smtpUser, 'Your Website');
$mailToUser->addAddress($email, $name);
// Content
$mailToUser->Subject = "Hi $name, we received your message";
$mailToUser->Body = "Hi $name,\n\nThank you for contacting us. We received your message and will get back to you shortly.\n\nBest regards,\nYour Website";
$mailToUser->send();
} catch (Exception $e) {
// In real projects, log the error instead of echoing it
error_log('Mailer Error (user): ' . $mailToUser->ErrorInfo);
}
// 2) Email to you (or your support inbox)
$mailToOwner = new PHPMailer(true);
try {
$mailToOwner->isSMTP();
$mailToOwner->Host = $smtpHost;
$mailToOwner->SMTPAuth = true;
$mailToOwner->Username = $smtpUser;
$mailToOwner->Password = $smtpPass;
$mailToOwner->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mailToOwner->Port = $smtpPort;
$mailToOwner->setFrom($smtpUser, 'Contact Form');
$mailToOwner->addAddress('support@example.com', 'Support Team');
$mailToOwner->Subject = "New message from $name";
$mailToOwner->Body = "You received a new contact form message:\n\n"
. "Name: $name\n"
. "Email: $email\n"
. "Message:\n$message\n";
$mailToOwner->send();
} catch (Exception $e) {
error_log('Mailer Error (owner): ' . $mailToOwner->ErrorInfo);
}
// Simple response
echo 'Thanks for your message!';
}
That’s it: submit the form once, and two emails go out.
If you want to keep your email password safe, move smtpUser and smtpPass into environment variables or a config file instead of hard-coding them. But for a quick demo, the structure above shows the main idea clearly.
Let’s break the flow down in plain language:
We check the request method
If it’s POST, we assume the form was submitted and start working with the data.
We read the form fields
$name, $email, $message come from $_POST. In a real PHP web app, you’d validate and sanitize these more seriously.
We configure PHPMailer once per email
Each email gets its own PHPMailer instance:
First one goes to the user.
Second one goes to you (or your team).
We send a friendly confirmation to the user
This is just a simple “We got your message” email so they know the form worked.
We send the full details to your own inbox
This one contains name, email, and message, so you can reply from your normal mailbox.
We log errors instead of crashing the page
In real web development, you don’t want to show SMTP errors to users. Logging them keeps your app neat and safe.
All this PHP and PHPMailer code is nice, but where it runs matters a lot.
If your hosting is slow or unstable, emails might be delayed or blocked.
If the server’s IP has a bad reputation, your messages can land in spam.
If your PHP environment is badly configured, everything becomes “why is this not sending?” debugging.
That’s where solid infrastructure helps. If you eventually move from a small shared hosting plan to a dedicated environment, you get more control over mail ports, IP reputation, and overall performance. For that kind of step up, 👉 GTHost dedicated servers are a practical option when you want fast, reliable PHP hosting that keeps your contact-form emails flowing.
Run your PHP app on a clean, stable server, and you spend less time fighting random email issues and more time shipping features.
Yes, easily.
For example, instead of creating another PHPMailer instance, you can send one notification email to multiple internal addresses:
php
$mailToOwner->addAddress('support@example.com', 'Support Team');
$mailToOwner->addAddress('sales@example.com', 'Sales Team');
$mailToOwner->addCC('manager@example.com', 'Manager');
Or you can keep using separate instances if the messages are very different for each person. Both approaches are fine; it depends on how customized you want each email to be.
Sending one PHP contact form submission to two different people is not complicated: a simple HTML form, a process.php script, and PHPMailer are enough to build something stable and clear. You collect the data once, then send one email to the user and another to yourself, all in one smooth flow.
When you’re ready to put this into production, the hosting environment starts to matter a lot, and that’s why GTHost is suitable for PHP contact forms and similar email-heavy web development scenarios—👉 GTHost gives you fast, dedicated servers with stable networking, which makes it much easier to keep your PHPMailer-based contact forms delivering reliably.