How To Send Emails From Localhost In PHP

Tutorial for Send Emails From Localhost In PHP

Sending emails from a localhost environment using PHP can be challenging due to server configurations. However, using some libraries makes this task considerably easier. In this step-by-step tutorial you will learn how to send emails from localhost in PHP with the help of the PHPMailer library.

What is PHPMailer

PHPMailer is a popular open-source PHP library used for send emails from websites or web applications. It helps in sending emails with attachments, HTML formatting, and secure delivery methods like SMTP. It’s popular because it simplifies email sending tasks and offers various features for customization and reliability.

Steps to Send Emails From Localhost Using PHPMailer

Step 1: Setting Up a Project Folder

Open your xampp htdocs folder or localhost www directory and create a new folder called php-email-project this is the application or project folder.

mkdir php-email-project
cd php-email-project

Step 2: Installing PHPMailer Library

In the php-email-project folder install the PHPMailer library via composer:

composer require phpmailer/phpmailer

Step 3: Generate Gmail App Password

In this example uses Gmail SMTP, if you’re using it too, you’ll need to generate an app password.

To Generate a google app password, open the following link (make sure you are logged in and two-step verification is enabled), and generate an app password.

https://myaccount.google.com/apppasswords

1. Enter the App Name and click on the “Create” button:

Enter the App Name and click Create to generate google app password

2. Now you will see your app password copy and save it. My Generated App Password is – “tnxeizcecpwljdnt” Note: there is no space.

Copy the google Generated App Password and save it

Step 4: PHP Script For Sending Email From Localhost

Create a new PHP file called send_email.php in your project directory. This script will use PHPMailer to send an email. In the following code, change the details according to yours:

<?php
require __DIR__ . "/vendor/autoload.php";

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Change the following details according to yours
$gmail = "[email protected]"; // Your Gmail address
$app_password = "app_password"; // Your Gmail app password

$sender_name = "Sender Name"; // Your name or organization name
$receiver_email = "[email protected]"; // Email address of the recipient
$mail_subject = "Email Subject"; // Subject of the email
$mail_body = "Email Body"; // Body content of the email

try {
    // Create an instance; passing `true` enables exceptions
    $mail = new PHPMailer(true);

    $mail->isSMTP();  // Send using SMTP

    // Enable SMTP debugging
    // SMTP::DEBUG_OFF = off (for production use)
    // SMTP::DEBUG_CLIENT = client messages
    // SMTP::DEBUG_SERVER = client and server messages
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;

    $mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through

    // TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
    $mail->Port = 587;

    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->SMTPAuth = true; // Whether to use SMTP authentication

    $mail->Username   = $gmail; // SMTP username
    $mail->Password   = $app_password; // SMTP password

    /** Set who the message is to be sent from.
     *  For Gmail, this generally needs to be the same as the user you logged in as. */
    $mail->setFrom($gmail, $sender_name);

    // Set recipient email address
    $mail->addAddress($receiver_email);

    $mail->isHTML(true);
    $mail->Subject = $mail_subject; // Subject of the email
    $mail->Body = $mail_body; // Body content of the email

    // For attachments
    //$mail->addAttachment('/path/of/the/file.ext');  // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // You can specify the file name

    $mail->send();
    echo "Email has been sent successfully.";
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

This script uses PHPMailer to send an email via Gmail SMTP. Make sure to replace the placeholders with your actual email and password.

Step 5: Send the Email From Localhost

Run the PHP script using the following command or you can run the script via browser:

php send_email.php

OR

http://localhost/php-email-project/send_email.php

This should output either “Email has been sent successfully.” or an error message.

Step 6: Handling Errors

If you encounter errors, review the SMTP settings and ensure they match your email provider’s requirements. Check for typos, and make sure your email provider allows SMTP access from your localhost.


Thank You 🙏. If you have any question leave in the comment box 💬.

Leave a Reply

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

We use cookies to ensure that we give you the best experience on our website. Privacy Policy