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.
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.
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}";
}
?>
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.
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.
i want php mail sending function using SMTP for OUTLOOK365 MAILS
This is PHP script presented in the tutorial is good enough.
Is it possible to use this code to send to more than one email address at a time? I tried adding a comma and then another email address, but it would not work.
Hi Andrew,
It will require to make some minor modifications to make it as a bulk mailer.
is there any system to activate mailsever in xampp to send mails in local intranet using localhost
Yes Bindumol. You need to setup a mailserver.
hi, I am facing an issue with php mail function. The thing is mail function is working on localhost but not working on the live server.
If PHP’s mail() is not working, then you need to check your server if it is configured properly to send emails.
How do i send site visitor details they have filled on the form. To send from contact form to my email/database and copy the site visitor
Hi Kiki,
You need to build around this logic and add these functionalities. Mail me if you need support.