How to Send Mail from Localhost XAMPP Using Gmail
Featured In - Magento 1,
Sanjay Jethva
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:
1
2
3
4
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = YourGmailId@gmail.com
sendmail_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
1
2
3
4
5
6
7
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=YourGmailId@gmail.com
auth_password=Your-Gmail-Password
force_sender=YourGmailId@gmail.com(optional)
Script To Send Mail:
1
2
3
4
5
6
7
8
9
10
11
<?php
$to_email = "[email protected]";
$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...";
}
Hope this guide will be helpful!Note:
Please configure “Less secure apps” settings as shown at //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!
4.9
(based on 118 Reviews)
Sanjay Jethva
Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.
Our Popular Magento 2 Extensions & Services
109 Comments
fantasy
Its works fine.
thank youSanjay Jethva
Hello Fantasy,
Glad to know that the above solution is helpful for you
Thank you
Elijah
Excellent peace of work. Much appreciated.
Sanjay Jethva
Hello Elijah,
Thank you for your appreciation.
Thank You
Chetan Bansal
Thank You so much, your valuable information saved my lots of time and effort. keep it up.
Sanjay Jethva
Hello Chetan,
We’re glad to help you!
Thank You
Frans Human
Thank you very much, it helped me a lot.
Sanjay Jethva
Hello Frans,
Glad to know that it helped you.
Thank you so much for the appreciation.
Lingam raja
Error # 11001Host not found.
Sanjay Jethva
Hello Lingam,
It’s working here. Please do follow the steps. You are good to go.
Thank you.
viswanadhan
Parse error: syntax error, unexpected ‘ ‘ (T_STRING) in D:\xampp\htdocs\send_mail_test.php on line 8
Sanjay Jethva
Hello,
It seems as if there’s a mistake in copying the code from this post.
Thanks.
Husain
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.Mary Joy Resurreccion
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
Sanjay Jethva
Hello,
The problem is in sending the mail.
The root script is created.
Thank you.
Alice
Hi! I can’t find the sendmail file at C:\xampp\ 🙁
Sanjay Jethva
Hello Alice,
Please check in C:\xampp\sendmail folder.
Thank you.
Ankit
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 8Sanjay Jethva
Hello Ankit,
The port is wrong. The 25th port is still being used.
I hope it helps.Ankit
Sir I don’t know why it is still using port 25 because I changed it to 587 in both files explicitly. Kindly guide.
Sanjay Jethva
Hello,
Please restart XAMPP.
Thanks.Husain
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.
Sanjay Jethva
Hello Husain,
Please recheck the port and setting.
The issue seems to be due to port only.
Thank you.
maklon
Thank you for the solution!
Sanjay Jethva
My pleasure!
Rayi mallesh
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 itSanjay Jethva
Hello,
There is a mistake in the sendmail_from due to which you are facing this error.
Thanks.
LAUR
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 =
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.Sanjay Jethva
Hello,
I have mentioned in the post to change the SMTP port which you have missed as it is still 25.
Thank you.
karl
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.
Sanjay Jethva
Hello Karl,
Please check the spam folder.
Thanks.Farmeen
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!Sanjay Jethva
Hey,
The PHPMailer is causing the issue.
Thanks.
Vinay
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.
Sanjay Jethva
Hello Vinay,
Please check if you have any issues from those mentioned at the end of the post.
Thanks.
Deepa
I hv don this all steps bt i didn’t get any emails.evn I’m enable allow less app secure setting also.
Sanjay Jethva
Hello Deepa,
Did you check the five reasons listed at the end of the post?
Marie
Hi,
Nice tutorial..!!!
Sanjay Jethva
Thanks!
Amardip
Sir , my localhost xampp server has not found php.ini file
Sanjay Jethva
Hello Amardip,
Please check this – //drops.meetanshi.com/FTLZ2n
This is also one of the methods.
Thank you.
Robertson
set g_smtp_allow_invalid true to permit delivery
Sanjay Jethva
Hello,
Are you using Windows or Mac?
Thank you.
Samyak
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!Sanjay Jethva
Hello Samyak,
You might be facing one of the above listed issues. Please check it once and try again.
Thank you.
wais
Thank you sir, it work for me.
Kevin
Thanks
Lakshmi Chandrasekar
No error. But mail not received
Sanjay Jethva
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.
sanny
pls sir are we configuring php.ini distribution or php.ini development……?
tnx for ur support.Sanjay Jethva
Hello, You have to make changes in the php.ini file.
Thank you.
teja k
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!Sanjay Jethva
Hello,
The issue seems to be in SMTP setup.
Please check your account in which you have setup.
Thank you.
SAIRA
Hy..I have followed all process successfully… all DNE WITHOUT ANY ERROR..bUT NO EMAIL IS SEND BY MAIL ACCOUNT.
Sanjay Jethva
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
Adeyemi
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”}Sanjay Jethva
Hello,
The issue is in SMTP port. This happens when the port is already in use.
Thank you.ogenna
Then what do we do in such cases
Sanjay Jethva
Hey,
You need to free up a port that is already being used.
Thank you.
Pushapak Kumar
may I need to paste or edit in that file?
Sanjay Jethva
Hello Pushpak Kumar,
Yes, you’ll need to make changes in that file.
Thank you.
byukusenge baraka
thanks alot for the help.
but where will i paste this script ??or i will create a new fresh file for it??Sanjay Jethva
Hello,
Yes, you need to create a new file for it.
Thanks
Gert Kroucamp
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.Sanjay Jethva
Hey, it’s great that my tutorial helped you out. And thanks for the appreciation!
Nik Suki
thanks for your guidance!!
Sanjay Jethva
happy to help 🙂
Kura
Thanks for the tutorial. Worked fine for me.
Sanjay Jethva
Hey! Welcome 🙂
Happy to help.
Villydger Pallarca
This is work with CI..good job
Sanjay Jethva
Hey, thanks for the appreciation 🙂
panos alexopoulos
i did all but still dont work
i have windows with 64bits .. is that a problem ?Sanjay Jethva
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.
Aditya
sir,
i follow your instructions but there is an errorParse error: syntax error, unexpected ‘ ‘ (T_STRING) in C:\xampp\htdocs\mail.php on line 8
please help me
Sanjay Jethva
Hey Aditya,
It seems that you have made a mistake of a semicolon (;)
Please refer to the above code and try again.
Thank you.
Tom
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!
Sanjay Jethva
Hi Tom,
Thanks for the appreciation. Yeah, that’s right, this solution can be used in the local environment.
helloworld
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
Sanjay Jethva
Hi, please re-check your configuration. Any mistake in it can cause the error.
Thanks.
ajina
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
Sanjay Jethva
Hello,
Please review your php.ini settings.
Thanks.
dshidende
This is really helpful, Thank you very much
Sanjay Jethva
Thank you!
padmini
Hai,
Email sending failed…
i am getting this error for the code which u have given. please help me out to fix this issue.Sanjay Jethva
Hello,
Please configure “Less secure apps” settings as shown at //support.google.com/cloudidentity/answer/6260879 for proper working as mentioned in the post.
Thanks.padmini
i am not getting the less secure option in my gmail account.
Sanjay Jethva
Hi again,
Please follow these steps: //drops.meetanshi.com/i/NtaEVP
Thanks.
miheer
this code isnt working for me…..it is showing else statement
Walid
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 tableSanjay Jethva
Hi Walid,
The address to which you are sending an Email is not available and that’s why it’s showing the error.
//drops.meetanshi.com/emKGOZ
vanita saini
Thanks for sharing this tutorial
Sanjay Jethva
Glad to know it helped you Vanita.
Danushan Kanagasingam
can we use this code for realhosting?
Sanjay Jethva
Hi Danushan,
You cannot use it for real hosting. It’s enabled by default in real hosting.
Harsh Patel
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 boxSanjay Jethva
Hello Harsh,
Please configure “Less secure apps” settings as shown at //support.google.com/cloudidentity/answer/6260879 to resolve your issue.
Thanks.
miltons
This was really helpful, thanx.
Jeff
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
How to set SMTP server for Gmail PHP?
Writing the PHP Code to Send Email using Gmail SMTP.Step 1: Download PHPMailer library from this github link. ... .Step 2: Writing the PHP Code to make an SMTP connection. ... .Step 3: Include packages and files for PHPMailer and SMTP protocol: ... .Step 4: Initialize PHP Mailer and set SMTP as mailing protocol:.How to change SMTP settings in PHP ini?
Changing php..Open your php.ini file using below: ... .Search [mail function] in the file. ... .Add your mail server details to the file or incase you have one you can change it (mail server can be your own ie. ... .Save/close the php.ini file..What is my SMTP server settings for Gmail?
The default Gmail SMTP details are as follows:.Gmail SMTP server address: smtp.gmail.com..Gmail SMTP name: Your full name..Gmail SMTP username: Your full Gmail address (e.g. you@gmail.com).Gmail SMTP password: The password that you use to log in to Gmail..Gmail SMTP port (TLS): 587..Gmail SMTP port (SSL): 465..How to use Ini_set for mail in PHP?
To set this parameter explicitly in your script you must use the ini_set() option. ini_set("sendmail_from", "you @ your_hosted_domain.com"); The email address you specify should be a hosted domain on our system.