How to Send Email from Localhost in PHP Using PHPMailer

send email from localhost in php

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.

The PHP mail() function comes with certain limitations, such as inadequate support for attachments, challenges in sending HTML emails, and a higher likelihood of being flagged as spam. To overcome these issues, the PHPMailer library offers a more reliable and user-friendly solution for sending emails in PHP.

How to Install PHPMailer Library for sending email from localhost in PHP?

To send emails in PHP, the PHPMailer library is an excellent choice for simplifying the process. Here’s a detailed guide on how to set up PHPMailer on your localhost server using Composer, PHP’s dependency manager. Follow these instructions to get started.

Install Composer

Before setting up PHPMailer, make sure Composer is installed on your system. Composer is an essential tool for handling PHP dependencies, such as PHPMailer, which allows you to send emails from a localhost server in PHP.

  1. Download Composer: Visit the Composer download page to get the latest version.
  2. Install Composer: For a detailed guide on installing Composer on Windows, follow this GeeksforGeeks tutorial.

Verify Composer Installation

Once Composer is installed, open the Command Prompt to verify that it has been set up correctly.

  1. Open Command Prompt (CMD).
  2. Type composer and press Enter.
  3. 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

Go 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

Navigate to your project directory and execute the command below to install PHPMailer:

composer require phpmailer/phpmailer

Running this command will download and install the PHPMailer library directly into your project.

How to Send Email From Localhost in PHP

The code below demonstrates how to send an email from a local web server.

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}";
}
PHP

In the code, I have used an app password the 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?

  1. Log in to Your Email Account: Open your email account (e.g., Gmail).
  2. Enable Two-Step Verification:
    • Go to your account’s security settings.
    • Turn on Two-Step Verification (if not already enabled).
  3. 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.
  4. Generate an App Password
    • Choose the app (e.g., “Mail”) and device (e.g., “Other”).
    • Click Generate to create a unique app password.
  5. Use the App Password:
    • Copy the generated password.
    • Replace your email account password in PHPMailer’s SMTP configuration with this app password.

OUTPUT:

send email from localhost in php

>>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



Using the PHPMailer library to send emails from localhost in PHP is a simple and reliable method for testing email functionality during development. By setting up PHPMailer, configuring SMTP, and creating an app password, you can efficiently send emails from your local server with ease.

With your setup complete, you can now start adding email functionality to your applications.

Happy Coding! 🚀

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.