How to Send Email from localhost in PHP using SMTP

To send an email using SMTP in PHP, you can use the PHPMailer library, which gives a helpful and dependable method for sending messages through SMTP servers. Here is a step-by-step guide on the most proficient method to make it happen:

Send Email using SMTP

The PHP email feature allows you to create custom email forms and send simple text messages to multiple recipients. Here is a blog to show you how to send mail encrypted using PHP Mailer. If you create your website in PHP programming language, you can send emails from your web server.

Installing PHPMailer Library

You can install PHPMailer using Composer or by manually downloading the library from For this guide, we will assume you have installed it using Composer.

If you have not already installed Composer, you can download it from. and follow the installation instructions.

Then, create a new directory for your PHP project and run the following command inside that directory to install PHPMailer:

composer require phpmailer/phpmailer

See also:

  1. How to Send Bulk Emails in PHP using PHPMailer with Ajax jQuery
  2. Send Multiple Emails at once in Gmail using PHP jQuery AJAX
  3. How to Send Email from localhost in PHP using SMTP
  4. Registration & Login with Email OTP verification using Jquery AJAX with PHP Mysql
  5. Contact form send attachment using jquery ajax in PHP

Settings must be enabled to send Email

  • Before you can send email using Gmail SMTP server, you need to set some security settings and permission levels in your Google account security settings
  • Make sure that 2-Step-Verification is enabled
  • If 2-step-verification is enabled, then you will have to create app password for your application or device.
  • For security measures, Google may require you to complete this additional step while signing-in. Click here to allow access to your Google account using the new device/app.

Creating HTML Contact Form

Here is a simple HTML contact form that you can use as a starting point. we are using in bootstrap 5 CDN link in this html page.


<!DOCTYPE html>
<html lang="en">
  <title>Send Email using PHP Mailer in PHP</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="" rel="stylesheet">
  <script src=""></script>

<div class="container-fluid p-5 bg-success text-white text-center">
  <h1>Send Email using PHP Mailer in PHP </h1>
  <p>Send Email using php smtp gmail localhost phpmyadmin</p> 
<div class="container mt-3">
  <div class="row">
    <div class="col-sm-4 col-md-4 offset-4">
      <form action="send.php" class="was-validated" method="POST">
        <div class="mb-3 mt-3">
          <label for="name" class="form-label">Name:</label>
          <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name" required>
          <div class="valid-feedback">Valid Name</div>
          <div class="invalid-feedback">Please fill out this field.</div>
        <div class="mb-3">
          <label for="email" class="form-label">Email:</label>
          <input type="email" class="form-control" id="email" placeholder="Enter Email" name="email" required>
          <div class="valid-feedback">Valid Email</div>
          <div class="invalid-feedback">Please fill out this field.</div>
        <div class="mb-3">
          <label for="message" class="form-label">Message:</label>
          <textarea type="text" class="form-control" placeholder="Message" name="message" rows="4"></textarea>
        <button type="submit" name="submit" name="submit" class="btn btn-primary">Send</button>

In this html form page, we have created a simple contact form with three fields: name, email, and message. The form is action attribute is set to “send.php”. This means that when the user submits the form, the form data is send into the send.php file. File for further processing (e.g. sending an email with the sent data).

Send email using php mailer in php

Creating Sending Email SMTP

Create a new PHP file send.php and include the PHPMailer library at the top of the file


//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

if (isset($_POST['submit'])) {

    // Received Form data from html page
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    try {

        //Create an instance; passing `true` enables exceptions
        $mail = new PHPMailer(true);
        //Server settings
        $mail->SMTPDebug = SMTP::DEBUG_OFF;                         //Enable verbose debug output
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host       = '';                       //Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
        $mail->Username   = '';                  //SMTP username
        $mail->Password   = 'your passkeys code';                   //SMTP password 
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable implicit TLS encryption
        $mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_SMTPS`
        $mail->CharSet    = 'UTF-8';

        $mail->setFrom('', 'Mailer');
        $mail->addAddress($email, 'webs Codex');    //Add a recipient

        $mail->SMTPOptions =
        ['ssl' =>[
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => false

        $mail->isHTML(true);                                   //Set email format to HTML
        $mail->Subject = 'How to Send Email using Gmail SMTP | 100% Working on Localhost and Server';
        $mail->Body    = '<h4>Hello, '. ucwords($name).'</h4><p>'.$message.'</p>';
        if ($mail->send()) {
           echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";

In the script above, you need to replace the placeholders with the current SMTP server information, SMTP username, password, and the sender’s and recipient’s email addresses. In addition, you can customize the subject, content and other parameters of the email to suit your needs.

Save the PHP file and run it on your web server. The script attempts to send the email using the provided SMTP settings. If successful, you will see the message “Message has been sent”.” otherwise, an error message is displayed indicating the reason for the failure.

Be sure to test it in a development or test environment before using it in a production environment.


We hope that the steps above were helpful and that you have successfully sent email from the SMTP server to Gmail using PHP. Feel free to contribute if you have an issue that is not listed as part of this lesson. Use the comments section below to ask / share feedback.

You can always support by sharing on social media or recommending my blog to your friends and colleagues. If you have any suggestions or problems about this tutorial, please comment on the form below.😊

How to Send Email from localhost in PHP using SMTP

Leave a Reply

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