Send Email from Localhost using PHP with SMTP and PHPMailer

by Vincy. Last modified on May 26th, 2023.

Localhost is the web developer’s favorite home. Local development environment is always convenient to develop and debug scripts.

Mailing via a script with in-built PHP function. This may not work almost always in a localhost. You need to have a sendmail program and appropriate configurations.

If the PHP mail() is not working on your localhost, there is an alternate solution to sending email. You can use a SMTP server and send email from localhost and it is the popular choice for sending emails for PHP programmers.

This example shows code to use PHPMailer to send email using SMTP from localhost.

mail sending from localhost using smtp

PHP Mail sending script with SMTP

This program uses the PHPMailer library to connect SMTP to send emails. You can test this in your localhost server. You need access to a SMTP server.

Before running this code, configure the SMTP settings to set the following details.

  1. Authentication directive and security protocol.
  2. Configure SMTP credentials to authenticate and authorize mail sending script.
  3. Add From, To and Reply-To addresses using the PHPMailer object.
  4. Build email body and add subject before sending the email.

The above steps are mandatory with the PHPMailer mail sending code. Added to that, this mailing library supports many features. Some of them are,

Set the SMTP Debug = 4 in development mode to print the status details of the mail-sending script.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once __DIR__ . '/vendor/phpmailer/src/Exception.php';
require_once __DIR__ . '/vendor/phpmailer/src/PHPMailer.php';
require_once __DIR__ . '/vendor/phpmailer/src/SMTP.php';

$mail = new PHPMailer(true);

$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'SET-SMTP-HOST';
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;

$mail->mailer = "smtp";

$mail->Username = 'SET-SMTP-USERNAME';
$mail->Password = 'SET-SMTP-PASSWORD';

// Sender and recipient address
$mail->SetFrom('SET-SENDER-EMAIL', 'SET-SENDER_NAME');
$mail->addAddress('ADD-RECIPIENT-EMAIL', 'ADD-RECIPIENT-NAME');
$mail->addReplyTo('ADD-REPLY-TO-EMAIL', 'ADD-REPLY-TO-NAME');

// Setting the subject and body
$mail->IsHTML(true);
$mail->Subject = "Send email from localhost using PHP";
$mail->Body = 'Hello World!';

if ($mail->send()) {
    echo "Email is sent successfully.";
} else {
    echo "Error in sending an email. Mailer Error: {$mail->ErrorInfo}";
}
?>

Google and Microsoft disabled insecure authentication

Earlier, programmers conveniently used GMail’s SMTP server for sending emails via PHP. Now, less secure APPs configuration in Google is disabled. Google and Microsoft force authentication via OAuth 2 to send email.

There is ill-informed text going around in this topic. People say that we cannot send email via Google SMTP any more. That is not the case. They have changed the authentication method.

IMPORTANT: I have written an article to help you send email using xOAuth2 via PHP. You can use this and continue to send email using Google or other email service providers who force to use OAuth.

Alternate: Enabling PHP’s built-in mail()

  1. If you do not have access to an email SMTP server.
  2. If you are not able to use xOAuth2 and Google / Microsoft’s mailing service.

In the above situations, you may try to setup your own server in localhost. Yes! that is possible and it will work.

PHP has a built-in mail function to send emails without using any third-party libraries.

The mail() function is capable of simple mail sending requirements in PHP.

In localhost, it will not work without setting the php.ini configuration. Find the following section in your php.ini file and set the sendmail path and related configuration.

php.ini mail setting

Download

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “Send Email from Localhost using PHP with SMTP and PHPMailer”

Leave a Reply

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

↑ Back to Top

Share this page