Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Magento Extensions 

Magento Development Services

How to Send Mail from Localhost XAMPP Using Gmail


In How to - Magento 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, con gure XAMPP after installing it. Follow the below steps for
the same.

Steps to Send Mail From Localhost XAMPP Using Gmail:


1. Open XAMPP Installation Directory.
2. Go to C:\xampp\php and open the php.ini le.
3. Find [mail function] by pressing ctrl + f.
4. Search and pass the following values:

1 SMTP=smtp.gmail.com
2 smtp_port=587
3 sendmail_from = YourGmailId@gmail.com
4 sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

5. Now, go to C:\xampp\sendmail and open sendmail.ini le.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 1/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

6. Find [sendmail] by pressing ctrl + f.


7. Search and pass the following values

1 smtp_server=smtp.gmail.com
2 smtp_port=587
3 error_logfile=error.log
4 debug_logfile=debug.log
5 auth_username=YourGmailId@gmail.com
6 auth_password=Your-Gmail-Password
7 force_sender=YourGmailId@gmail.com(optional)

Script To Send Mail:


1 <?php
2 $to_email = "receipient@gmail.com";
3 $subject = "Simple Email Test via PHP";
4 $body = "Hi,nn This is test email send by PHP Script";
5 $headers = "From: sender\'s email";
6  
7 if (mail($to_email, $subject, $body, $headers)) {
8     echo "Email successfully sent to $to_email...";
9 } else {
10     echo "Email sending failed...";
11 }

Hope this guide will be helpful!

Note:

Please con gure “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:

1. The mails are being sent late
2. The mail is not con gured properly
3. The port is blocked
4. Gmail does not have required permissions
5. 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!

Get Weekly Updates


Never miss Magento tips, tricks, tutorials, and news.

*Email Address SUBSCRIBE

4.8
https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 2/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

(based on 103 Reviews)

Sanjay Jethva
Sanjay is a co-founder at Meetanshi. He is a Certi ed 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.

 

101 comments

Lingam raja
- Reply

Error # 11001Host not found.

Sanjay Jethva 
- Reply

 Hello Lingam,

It’s working here. Please do follow the steps. You are good to go.

Thank you.

viswanadhan
- Reply

Parse error: syntax error, unexpected ‘ ‘ (T_STRING) in D:\xampp\htdocs\send_mail_test.php on line 8

Sanjay Jethva 
- Reply

Hello,
It seems as if there’s a mistake in copying the code from this post.
Thanks.

Husain
- Reply

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.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 3/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Mary Joy Resurreccion


- Reply

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 
- Reply

Hello,
The problem is in sending the mail.
The root script is created.
Thank you.

Alice
- Reply

Hi! I can’t nd the sendmail le at C:\xampp\

Sanjay Jethva 
- Reply

Hello Alice,

Please check in C:\xampp\sendmail folder.

 Thank you.


https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 4/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Ankit
- Reply

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

Sanjay Jethva 
- Reply

Hello Ankit,
The port is wrong. The 25th port is still being used.
I hope it helps.

Ankit 
- Reply

Sir I don’t know why it is still using port 25 because I changed it to 587 in both les explicitly.
Kindly guide.

Sanjay Jethva 
- Reply

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.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 5/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

maklon
- Reply

Thank you for the solution!

Sanjay Jethva 
- Reply

My pleasure!

Rayi mallesh
- Reply

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

Sanjay Jethva 
- Reply

Hello,
There is a mistake in the sendmail_from due to which you are facing this error.
Thanks.

 LAUR
- Reply
 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 = user@gmail.com
sendmail_path = “\”D:\……
-and to sendmail smtp_server=smtp.gmail.com
smtp_port=4
error_log le=error.log
debug_log le=debug.log
auth_username=user@gmail.com
auth_password=pass
force_sender=user@gmail.com
– where is my error?
THANKS.

Sanjay Jethva 
- Reply

Hello,
I have mentioned in the post to change the SMTP port which you have missed as it is still 25.
Thank you.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 6/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

karl
- Reply

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 
- Reply

Hello Karl,
Please check the spam folder.
Thanks.

Farmeen 
- Reply

hello sir, i did the above con guration 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 
- Reply

Hey,
The PHPMailer is causing the issue.

Thanks.


Vinay
- Reply

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 
- Reply

Hello Vinay,
Please check if you have any issues from those mentioned at the end of the post.
Thanks.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 7/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Deepa
- Reply

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 
- Reply

Hello Deepa,
Did you check the ve reasons listed at the end of the post?

Marie
- Reply

Hi,

Nice tutorial..!!!

Sanjay Jethva 
- Reply

Thanks!

Amardip

- Reply

 Sir , my localhost xampp server has not found php.ini le


Sanjay Jethva 
- Reply

Hello Amardip,
Please check this – https://drops.meetanshi.com/FTLZ2n
This is also one of the methods.
Thank you.

Robertson
- Reply

set g_smtp_allow_invalid true to permit delivery

Sanjay Jethva 
- Reply

Hello,
Are you using Windows or Mac?
Thank you.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 8/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Samyak
- Reply

Hey!
So I followed all the steps given. It not showing any error but the mail is not being received or sent. I will
con gured the les as per instructions.
Please help!

Sanjay Jethva 
- Reply

Hello Samyak,
You might be facing one of the above listed issues. Please check it once and try again.
Thank you.

wais
- Reply

Thank you sir, it work for me.

Kevin
- Reply

Thanks


Lakshmi Chandrasekar
 - Reply

No error. But mail not received


Sanjay Jethva 
- Reply

Hello,
The issue can be due to any one of the following:
1. The mails are being sent late
2. The mail is not con gured properly
3. The port is blocked
4. Gmail does not have required permissions
5. Password is wrong
Thanks.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 9/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

sanny
- Reply

pls sir are we con guring php.ini distribution or php.ini development……?


tnx for ur support.

Sanjay Jethva 
- Reply

Hello, You have to make changes in the php.ini le.


Thank you.

teja k
- Reply

hi there, I am getting this error …Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS
command rst. z62sm5560709pf b.47 – gsmtp in C:\xampp\htdocs\form2\mail_handler.php on line 13
Something went wrong!

Sanjay Jethva 
- Reply

Hello,
The issue seems to be in SMTP setup.
Please check your account in which you have setup.
 Thank you.


 SAIRA
- Reply

Hy..I have followed all process successfully… all DNE WITHOUT ANY ERROR..bUT NO EMAIL IS SEND BY MAIL
ACCOUNT.

Sanjay Jethva 
- Reply

Hello Saira,

There are multiple reasons for the issue:

1. The mails are being sent late


2. The mail is not con gured properly
3. The port is blocked
4. Gmail does not have required permissions
5. Password is wrong

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 10/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Adeyemi
- Reply

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 
- Reply

Hello,
The issue is in SMTP port. This happens when the port is already in use.
Thank you.

ogenna 
- Reply

Then what do we do in such cases

Sanjay Jethva 
- Reply

Hey,
You need to free up a port that is already being used.
 Thank you.



Pushapak Kumar
- Reply

may I need to paste or edit in that le?

Sanjay Jethva 
- Reply

Hello Pushpak Kumar,


Yes, you’ll need to make changes in that le.
Thank you.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 11/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

byukusenge baraka
- Reply

thanks alot for the help.


but where will i paste this script ??or i will create a new fresh le for it??

Sanjay Jethva 
- Reply

Hello,
Yes, you need to create a new le for it.
Thanks

Gert Kroucamp
- Reply

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 
 - Reply

 Hey, it’s great that my tutorial helped you out. And thanks for the appreciation!


Nik Suki
- Reply

thanks for your guidance!!

Sanjay Jethva 
- Reply

happy to help

Kura
- Reply

Thanks for the tutorial. Worked ne for me.

Sanjay Jethva 
- Reply

Hey! Welcome
Happy to help.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 12/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Villydger Pallarca
- Reply

This is work with CI..good job

Sanjay Jethva 
- Reply

Hey, thanks for the appreciation

panos alexopoulos
- Reply

i did all but still dont work


i have windows with 64bits .. is that a problem ?

Sanjay Jethva 
- Reply

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
 - Reply

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

Sanjay Jethva 
- Reply

Hey Aditya,
It seems that you have made a mistake of a semicolon (;)
Please refer to the above code and try again.
Thank you.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 13/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Tom
- Reply

Super helpful managed to get it working just ne! 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 
- Reply

Hi Tom,
Thanks for the appreciation. Yeah, that’s right, this solution can be used in the local environment.

helloworld
- Reply

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 le and everything is in order, can you help me out? Thanks alot

Sanjay Jethva 
 - Reply

Hi, please re-check your con guration. Any mistake in it can cause the error.
 Thanks.

ajina
- Reply

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 
- Reply

Hello,
Please review your php.ini settings.
Thanks.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 14/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

dshidende
- Reply

This is really helpful, Thank you very much

Sanjay Jethva 
- Reply

Thank you!

padmini
- Reply

Hai,

Email sending failed…


i am getting this error for the code which u have given. please help me out to x this issue.

Sanjay Jethva 
- Reply

Hello,
Please con gure “Less secure apps” settings as shown at
https://support.google.com/cloudidentity/answer/6260879 for proper working as mentioned in the post.
Thanks.

 padmini 
- Reply

i am not getting the less secure option in my gmail account.

Sanjay Jethva 
- Reply

Hi again,
Please follow these steps: https://drops.meetanshi.com/wS6RSe
Thanks.

miheer
- Reply

this code isnt working for me…..it is showing else statement

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 15/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Walid
- Reply

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

Sanjay Jethva 
- Reply

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

vanita saini
- Reply

Thanks for sharing this tutorial

Sanjay Jethva 
- Reply

 Glad to know it helped you Vanita.


 Danushan Kanagasingam
- Reply

can we use this code for realhosting?

Sanjay Jethva 
- Reply

Hi Danushan,
You cannot use it for real hosting. It’s enabled by default in real hosting.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 16/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Harsh Patel
- Reply

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

Sanjay Jethva 
- Reply

Hello Harsh,
Please con gure “Less secure apps” settings as shown at
https://support.google.com/cloudidentity/answer/6260879 to resolve your issue.
Thanks.

miltons
- Reply

This was really helpful, thanx.

Jeff
- Reply

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.


Sanjay Jethva 
- Reply

Hello,
The problem can be with PC settings or the version of XAMPP.
Thanks.

rajarata
- Reply

throw this error


Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\mailtest.php on line 8

Sanjay Jethva 
- Reply

Hello,
This may happen if you have forgotten to place a “;” at line 8 in the script to send mail.
Thanks.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 17/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Brandon Giampa
- Reply

This still isn’t working for me. I get a success message each time, but no email.

Sanjay Jethva 
- Reply

Hello Brandon,
Please con gure “Less secure apps” settings as shown at
https://support.google.com/cloudidentity/answer/6260879 to resolve your issue.
Thanks.

Do Some New
- Reply

Thanks Sanjay Sir. For this code I will always grateful to you.

Sanjay Jethva 
- Reply

Happy to help

 rishabh
- Reply
 Thank you for making this look simple, amazing tutorial


Sanjay Jethva 
- Reply

Thanks for the appreciation

Kalpesh
- Reply

Pretty amazing tutorial

Sanjay Jethva 
- Reply

Thank you so much Kalpesh for the appreciation!

Leave a reply:
Your email address will not be published.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 18/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

Name

Email

Website

Save my name, email, and website in this browser for the next time I comment.

Post Comment


     

HQ
 Bhavnagar
303, Shiv Elite, Madhav
Baug, Waghawadi Rd,
opp. Poojara Telecom,
Bhavnagar, Gujarat.

 +91 7202 997997

 Ahmedabad
Branch
411, Arista Business
Space, Sindhu Bhavan
Marg, Bodakdev,
Ahmedabad, Gujarat.

Custom
er
Service

 About
 Contact
Us

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 19/20
12/22/2020 How To Send Mail From Localhost XAMPP Using Gmail

 Help
Desk
 Partner
Program

Quick
Links

 Career Hiring

 Free
Magento
Extensions
 Best Magent
o Hosting
 Best Magent
o 2 Themes
 Magento
Upgrade
Service
 Magento 2
Migration
 Service


Client's Love 

© 2017-2020 Meetanshi - Magento Extensions Store. All Rights Reserved.

https://meetanshi.com/blog/send-mail-from-localhost-xampp-using-gmail/ 20/20

You might also like