
How to Set Up Contact Forms Using PHP and PHPMailer (SMTP Guide)
Learn how to configure contact forms with PHPMailer and Gmail SMTP. Step-by-step guide to set up secure email sending with App Password.
Introduction
Setting up a contact form on your website is essential for connecting with visitors. This guide will show you how to configure your contact form using PHPMailer and Gmail SMTP with a secure App Password.
What you'll learn:
- Understanding the PHP file structure
- Enabling 2-Step Verification on Google
- Generating a Gmail App Password
- Configuring submit.php file
- Testing your contact form
Let's get started! ๐
๐ฆ Step 1: Understand the PHP File Structure
Your template contains the following PHP files:
File Structure:
/assets/php/
โโโ submit.php โ Contact form configuration
โโโ newsletter.php โ Newsletter subscription configuration
โโโ smtp/
โ โโโ Exception.php โ PHPMailer exception handling
โ โโโ PHPMailer.php โ PHPMailer core library
โ โโโ SMTP.php โ SMTP protocol handling
๐ Step 2: Enable 2-Step Verification on Google
Before generating an App Password, you must enable 2-Step Verification.
Steps:
- Login to your Google Account
- Click on the Security tab (left sidebar)
- Under "Signing in to Google", click 2-Step Verification
- Click Get Started
- Enter your password to verify your account
- Enter your phone number for verification
- Choose how to receive the code (SMS or call)
- Enter the verification code you receive
- Click Turn On
๐ Note: 2-Step Verification is required to generate App Passwords. This keeps your account secure.
๐ Step 3: Generate Gmail App Password
Once 2-Step Verification is enabled, you can create an App Password.
Steps:
- Go back to Security tab
- Under "Signing in to Google", click App passwords
- Verify your account if prompted
- Under "Select app", choose Mail
- Under "Select device", choose Other (custom name)
- Enter a name (e.g., "Contact Form" or "My Portfolio")
- Click Generate
๐ Copy Your App Password:
The App Password is a 16-character code in the yellow bar. Copy it NOW and save it somewhere safe. You won't be able to see it again!
โ ๏ธ Important
Your regular Gmail password will NOT work with PHPMailer. You MUST use the 16-character App Password generated in this step.
โ๏ธ Step 4: Configure submit.php File
Open /assets/php/submit.php and update the following fields:
Update These Lines:
// 1. Your Gmail ID (SMTP server)
$mail->Username = "your_email@gmail.com";
// 2. Your App Password (16-character code)
$mail->Password = "your-app-password";
// 3. Where to receive form submissions
$mail->addAddress("your_email@gmail.com", "Your Name");
// 4. Sender's email and name
$mail->setFrom('your_email@gmail.com', 'Your Name');
Complete Example:
<?php
// SMTP configuration
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'john@gmail.com'; // โ Your Gmail
$mail->Password = 'abcd1234efgh5678'; // โ Your App Password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Recipient
$mail->addAddress('john@gmail.com', 'John'); // โ Where email goes
// Sender
$mail->setFrom('john@gmail.com', 'John Doe'); // โ Sender info
// Subject
$mail->Subject = '(Portfolio) New Contact Info';
?>
๐ง Step 5: Update Email Subject and Settings
Customize the Subject Line:
// Change this line
$mail->Subject = "(Lorenzo) New Contact Info";
// To your own
$mail->Subject = "(MySite) New Contact Form Message";
Customize Email Body (Optional):
// Find this section
$mail->Body = "Name: " . $_POST['name'] . "\r\n";
$mail->Body .= "Email: " . $_POST['email'] . "\r\n";
$mail->Body .= "Message: " . $_POST['message'] . "\r\n";
// You can add more fields or format it differently
$mail->Body = "๐ New Contact Form Submission\r\n";
$mail->Body .= "โโโโโโโโโโโโโโโโโโโโ\r\n";
$mail->Body .= "๐ค Name: " . $_POST['name'] . "\r\n";
$mail->Body .= "๐ง Email: " . $_POST['email'] . "\r\n";
$mail->Body .= "๐ Message: " . $_POST['message'] . "\r\n";
Add CC or BCC (Optional):
// CC - Copy to someone else
$mail->addCC('manager@example.com');
// BCC - Hidden copy
$mail->addBCC('backup@example.com');
๐ฐ Step 6: Configure newsletter.php
The newsletter subscription form works the same way. Open /assets/php/newsletter.php and apply the same settings:
// /assets/php/newsletter.php
// Same configuration as submit.php
$mail->Username = "your_email@gmail.com"; // Your Gmail
$mail->Password = "your-app-password"; // Your App Password
$mail->addAddress("your_email@gmail.com", "Your Name");
$mail->setFrom('your_email@gmail.com', 'Your Name');
// Different subject for newsletters
$mail->Subject = "(MySite) New Newsletter Signup";
๐งช Step 7: Test Your Contact Form
Testing Methods:
Option A: Test on Localhost (XAMPP)
- Start Apache in XAMPP
- Place your files in
htdocs/ - Visit
http://localhost/your-site/ - Fill out the contact form
- Submit and check your email
Option B: Test on Live Server
- Upload all files to your hosting
- Visit your domain
- Fill out the contact form
- Submit and check your email
Summary: Quick Setup Checklist
โ 2-Step Verification enabled on Google
โ App Password generated (16-character code)
โ Username updated in submit.php
โ App Password updated in submit.php
โ addAddress updated with your email
โ setFrom updated with your email
โ Subject line customized
โ newsletter.php updated the same way
โ Form tested and working
โ Frequently Asked Questions
Why can't I use my regular Gmail password?
Google requires an App Password for less secure apps like PHPMailer. This is a security measure to protect your account. Your regular password will NOT work.
Where do I find the App Password?
Go to Google Account โ Security โ App passwords. Generate a new one and save the 16-character code immediately.
What if I lose my App Password?
You cannot retrieve it. Go to Google App passwords and generate a new one. Just update it in your submit.php file.
Why is 2-Step Verification required?
Google requires 2-Step Verification before you can generate App Passwords. It adds an extra layer of security to your account.
How do I test the form locally?
Use XAMPP or MAMP. Start the server, place your files in htdocs, and visit http://localhost/your-site/. The form will work exactly like on a live server.
๐ค Share this post
Ready to Build Something Amazing?
Browse our collection of premium HTML and React templates
View All Templates โ