Hello friends, today in this blog you’ll learn How to configure XAMPP to Send Mail from Localhost in PHP. If you don’t know, XAMPP is an abbreviation for cross-platform, Apache, MySQL, PHP, and Perl. Using XAMPP we can work on our local server and test our local copies or projects of websites using PHP code and MySQL databases.
As a part of the experiment, developers need to send emails and we all know that sending mail from localhost using PHP can be much more painful if we don’t know how to properly configure XAMPP for it.
Before starting this blog, if you want to send an email in node.js applications, then you can see this blog How to Send Email in Node.js using Nodemailer & Gmail.
To send mail from localhost using XAMPP, we’ve to configure XAMPP after installing it. To configure the XAMPP server to send mail from the local host, we have to make some changes in two files one is PHP and another one is Sendmail.
First, go to the XAMPP installation directory and open the XAMPP folder and follow the below steps same: I’ve installed XAMPP in the C directory.
1. Go to the (C:xampp\php) and open the PHP configuration setting file then find the [mail function]
by scrolling down or simply press ctrl+f to search it directly then find the following lines and pass these values. Remember, there may be a semicolon ; at the start of each line, simply remove the semicolon from each line which is given below.
[mail function] For Win32 only. http://php.net/smtp SMTP=smtp.gmail.com http://php.net/smtp-port smtp_port=587 sendmail_from = your_email_address_here sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
That’s all for this file, press ctrl+s to save this file and then close it.
2. Now, go the (C:\xampp\sendmail) and open the sendmail configuration setting file then find sendmail
by scrolling down or press ctrl+f to search it directly then find the following lines and pass these values. Remember, there may be a semicolon ; at the start of each line, simply remove the semicolon from each line which is given below.
smtp_server=smtp.gmail.com smtp_port=587 error_logfile=error.log debug_logfile=debug.log auth_username=your_email_address_here auth_password=your_password_here force_sender=your_email_address_here (it's optional)
That’s all for this file, press ctrl+s to save this file and then close it. After all changes in the two files, don’t forget to restart your apache server.
Now, you’re done with the required changes in these files. To check the changes you’ve made are correct or not. First, create a PHP file with the .php extension and paste the following codes into your PHP file. After pasting the codes, put your details into the given variables – In the $receiver variable put the receiver email address, in the $subject variable put the email subject and do respectively.
<?php $receiver = "receiver email address here"; $subject = "Email Test via PHP using Localhost"; $body = "Hi, there...This is a test email send from Localhost."; $sender = "From:sender email address here"; if(mail($receiver, $subject, $body, $sender)){ echo "Email sent successfully to $receiver"; }else{ echo "Sorry, failed while sending mail!"; } ?>
After completing these steps, just open this PHP file on your browser. If your mail is sent successfully then there is appears a success message “Email sent successfully to …..” and in the case your mail not sent then there is appears “Sorry, failed while sending mail!”.
If mail is sent then check whether the receiver has got your email or not. If yes, then great you did all changes perfectly. If not, check all the changes that you have done earlier are correct or not.
Note: Google removed the “Less secure apps” feature on May 30, 2022. So you need to do some extra steps and make changes in the files to send mail from localhost using Gmail.
Instead of using your Google account password as auth_password in sendmail configuration file, you need to use an App password which is 16 characters long.
To create an app password, go to manage your google account > Click Security > Under “Signing in to Google,” select App passwords > Select App & Device, and then you’ll get a 16-character code and this is your password. For more details about creating App Passwords, read the Official Google Article.
i did solve the error u just need to use phpmailer instead of ()mail
u need to install php mailer in your vs code
first open your terminal in vs code and paste this composer require phpmailer/phpmailer
then it will be success.
second modify the controllerUserData.php “if user signup button” change the old to this
// Include PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require ‘C:\xampp\htdocs\Login\vendor\autoload.php’; // If using Composer
// require ‘path/to/PHPMailer/src/Exception.php’;
// require ‘path/to/PHPMailer/src/PHPMailer.php’;
// require ‘path/to/PHPMailer/src/SMTP.php’; // If not using Composer
//if user signup button
if(isset($_POST[‘signup’])){
$name = mysqli_real_escape_string($con, $_POST[‘name’]);
$email = mysqli_real_escape_string($con, $_POST[’email’]);
$password = mysqli_real_escape_string($con, $_POST[‘password’]);
$cpassword = mysqli_real_escape_string($con, $_POST[‘cpassword’]);
if($password !== $cpassword){
$errors[‘password’] = “Confirm password not matched!”;
}
$email_check = “SELECT * FROM usertable WHERE email = ‘$email'”;
$res = mysqli_query($con, $email_check);
if(mysqli_num_rows($res) > 0){
$errors[’email’] = “Email that you have entered is already exist!”;
}
if(count($errors) === 0){
$encpass = password_hash($password, PASSWORD_BCRYPT);
$code = rand(999999, 111111);
$status = “notverified”;
$insert_data = “INSERT INTO usertable (name, email, password, code, status)
values(‘$name’, ‘$email’, ‘$encpass’, ‘$code’, ‘$status’)”;
$data_check = mysqli_query($con, $insert_data);
if($data_check){
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = ‘smtp.gmail.com’; // Set the SMTP server to send through
$mail->SMTPAuth = true;
$mail->Username = ‘your_email_address’; // SMTP username
$mail->Password = ‘your app password’; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom(‘your_email_address’, ‘Mailer’);
$mail->addAddress($email); // Add a recipient
// Content
$mail->isHTML(true);
$mail->Subject = ‘Email Verification Code’;
$mail->Body = “Your verification code is $code”;
$mail->send();
$info = “We’ve sent a verification code to your email – $email”;
$_SESSION[‘info’] = $info;
$_SESSION[’email’] = $email;
$_SESSION[‘password’] = $password;
header(‘location: user-otp.php’);
exit();
} catch (Exception $e) {
$errors[‘otp-error’] = “Message could not be sent. Mailer Error: {$mail->ErrorInfo}”;
}
} else {
$errors[‘db-error’] = “Failed while inserting data into database!”;
}
}
}
third rename this path to your path.
require ‘C:\xampp\htdocs\Login\vendor\autoload.php’; // If using Composer
fourth to get the app password you must go to google user accounts then find the security>on f2A authentication>search app password>create app>paste the password no space. $mail->Password = ‘your app password’; // SMTP password
fifth do not use this link https://www.codingnepalweb.com/configure-xampp-to-send-mail-from-localhost/ because i tried to work on this and its showing error and the only solution is to use the phpmailer.
Warning: mail(): Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set() in C:\xampp\htdocs\mail.php on line 7
Sorry, failed while sending mail!
WHAT IS THE ERR I CANT FIND I PUT PORT NO 587
I WANT A CONTACT WAY OF YOU FOR FURTHR COMMUNICATION
PLZ
I don’t understand. What is my password here in sendmail.ini file?
Sir please i need ur help how did you know php, i want to learn php but i can’t understand, i have no idea php is so hard for me to learn, please teach me php
I found a solution! 🙂
https://support.google.com/accounts/answer/185833
1. Go to your google account
2. Select Security
3. Under Signing in to Google select App Passwords
4. Select App > Mail
5. Select Device > (I am using Windows Computer)
6. Click Generate
Use this generate code as the auth_password in the sendmail config textfile instead of your actual password and it should fix the problem!
Thanks bruh it really works
Google no longer has a Less secure app access setting option as of May 30, 2022. Do you have any suggestions on how to connect my google account to send an email?
https://support.google.com/accounts/answer/6010255?hl=en&utm_source=google-account&utm_medium=profile-less-secure-apps-card#zippy=%2Cuse-an-app-password
Thanks bro.
do i need to remove all semi colom under php configuration before it work
No, you’ve to remove some semicolons for it. Read the blog or watch the video for it.
Thanks Sir! After one year I found here the perfect solution.
Great work.
Can u pls tell how to download and install XAAMP??
How to fix this (The requested URL was not found on this server)
thanks bro, you are a life saver, took me up to 3 hours watching and reading the article together before having a clear understanding. it works perfectly, i am very grateful
Happy to help you 🙂
Great job bro, you saved me! I already love youuuu
thankyou so much u saved me
You’re most welcome
Can you show how to send mail after uploading it in a hosting server
Is it the same process if I use a domain email instead of gmail?
You can set up your domain email on Gmail and redirect all domain emails to your Gmail account.
It works! Cheers
Alhamdulillah it worked first it was not working but after i turned on less secure app access, it worked
thank you for explaining the configuration of Xampp and for the source code. you really helped us 🙂
Glad it was helpful
Good day, please how can I password protect a page on a website, where logged in users will have to enter their registered password only without username to have access
Great work!!
Do you have code to use this on live site with PHPmailer?
Thanks.
No, but I’ll try to make a video and write a blog post on it.
Hi Sir.
Where do I enter the email server credentials to use this on live site?
Thank you.
You don’t need it on online server. This is only for localhost.
big thanks!! its worked
You’re welcome.
Hi, thx for your tutorial, the last internet page says it worked, but I don’t get anything in my mailbox after 20 minutes, is it normal ?
And, I’m actually learning programmation, and for my projet, in C# I would like to send mail, and we just work in localhost, so I guess what you have showed here should be useful to me.
Cause I use XAMPP, but how can I use this php script now ? Do you have an Idea ? Or maybe I am totally wrong
(Sry for my very bad english, I’m french and we are known to be the worst guys in world to learn other language ^^’)
Sorry, I don’t know about c# so I can’t tell you what should you need do.
Thank You so much for this tutorial
It’s working
You are most welcome.
what kind of server u was used?
I still facing problem plz tell me how it was be solve
which password we have to write in the Sendmail file
Your Gmail account password
hello sir how to setup in magento 2 gmail sending in xampp server
Sorry I don’t know about it
It is saying:
Email Sent Successfully to [email protected]
But I am not getting any email
___ is replaced with my email I don’t want to reveal the email address. Thank you
Make sure you’ve entered an email that exists and you may have to wait a minute because it may take some time. You can also check your spam folder.
it’s worked
Massive Thanks.
You’re welcome!
Hi. What can i do when i got “Email sent successfully”, but i don’t get any mail on my mail inbox? i enabled that thing with “not secure sites” on my account, but i still don’t get any mail. Do you know how can i fix this? I’ve been struggling 4 days to resolve this. Help, please!!
It’s because you may have entered the wrong email and the email sent to someone else. Please try again.
But if i get the “Email sent successfully”, that means it should work, right? It can’t be a server error or something, right??? I’m really tired, i can’t give up, because i can’t finish my site without it. I worked over 20 hours to resolve this problem, :(( I’m gonna ask a friend if he can do that. Thanks for your help btw
hi, sir please make the video for this process please sir soon I am waiting for your video. make as soon as possible.
I’ve mentioned all steps or changes on this blog. Please read properly.
How to fix it in web hosting? PLease reply shortly…
I didn’t understand
Parse error: syntax error, unexpected token “@”, expecting “)” in C:\xampp\htdocs\mail.php on line 7
You may did mistakes while changing. Please recheck properly.
Thanks for doing tutorials. What to do if I'm using macbook?
hi? did you find any solution for this? It is the same error I'm facing rn help plsss
hello sir how to configure login and signup in cpanel
auth_username=your_email_address_here
auth_password=your_password_here
in this we have to pass email address and password or paste it like this only
not able to find mail function in php
open php folder in vs code then you’ll get php.ini file in that file mail function are present
Sir can you pls make it video downloader and publish it sir pls.
Sure but if this video will get a good response – Download Any YouTube Video Thumbnail using HTML CSS JavaScript & PHP
PLEASE MAKE A VIDEO ON IT
its not working please make it on javascript smtp.js
i watched your other tutorial and i have a question sir?
should i enter the last part of the code PHP CODE: to my controllerUserData.php or separate file?
Heyy the last php code
So be with which name just .php or like some name send.php???
PLZZ helpp
1.when i click on the send mail it shows this page is not working can you please help?
2.we need to link our php file to index.html or not?
Hi, how can I config wampp server for send mail
I didn't recieve the email, and yes, I turn on the "less secure apps" on Google. The blog was great but I think you should update it, so that you can address the multiple problems we are all having.
Hello sir how to use on infinity hosting or any website i upload this login page but not working
$receiver = "receiver email address here";
$subject = "Email Test via PHP using Localhost";
$body = "Hi, there…This is a test email send from Localhost.";
$sender = "From:sender email address here";
if(mail($receiver, $subject, $body, $sender)){
echo "Email sent successfully to $receiver";
}else{
echo "Sorry, failed while sending mail!";
}
where want to paste this code. pls
Ok I'll update it soon. When I posted this blog, there is php.ini file for configuration but now it's only php.
Nice one! That worked!
Thanks worked .. I was getting multiple issues ..as I open the .ini files in notepad… If you can update the comment .. the ini.. to be opened in PHP … most error will be sorted.
Yes I think it definitely help to all beginners.
It would have been better if there was a video to help beginners like me
Maybe you don't have permission to change the C drive files
The above one is. There is a name with php before php.ini-development. You've to open it
Please why do I receive an error message say "Access denied"while saving the PHP.ini file
Also there's PHP.in-production and PHP.ini-development
I keep getting this error. Please help. I have configured properly as mentioned abovee
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
Jai tombé sur cet erreur là: "Failed while sending code!"
Great now you can send Email to any existing email address from localhost
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:xampphtdocsemailcontrollerUserData.php on line 19
whats happen??
Warning: mail(): Failed to connect to mailserver at "smtp.gmail.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:xampphtdocsSIScontact.php on line 41
its been 2hrs since ive been trying to read the blog
pleasseeee helpppp
i already got it after 3h THANKK YOU.
for errors , kindly restart your apache after encoding those codes.
and some are in comment in php.ini so we should remove the " ; " to enable the lines
after that dont forget to enable the less app in gmail settings
much thanks. u saved my life
Yes you're right
Ok stay tuned
Login-singup-form-with-email-verification using phpmailer can you make this video
did you find the solution on this error? need help…
Thanks for the tutorial. I got this error:
21/01/20 11:13:23 : Username and Password not accepted. Learn more at https://support.google.com/mail/?p=BadCredentials f13sm1134675iog.18 – gsmtp
To fix this problem, I had to login to my Gmail account, go to security, and then turn on "Less secure app access." This is "not recommended" according to Google, but it wouldn't work without this setting turned on.
This blog is about configuring XAMPP…I think you're facing the error which is not related to this blog. So please read this carefully – https://www.codingnepalweb.com/2020/09/login-signup-form-with-email-verification-php-mysql-bootstrap.html and must be careful while putting the name of database, table, and columns.
sorry to disturb admin. I have followed the instructions for creating your website and I entered the registration menu. I have filled in all the data then I press the register button. a text appears. FAILED WHEN ENTERING DATA TO DATABASE ??
Please explain where I went wrong. Thank you
sorry to disturb admin. I have followed the instructions for creating your website and I entered the registration menu. I have filled in all the data then I press the register button. a text appears. FAILED WHEN ENTERING DATA TO DATABASE ??
Please explain where I went wrong. Thank you
sorry to disturb admin. I have followed the instructions for creating your website and I entered the registration menu. I have filled in all the data then I press the register button. a text appears. FAILED WHEN ENTERING DATA TO DATABASE ??
Please explain where I went wrong. Thank you
Please read the blog carefully…I can't do anything except this…All answers are in the blog.
how do I get a specific password from google?? the error log keeps giving me this msg Application-specific password required. failed while sending email
Hi thanks for this tutorial and instructions but I got the error warning after I signed up.
The data is already input on my data base. Name, email, password and the code also the verification
But the email is not.
Warning said is mail():SMTP server response 530 5.7.0 must issue a starTTLS command first.gjlsm12559445pjb.11 – gsmtp in "distinction folder" on line 33 where located this code–>> if(mail($email, $subject, $message, $sender)){ . . . . ..
No error in the code, but mail is not sending
No error while PHP codes runs ,and it turn to OTP page.but mail didn't send to corresponding ?.pls help me out
Notice: Undefined index: message in C:xampphtdocsmail.php on line 3
Warning: mysqli::__construct(): (HY000/1049): Unknown database 'contact us' in C:xampphtdocsmail.php on line 6
Connection Failed : Unknown database 'contact us'
Notice: Undefined index: message in C:xampphtdocsmail.php on line 3
Warning: mysqli::__construct(): (HY000/1049): Unknown database 'contact us' in C:xampphtdocsmail.php on line 6
Connection Failed : Unknown database 'contact us'
we are feeling bore with HTML & CSS only. PLease Upload PHP related videos atleast 2 in a week
Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. r17sm13504747pfr.153 – gsmtp in C:xampphtdocsmail.php on line 7
Sorry, failed while sending mail!
bro, create a telegram group so that we will share our queires there so that you can respond to all
Sir, when will you make a tutorial about the login and sign up with email otp verification??? huhu
after registration i keep getting error of failed while sending code even after enabling less secure apps what should i do
for all of u that have "smtp_port" setting in php.ini or use ini_set() in C:xampphtdocsswdacontrollerUserData.php on line 33 error pls restart your apache.
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:wamp64wwwlogincontrollerUserData.php on line 118
what was the mistake?
Can you send me the screenshot of your problems on my Instagram account?
Did you enable less secure app panel on your google account?
Did you enbale less secure app panel on your google account?
When run the php file result is email sending failed
Sir I configured properly but result is email sending failed.pls help
When will you be making this video, I really need help on this for my school project?
Sure stay tuned with us.
bro please make a video for it i am confused
yes
You're welcome 🙂
Create a php file inside the HTDOCS folder with .php extension and paste the following codes in this file then put the receiver email and sender email address in the mentioned fields.
<?php
$receiver = "[email protected]";
$subject = "Email Test via PHP using Localhost";
$body = "Hi, there…This is a test email send from Localhost.";
$sender = "From:[email protected]";
if(mail($receiver, $subject, $body, $sender)){
echo "Email sent successfully to $receiver";
}else{
echo "Sorry, failed while sending mail!";
}
This php file with what name we have to save and where it showing error
Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. 78sm5550625pfz.211 – gsmtp in C:xampphtdocsLogin and Signup Form with Email Verification – PHPcontrollerUserData.php on line 33 (Failed while sending code) I configure all xampp and sendmail settings.
Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. x5sm10407906pfr.83 – gsmtp in C:xampphtdocsLogin and Signup Form with Email Verification – PHPcontrollerUserData.php on line 33
Maybe the email address you've entered doesn't exist or You may haven't on Less Secure Apps panel in your google account
sir, i configured properly…… but when i try to signup …. it redirect me to otp verification page…. no email comes to the gmail… please help me…
Please configure carefully. Maybe you did some mistakes.
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:xampphtdocsswdacontrollerUserData.php on line 33
please help sir
You haven't configured your XAMPP properly…Please read this blog carefully and apply the changes in your files.
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:xampphtdocsusertablecontrollerUserData.php on line 33
I KEEP GETTING THIS
Please recheck your changes files with the above given codes.
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:xampphtdocsusertablecontrollerUserData.php on line 33
what does it mean
Please recheck your changes in files.
I followed all the steps but an error message appeared to me saying
Warning: mail (): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set () in C: xampp htdocs p1 mail.php on line 7
Sorry, failed while sending mail
English please!
Hola buenas tardes, me manda el siguiente error:
Advertencia:mail(): No se pudo conectar al servidor de correo en el puerto 25 "localhost", verifique su configuración "SMTP" y "smtp_port" en php.ini o utilice ini_set() en C:-xampp-htdocs-Verificacion de email-controllerUserData.php en la línea 33
hy
Sadly.. I haven't knowledge of c++
can u make something like this again but with rust instead of php. Rust is faster and synatically equal to c++.
Ok bro
Bro please make a video in it.
You've to create a PHP file with the extension .php and paste the last codes on that PHP file but before pasting the codes make sure you've configured the XAMPP and turn on less secure app on your google account. You can read this blog carefully.
which php file should i copy and paste the last code ?, i got an error that says failed while sending code
Thank you 🙂
Thanks for this, now if we are using another server online,(after publishing our website online),will the mail sending still work?
No you've configure require changes in your server file. This is only works on XAMPP server.
Thanks for this, now if we are using another server online, (after uploading our codes online), will the mail sending still work?