How to Send Mail from Localhost XAMPP Using Gmail
XAMPP is Cross-Platform (X), Apache (A), MariaDB (M), PHP (P) and Perl (P). It is for developers to create a local webserver for testing or deployment purposes.
As a part of testing, developers need to send emails that are always going to be there when it comes to online businesses.
To send mail from localhost XAMPP using Gmail, configure XAMPP after installing it. Follow the below steps for the same.
Steps to Send Mail From Localhost XAMPP Using Gmail:
- Open XAMPP Installation Directory.
- Go to C:\xampp\php and open the php.ini file.
- Find [mail function] by pressing ctrl + f.
- Search and pass the following values:
1234SMTP=smtp.gmail.comsmtp_port=587sendmail_from = YourGmailId@gmail.comsendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" - Now, go to C:\xampp\sendmail and open sendmail.ini file.
- Find [sendmail] by pressing ctrl + f.
- Search and pass the following values
1234567smtp_server=smtp.gmail.comsmtp_port=587error_logfile=error.logdebug_logfile=debug.logauth_username=YourGmailId@gmail.comauth_password=Your-Gmail-Passwordforce_sender=YourGmailId@gmail.com(optional)
Script To Send Mail:
1 2 3 4 5 6 7 8 9 10 11 |
<?php $subject = "Simple Email Test via PHP"; $body = "Hi,nn This is test email send by PHP Script"; $headers = "From: sender\'s email"; if (mail($to_email, $subject, $body, $headers)) { echo "Email successfully sent to $to_email..."; } else { echo "Email sending failed..."; } |
Note:
Please configure “Less secure apps” settings as shown at https://support.google.com/cloudidentity/answer/6260879 for proper working.
Also, if you are still facing issues, the reason can be one of the below:
- The mails are being sent late
- The mail is not configured properly
- The port is blocked
- Gmail does not have required permissions
- Password is wrong
Any doubts on the topic? Please mention them in the Comments section below and I’d be glad to help you out.
Feel free to share the solution via social media.
Thanks!
Still need help? Hire our Adobe-certified Magento experts.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
113 Comments
Its works fine.
thank you
Hello Fantasy,
Glad to know that the above solution is helpful for you
Thank you
Getting message as “Email sending failed…”
Hey Suresh,
Please check configuration of php.ini file as mentioned in blog,
As it is working properly from our end.
Excellent peace of work. Much appreciated.
Hello Elijah,
Thank you for your appreciation.
Thank You
Thank You so much, your valuable information saved my lots of time and effort. keep it up.
Hello Chetan,
We’re glad to help you!
Thank You
Thank you very much, it helped me a lot.
Hello Frans,
Glad to know that it helped you.
Thank you so much for the appreciation.
Error # 11001Host not found.
Hello Lingam,
It’s working here. Please do follow the steps. You are good to go.
Thank you.
Parse error: syntax error, unexpected ‘ ‘ (T_STRING) in D:\xampp\htdocs\send_mail_test.php on line 8
Hello,
It seems as if there’s a mistake in copying the code from this post.
Thanks.
Please I have tried the method for many times. But still don’t send mail.
Hey Yira,
Can You please tell us your error in a bit Descriptive Form ?
Hello,
I was getting error of port=25 but as you sggested to restart the XAMPP i did it . Now the error message is gone
but it is still failing to send email.
Email sending failed…
Pls guide.
Parse error: syntax error, unexpected ‘ ‘ (T_STRING) in C:\xampp\htdocs\testing\testing.php on line 8 is there anyone that can solve this?? I actually copy and then paste the code. I don’t know what’s wrong. I need help
Hello,
The problem is in sending the mail.
The root script is created.
Thank you.
Hi! I can’t find the sendmail file at C:\xampp\ 🙁
Hello Alice,
Please check in C:\xampp\sendmail folder.
Thank you.
Tried exactly what you mentioned. Enabled less secure apps in gmail. The remaining 5 options were also not applicable, still getting the error:
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:xampphtdocsEC_websiteindex.php on line 8
Hello Ankit,
The port is wrong. The 25th port is still being used.
I hope it helps.
Sir I don’t know why it is still using port 25 because I changed it to 587 in both files explicitly. Kindly guide.
Hello,
Please restart XAMPP.
Thanks.
Hello Mr Sanjay,
After following all of the above mentioned steps till your last comment “restart XAMPP” it is now showing
Email sending failed…
It is not showing any error message .. earlier it was showing the same error reported by Mr Ankit.
Pls Guide.
Hello Husain,
Please recheck the port and setting.
The issue seems to be due to port only.
Thank you.
Thank you for the solution!
My pleasure!
hey i used same code as you said but it is showing error
Warning: mail(): “sendmail_from” not set in php.ini or custom “From:” header missing in C:\xampp\htdocs\ELECTRICALS\index.php on line 13
how can I solve it
Hello,
There is a mistake in the sendmail_from due to which you are facing this error.
Thanks.
Hello, i try to send mail from localhost php in win10 and i receved this errror
Warning: mail() [function.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 D…
-i set in php ini
SMTP = smtp.gmail.com
smtp_port = 25
sendmail_from = [email protected]
sendmail_path = “\”D:\……
-and to sendmail smtp_server=smtp.gmail.com
smtp_port=4
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=pass
[email protected]
– where is my error?
THANKS.
Hello,
I have mentioned in the post to change the SMTP port which you have missed as it is still 25.
Thank you.
when I run the php it says email successfully sent but I don’t receive anything in my gmail, I’ve turned on insecure access and same happens.
Hello Karl,
Please check the spam folder.
Thanks.
hello sir, i did the above configuration and when i try to send a test mail from wp mail SMTP it gives a problem and the error says “PHPMailer was able to connect to SMTP server but failed while trying to send an email.”.
what to do .Please help!
Hey,
The PHPMailer is causing the issue.
Thanks.
Hello sir, i applied all the steps but when i m running the script to send mail then
Email sending failed error is showing… please help me.
Hello Vinay,
Please check if you have any issues from those mentioned at the end of the post.
Thanks.
I hv don this all steps bt i didn’t get any emails.evn I’m enable allow less app secure setting also.
Hello Deepa,
Did you check the five reasons listed at the end of the post?
Hi,
Nice tutorial..!!!
Thanks!
Sir , my localhost xampp server has not found php.ini file
Hello Amardip,
Please check this – https://drops.meetanshi.com/FTLZ2n
This is also one of the methods.
Thank you.
set g_smtp_allow_invalid true to permit delivery
Hello,
Are you using Windows or Mac?
Thank you.
Hey!
So I followed all the steps given. It not showing any error but the mail is not being received or sent. I will configured the files as per instructions.
Please help!
Hello Samyak,
You might be facing one of the above listed issues. Please check it once and try again.
Thank you.
Thank you sir, it work for me.
Thanks
No error. But mail not received
Hello,
The issue can be due to any one of the following:
1. The mails are being sent late
2. The mail is not configured properly
3. The port is blocked
4. Gmail does not have required permissions
5. Password is wrong
Thanks.
pls sir are we configuring php.ini distribution or php.ini development……?
tnx for ur support.
Hello, You have to make changes in the php.ini file.
Thank you.
hi there, I am getting this error …Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. z62sm5560709pfb.47 – gsmtp in C:\xampp\htdocs\form2\mail_handler.php on line 13
Something went wrong!
Hello,
The issue seems to be in SMTP setup.
Please check your account in which you have setup.
Thank you.
Hy..I have followed all process successfully… all DNE WITHOUT ANY ERROR..bUT NO EMAIL IS SEND BY MAIL ACCOUNT.
Hello Saira,
There are multiple reasons for the issue:
1. The mails are being sent late
2. The mail is not configured properly
3. The port is blocked
4. Gmail does not have required permissions
5. Password is wrong
Hello Sir, I did exactly as you said but I keep getting this error.
Warning: mail(): Failed to connect to mailserver at “smtp.gmail.com” port 587, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set() in C:\xampp\htdocs\dada_php\contact_us.php on line 22
{“success”:”true”}
Hello,
The issue is in SMTP port. This happens when the port is already in use.
Thank you.
Then what do we do in such cases
Hey,
You need to free up a port that is already being used.
Thank you.
may I need to paste or edit in that file?
Hello Pushpak Kumar,
Yes, you’ll need to make changes in that file.
Thank you.
thanks alot for the help.
but where will i paste this script ??or i will create a new fresh file for it??
Hello,
Yes, you need to create a new file for it.
Thanks
Thank you. Using your directions above, provide me with success.
I am learning to develop websites, front end and back end.
A lot to learn from scratch, a lot of research.
I am grabbing around and test all options available.
New learner, new software updates of window 10 and xampp with old
directives, confusing me.
I’m good now, thank you.
Hey, it’s great that my tutorial helped you out. And thanks for the appreciation!
thanks for your guidance!!
happy to help 🙂
Thanks for the tutorial. Worked fine for me.
Hey! Welcome 🙂
Happy to help.
This is work with CI..good job
Hey, thanks for the appreciation 🙂
i did all but still dont work
i have windows with 64bits .. is that a problem ?
Hey,
I have implemented the same solution in windows with 64 bits only. So that’s not an issue.
Please check your settings again.
Thank you.
sir,
i follow your instructions but there is an error
Parse error: syntax error, unexpected ‘ ‘ (T_STRING) in C:\xampp\htdocs\mail.php on line 8
please help me
Hey Aditya,
It seems that you have made a mistake of a semicolon (;)
Please refer to the above code and try again.
Thank you.
Super helpful managed to get it working just fine! FYI for anyone else you are required to turn on less secure apps in your gmail account and any good hosting service will have this set up as default this is just for testing on localhost! Have fun!
Hi Tom,
Thanks for the appreciation. Yeah, that’s right, this solution can be used in the local environment.
Hi, I have an error message:
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 E:\xampp\htdocs\testing\mailTesting.php on line 8
Email sending failed…
I have checked my php.ini file and everything is in order, can you help me out? Thanks alot
Hi, please re-check your configuration. Any mistake in it can cause the error.
Thanks.
hi..
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\ajina\DBtoPHP\form.php on line 13
Email sending failed..
this is the error showing
Hello,
Please review your php.ini settings.
Thanks.
This is really helpful, Thank you very much
Thank you!
Hai,
Email sending failed…
i am getting this error for the code which u have given. please help me out to fix this issue.
Hello,
Please configure “Less secure apps” settings as shown at https://support.google.com/cloudidentity/answer/6260879 for proper working as mentioned in the post.
Thanks.
i am not getting the less secure option in my gmail account.
Hi again,
Please follow these steps: https://drops.meetanshi.com/i/NtaEVP
Thanks.
this code isnt working for me…..it is showing else statement
Thank you , it really helps me . I have juste one problem which is :
when I send a mail from php to my mailbox , I receive a Mail Delivery Subsystem which tell me that my adress is not avalaible.
response of the server:
550 5.1.1 :
Recipient address rejected: User unknown in relay recipient table
Hi Walid,
The address to which you are sending an Email is not available and that’s why it’s showing the error.
https://drops.meetanshi.com/emKGOZ
Thanks for sharing this tutorial
Glad to know it helped you Vanita.
can we use this code for realhosting?
Hi Danushan,
You cannot use it for real hosting. It’s enabled by default in real hosting.
Hey there,
the code seems to work but the actually mail was not sent.
It said mail sent succesfully but mail didnt showup in the inbox or sent box
Hello Harsh,
Please configure “Less secure apps” settings as shown at https://support.google.com/cloudidentity/answer/6260879 to resolve your issue.
Thanks.
This was really helpful, thanx.
This worked for me setting it up on two computers, but a third laptop is giving this error: Error: [SocketException] Could not send email: Function ereg() is deprecated
even though my code isn’t using ereg() anywhere.
Hello,
The problem can be with PC settings or the version of XAMPP.
Thanks.
throw this error
Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\mailtest.php on line 8
Hello,
This may happen if you have forgotten to place a “;” at line 8 in the script to send mail.
Thanks.
This still isn’t working for me. I get a success message each time, but no email.
Hello Brandon,
Please configure “Less secure apps” settings as shown at https://support.google.com/cloudidentity/answer/6260879 to resolve your issue.
Thanks.
Thanks Sanjay Sir. For this code I will always grateful to you.
Happy to help 🙂
Thank you for making this look simple, amazing tutorial
Thanks for the appreciation 🙂
Pretty amazing tutorial
Thank you so much Kalpesh for the appreciation!