In this guide, you’ll learn how to send emails from your localhost environment using the PHP Mailer library and Gmail’s SMTP server. PHPMailer simplifies sending emails in PHP, while Gmail's SMTP ensures reliable delivery
Chapter 1: Setting Up the Environment
1.1 Install WAMP Server
● WAMP stands for Windows, Apache, MySQL, and PHP. It provides a local development environment for testing your applications.
● Steps to Install WAMP:
a. Download WAMP from the official website.
b. Install the software by following the on screen instructions.
c. Once installed, start WAMP and ensure the server is running by checking the WAMP icon in the system tray (it should be green).
1.2 Download and Install PHPMailer
● PHPMailer is a popular library for sending emails through PHP.
● Using Composer:
● Run the following command in your project
directory to install PHPMailer:
Manual Installation:
Download the PHPMailer files from the GitHubrepository, and place the extracted files in your project directory (e.g.,C:\wamp64\www\yourproject\PHPMailer)
Chapter 2: Configuring Gmail SMTP
Before you can send emails using Gmail's SMTP server, some configuration is needed in your Google account:
2.1 Enable Less Secure Apps (If Needed)
● Go to your Google Account settings.
● Scroll down and Turn on "Less secure app access"
(only if you're not using 2-factor authentication).
2.2 Generate an App Password (if using 2FA)
● If 2FA (Two-Factor Authentication) is enabled, go to Google Account Security.
● Under "Signing in to Google," select App passwords and generate a 16-character app password.
Step 1: Manage Your Google Account
1. Open Gmail and sign in to your account.
2. In the top-right corner, click on your profile picture.
3. From the drop-down menu, select "Manage your Google Account.
Step 2: Go to Security Settings
1. On the left sidebar, click on "Security."
2. Scroll down to the "Signing in to Google" section.
Step 3: Enable 2-Step Verification
1. Click on "2-Step Verification" and then click "Get Started."
2. You will be prompted to verify your account by entering your password.
3. Follow the on-screen instructions to add your phone number and receive a verification code via SMS or call.
4. Enter the verification code, and then click "Turn On" to activate 2-Step Verification.
Once 2-Step Verification is enabled, you’re ready to generate an app password if necessary, which you’ll use instead of your main Google password when configuring PHPMailer. Continue to the next section to generate your App Password
Chapter 3: Setting Up PHPMailer
Now that Gmail is configured, you can set up your PHP script to send emails.
3.1 Basic PHPMailer Script Create a mail.php file in your project directory, and use the following code to configure PHPMailer:
<?php
// Include PHPMailer classes
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
if (isset($_POST["submit"])) {
// Create a new PHPMailer instance
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send
through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'makeanouar@gmail.com'; // Your Gmail address
(SMTP account)
$mail->Password = 'rxarhqslsdadkkpg'; // Your Gmail App Password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable SSL
encryption
$mail->Port = 465; // TCP port for SSL
// Collect form data
$userEmail = $_POST['emailAddress']; // Sender's email from the form
$userSubject = $_POST['objet']; // Subject from the form
$userMessage = $_POST['message']; // Message from the form
// Set recipient (YOU, the site owner)
$mail->addAddress('makeanouar@gmail.com'); // YOUR email to receive
the form data
// Set the sender's email address
$mail->setFrom($userEmail, 'Website Contact'); // The email entered in the
form will be the sender
$mail->addReplyTo($userEmail); // You can reply to the sender's email
// Email content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'New contact form: ' . $userSubject; // Set the email
subject
$mail->Body = "
<strong>From:</strong> $userEmail<br>
<strong>Subject:</strong> $userSubject<br>
<strong>Message:</strong><br>" . nl2br($userMessage); // Format message
with new lines
// Send email
$mail->send();
// Success message (alert and redirect)
echo "
<script>
alert('*** THANK YOU ***\\n \\nWe have received your application and will
contact you shortly!');
document.location.href = 'index.php';
</script>
";
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>
Chapter 4: Creating the Contact Form
Now let’s connect this script to a form. 4.1 Contact Form (HTML)
● objet: Name for the subject field.
● emailAddress: Name for the email field.
● message: Name for the message field.
These name attributes must match when retrieving form data in the PHP script, ensuring proper handling when the form is submitted.
Mail.php
Your Gmail Address:
● Replace'your-email@gmail.com' with your actual Gmail account.
● Your Gmail App Password:
● Replace 'your-app-password' with the app password generated after enabling 2-Step Verification, or your regular Gmail password if 2
Step Verification is not enabled.
● Recipient Address:
● Replace 'recipient@example.com' with the email address where you want to receive the form submissions.
Make sure the password is without any spaces
Everything is good, and now let's try to send an email through the contact form.
WE HOPE THIS HAS PROVIDED YOU WITH CLEAR AND HELPFUL INSTRUCTIONS TO IMPLEMENT THIS FUNCTIONALITY IN YOUR PROJECTS. YOUR SUPPORT IS GREATLY APPRECIATED, AND WE LOOK FORWARD TO BRINGING YOU MORE VALUABLE CONTENT IN THE FUTURE. IF YOU HAVE ANY QUESTIONS OR FEEDBACK, FEEL FREE TO REACH OUT. THANK YOU ONCE AGAIN FOR YOUR SUPPORT!
