If you’ve been working on a PHP project and need to test email functionality locally, you might wonder, How to send email from localhost in PHP using the PHPMailer library. PHPMailer is a popular, easy-to-use PHP library that simplifies sending emails. By configuring PHPMailer to work with your local host server, you can efficiently test email sending without relying on a live server. In this guide, we’ll walk you through the steps to set up and how to send email from localhost in PHP using PHPMailer, making it simple to troubleshoot and test your email scripts.
PHP’s built-in mail() function has some limitations, including poor handling of attachments, difficulty sending HTML emails, and susceptibility to spam filters. We can use the PHPMailer library instead, which provides a simple and reliable way to send emails in PHP.
Table of Contents
How to Install PHPMailer Library for sending email from localhost in PHP?
If you’re looking to send emails in PHP, the PHPMailer library is a powerful tool that simplifies the process. Below, we provide a step-by-step guide on how to install PHPMailer on your localhost server using Composer, a dependency manager for PHP. Follow these steps to get started:
Install Composer
Before installing PHPMailer, ensure Composer is installed on your system. Composer is essential for managing PHP dependencies, including PHPMailer. PHPMailer helps you to send mail from localhost in PHP.
- Download Composer: Visit the Composer download page to get the latest version.
- Install Composer: For a detailed guide on installing Composer on Windows, follow this GeeksforGeeks tutorial.
Verify Composer Installation
After installing Composer, open the Command Prompt to confirm it’s correctly set up:
- Open Command Prompt (CMD).
- Type
composer
and press Enter. - If Composer is installed correctly, you’ll see the Composer version and available commands. If not, recheck the installation steps.
Set Up Your Project Directory
Navigate to the directory where you want to create your PHP project:
- For XAMPP users:
cd C:\xampp\htdocs\php_project
- For WAMP users:
cd C:\wamp64\www\php_project
Replace php_project
with your desired project folder name.
Install PHPMailer Using Composer
Once you’re in the project directory, run the following command to install PHPMailer:
composer require phpmailer/phpmailer
This command will automatically download and install the PHPMailer library into your project.
How to Send Email From Localhost in PHP
You can send email from a local web server by using the code below.
Code For Send Email From Localhost in PHP:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
// Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$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 = 'bitlevelcode@gmail.com'; // Your Gmail address
$mail->Password = 'abcd efgh ijkl mnop'; // Your generated App Password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption
$mail->Port = 587; // TCP port to connect to
// Recipients
$mail->setFrom('bitlevelcode@gmail.com', 'bitlevelcode'); // Mention your Gemail address and the name
$mail->addAddress('you@gmail.com', 'user'); // Specify the recipient's email address where you want to send the email
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'Thank you for visiting bitlevelcode! We’re thrilled to have you explore our website and learn more about what we offer.
Our mission is Empowering You to Code with Excellence.';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
PHPIn the code above, I’ve used an app password to securely authenticate the email account, ensuring safe access without sharing your main password.
How Do I Generate an App Password for Secure Access to send emails from localhost in PHP?
- Log in to Your Email Account: Open your email account (e.g., Gmail).
- Enable Two-Step Verification:
- Go to your account’s security settings.
- Turn on Two-Step Verification (if not already enabled).
- Follow the instructions to complete the setup.
- Find the App Password Option:
- After enabling two-step verification, you’ll see an option for App Passwords.
- If you don’t see this option, directly visit Google App Passwords.
- Generate an App Password
- Choose the app (e.g., “Mail”) and device (e.g., “Other”).
- Click Generate to create a unique app password.
- Use the App Password:
- Copy the generated password.
- Replace your email account password in PHPMailer’s SMTP configuration with this app password.
Tip: Keep this password secure. If compromised, revoke it immediately from your account settings.
OUTPUT:
Related Posts:
>>How to Create a WordPress Theme: 2 Easy Steps
>>How to Use DropzoneJS in PHP? – Drag & Drop File
>>How To Resize an Image in PHP?
Conclusion: How to Send Email from Localhost in PHP
Sending emails from localhost in PHP using the PHPMailer library is a straightforward and secure way to test email functionality during development. By following the steps outlined above—installing PHPMailer, configuring SMTP, and generating an app password—you can ensure seamless email delivery from your local environment.
Now that you have a working setup, you’re ready to integrate email features into your applications confidently.
Happy Coding! 🚀