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.

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.

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.

  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

After installing Composer, open the Command Prompt to confirm it’s correctly set up:

  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

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

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

  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

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

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.