Sending e-mail from localhost in PHP in Windows Environment
- Wednesday, December 26, 2007, 17:22
- how-to, php, tips and technique
- 105 comments
Have you ever been frustrating, why e-mail is not going from the localhost while using XAMPP or WAMP or any other PHP servers in windows environment? well in that situation i think i can help you.In this article i am going to tell you how to send e-mail from localhost in PHP.
1) Open the “php.ini“. You should know where it is located because it depends upon the particular server you’re running.
2) Search for the attribute called “SMTP” in the php.ini file.Generally you can find the line “SMTP=localhost“. change the localhost to the smtp server name of your ISP. And, there is another attribute called “smtp_port” which should be set to 25.I’ve set the following values in my php.ini file.
SMTP = smtp.wlink.com.np
smtp_port = 25
3) Restart the apache server so that PHP modules and attributes will be reloaded.
4) Now try to send the mail using the mail() function ,
mail(“you@yourdomain.com”,”test subject”,”test body”);
you might get the warning like this,
Warning: mail() [function.mail]: “sendmail_from” not set in php.ini or custom “From:” header missing in C:\Program Files\xampp\htdocs\testmail.php on line 1
5) Now specify the following headers and try to send the mail again,
$headers = ‘MIME-Version: 1.0′ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1′ . “\r\n”;
$headers .= ‘From: sender@sender.com’ . “\r\n”;
mail(“you@yourdomain.com”,”test subject”,”test body”,$headers);
Well that’s all, the mail is sent to “you@yourdomain.com” from the localhost.
Note : Some smtp server verifies the email address of the sender so the email address which is in the place of “sender@sender.com” should be a valid and existing email address otherwise mail might not be sent to the “you@yourdomain.com”.
If you have any question or feedback then write at bhattarairoshan at yahoo dot com.



thanks for this post, budddy! I was long freaked out how to send email through my localhost server… now my prob’s solved. Thanks again!
[ btw, thanks for commenting on my blog(www.puttingblogsfirst.com)! ]
Very interesting and worked for me with XAMPP
In my version of XAMPP I had the error in step 4/ then noticed this below the SMTP setting:
; For Win32 only.
;sendmail_from = me@example.com
I uncommented it and it worked without the error although doing step 5/ has the same effect.
thanks for ur help. but, should i need to install a separate SMTP server in my machine. First of all, i want to test the mail() function. Can i do this without internet i.e in the LAN? pls reply
you don’t need to install a separate SMTP server just call your ISP(internet service provider) and ask for the smtp host name and you can not do it without internet connection.
how to send email automatically by setting a particular time in windows by using php
In Linux you can use the cron-tab to execute a file in a particular time you can look at this post
http://roshanbh.com.np/2008/01/how-to-run-a-php-file-automatically-in-a-certain-interval-using-cron-job.html
But in the windows you need to take help from the scheduled task to do it but I’m not pretty sure that it is going to work in windows.
Roshan,
How do i know the SMTP server name of my ISP. Yeah i know this is an absolutely dumb question. But please help me out.. Is it something related to the domain that i should purchase or the provider of my internet? Please help
Oh if you’re purchasing the domain and web space then smtp server comes with the hosting space and if you’re using localhost then contact your ISP, they will give you the name of the smtp server of your ISP..
hope this helps…
I have a problem that my php file does work on xampp but not in wamp……why ?
Are you using the same smtp server’s name in both wamp and xampp ? and are you sure that smtp is set to proper value in php.ini of wamp?
Can I use the gmail gateway smtp.gmail.com at port 25. But where do i specify the user id and password. Something similar to what Windows Mail uses.
I don’t think you can use the smtp.gmail.com using this tips…better ask with your ISP provider what is their smtp server name to send email
I am a little confused, where do you specify the user id and password for the smtp server.
In this step SMTP-AUTH(http://en.wikipedia.org/wiki/SMTP-AUTH)
is not required…if you ask the unauthenticated SMTP server name with your ISP provider they will just provide the name and you don’t need any smtp authentication for this…hope this helps
Sweet Tutorial. This time I really wanted to DIGG/Reddit/Y!Buzz this article(tutorial) and I see nothing click. I think I will be returning again and if it continues to help me expect more than $2 of donation.
thanks for a such a lovely comment D.Baishya…you’ll be the first one to donate for this blog if you do so…
Thats not work!
Error on host & port(Local)!
Did you follow the all the instruction properly ? Did you set the proper smtp server’s name?
In some cases, it takes a bit longer time to send email set the max_execution_time in php.ini to a bit higher..
Please help i got this error:
Warning: mail() [function.mail]: SMTP server response: 553 We do not relay non-local mail, sorry. in E:\xampp\htdocs\pages\contact_us.php on line 8
????
hi khan…..it your ISP which can help you as they are not allowing to send the mail from your localhost, ask them to allow send email from localhost..
Use Denwer. It use local folder to save sended mail. I remove my xampp and now I user Denwer!
oh…..I didn’t know about devwer..have to check that out…
Hi Roshan,
Not many developers can write good tutorials but you break the mold. This was really good, short and straight to the point.
WELL DONE!
Hey Darren…Thanks a lot for motivation…which surely boost up to write more and more tutorial in this blog…
My ISP requires a username and password. How can I set this?
Dhanyabad!!!
Thank you. I was trying to send mail from my local computer in php, apache server. I was having problem with the SMTP server. My isp is NTC and I got the idea to change SMTP setting in php.ini to smtp.ntc.net.np
Thanks again!
Hi,
I am getting this error, any help would be appreciated.
Warning: mail() [function.mail]: SMTP server response: 550 must be authenticated
Thanks in advance.
-Praveen Gupta
do i still have to set up my microsoft outlook express? because it always pop when i click send.
@erwin – no you don’t havwe to set up any thing in microsoft outlook express, it fairly simple technique from PHP
thanks for the reply…but how come outlook express always pop up when i click send…by the way im using WAMPserver…
rat32chat.freehostia.com is completely php coded
to check message of friends we need to refresh
how to make it automatic
mail me
@ratna – I don’t think freehostia.com provide smtp facility for free web hosting account..
hi
At the PHP.ini file i have set SMTP as smra.dataone.in
and the port no is 25.
But in the PHP script I am getting error where I am using gmail id.Coz I dont have my ISP id at all.
Was that wright to use??
Altough I am gettting error still and surely not receiving any sort of mail yet.
The error is
“Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini”
Cod eis seems to be ok…I am using Wamp server 2 ….!!
Anybody plz help me out!!!
Thank you
@Avirup- can you please verify smtp server name once with your ISP and also verify that they allow to use SMTP server without authentication or not…
I have even tried the second code ie using header but following error occurs. Please help me.
Error:
Parse error: parse error, unexpected ‘:’ in C:\Program Files\xampp\htdocs\Mountain Biking Nepal on server\Contact.php on line 75
@pradeep….it is a parser error man….not smtp error . try to make the proper string…
This is the code i tried and i placed my email id instead of you@yourdomain.com and sender@sender.com.
And the Error comes like:
Error:
Parse error: parse error, unexpected ‘:’ in C:\Program Files\xampp\htdocs\Mountain Biking Nepal on server\test.php on line 2
I am very new to php so please help me out and give me the suggestion how to make proper string as i have copied your code exactly. so whats goes wrong with your code.Please help me out.Please Please Please.
I found the mistake and its working now Roshan ji.
And through this blog i want to say the readers don’t blindly copy the code see the syntax too.
My mistake was the string formation. the copied string contains another “” which i replaced manually with mine and its all working now.
Sorry to bother you due to my silly ness. And thank you very much for you nice coding. Hoping for further assistant in future too.
Regards
Pradeep
One more question Roshan ji, as i am making a website and i am using your code to email the form’s information which the visitors will entry and the visitors can be of any part of the world so does the smtp i insert which is now my homes internet provider ie smtp.ntc.net.np works after i upload my website in the server which is not related with NTC.
Hoping for you assistant again.
Best Regards
Pradeep
Thanks alot buddy…it realy helped me alot..and now i can send emails from localhost..
you are a STAR!!! thanks, perfect tutorial..up and running within 15 mins. best site I have found for this info, thanks again!!
thanks!
Warning: mail() [function.mail]: SMTP server response: 554 : Sender address rejected: Access denied in C:\wamp\www\Mail.php on line 10
I tried a lot and they told they are allowing
But i dont know where is the mistake.
It says above error. i put my email addresses for senders and receivers email address to check
Please help me
And if you know how to receive emails what is what i really need. (Sending email also i need. Please check above comment. Still i couldnt find the answer)
Please tell me is this possible?
I need to know
Someone is sending a mail to me. Then that mail should go to the database (senders email, subject, body, attachments seperatly)
Mysql database has fields to get those data (email field, subject field ……)
*Mail should be go to the database without opening the email or email account
*if above statement is not possible help me to some extend
Help me
Thanks, Roshan.
Before now, I used mercury mail sever (with option “Do not permit SMTP relaying non-local mail” unchecked) in XAMPP. It always failed to send mail to non-local address (like yahoo or gmail, etc). This is my php.ini settings:
SMTP = localhost
smtp_port = 25
sendmail_from = admin@localhost
Do you know why I cannot send mail to non-local mail using local mail server?
Is there any possibility that my ISP didn’t permit me to use local mail server?
Please answer me to my email. I’m afraid I won’t often come here for checking my post.
i wan t to set a schudle in wamp server, how i do this
like send mails to reminder to staff as job schudle
@rakesh – I don’t think there are any automated services in windows like Linux have cron jobs…
try scheduled task in windows weather it can call Internet explorer or firefox with applied url or not..
Hi dude!
i have a problem. I configured all the settings and sent some mails. There were displayed successfully sent messages also. But the problem is the mail didn’t go to the particular mail account. I’m using xampp server and noticed that inside of the xampp\MercuryMail\QUEUE folder, it were all the mails that i was sent. So, pls tell me how to send these mails to the my mail account. I’m using yahoo mail. Waiting 4 a soon reply
thanx!
For everyone requiring SMTP authentification.. Check out this article:
http://email.about.com/od/emailprogrammingtips/qt/et073006.htm
I had to download the, “socket”, “SMTP” and “Mail” PEAR packages, but now it works, and was fairly simple.
Thanks for the tips, btw.
Very good!!
sorry because im just a newbie
my question is where can i find the mail() function?
and where can i find the statement number 5 which specifies the header?
hi dude,
i have a problem ,the following error occoured;
Warning: mail() [function.mail]: SMTP server response: 450 This mail is temporarily denied in D:\wamp\www\test\files\rec.php on line 5
Hi Roshan,
Nice post and i think there wasnt another site where i can find this issue explained very well like this…i can see that this issue got solved for everyone…and i too thought that wud work for me as well..but i have a strange with XAMPP, i changed the values of SMTP and checked nothing is working ans still showing erros…i tried to echo ini_get(‘SMTP); and strange, i get the result “localhost”, actually i changed the value in the file xampp/php/php.ini……..which is where the php.ini file shud be there…i renamed the file and tried and still showing the message..this means php.ini file is in some other location???? Also i tried renaming the file in xampp/apache/bin/php.ini….oooooooops..still same issue…strange naaa…plz yaar..if you have any idea plz help me..thanks in advance…
It’s works… thx
hi roshnan thanks for the article it was very helpful.i have another problem related to testing onwamp servers.i m doing a project on creating a email client (webmail) using php.i have finished with the functionality and i m not able o test on localmachine.how do i test it locally in my home pc and do u know abt database management for email clients
if my internet connection is via proxy what should i keep as smtp server name..
thank u for this great lesson
how can i set the sender of email to be any thing as ‘my company’ not the email, as we do using outlook??
thank u
i want to send a email from local host using php in Linux can you help me please?
hi….
i follow all the instruction given by you but brouser show such type of error ” SMTP server response: 530 authentication required (#5.7.1) ”
pls help me……..
hi
i follow all the instruction given by you but code show such type of error ” SMTP server response: 530 authentication required (#5.7.1) pls help me……………
tnx,,, its really working, i didnt use XAMPP, or any of them,
just an ordinary server i used, apache and php…..
Quick and easy fix, thanks.
Thanks for those wonderful post and support…
I just want you to help me on this question.
I have a guest filling form and i want my php code(ie. at my mail() section ) to email me and also email the guest.How do i code the mail() ?
Thanks in advance.
this solved my problem thanks
~belstein
i follow all instructions but still i got this Warning: mail() [function.mail]: Failed to connect to mailserver at “sample.com” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set()
pls help
Warning: mail() [function.mail]: SMTP server response: 450 This mail is temporarily denied in C:\xampp\htdocs\T.php on line 5
what is this mean???
Thanks for the great tip!
I found that you can use the ini_set() function directly inside your php script to achieve the same desired outcome when sending from localhost (I wanted to send from localhost for testing prior to uploading to my web server). Simply include the two lines below in your script directly before the mail() function. I am running WAMP on Windows Vista.
EXAMPLE:
ini_set(‘SMTP’, ‘smtp.cox.net’);
ini_set(‘smtp_port’,25);
mail(to, subject, message);
SMTP Error! Could not connect to SMTP host. When I Create a new user from Localhost. Please help me? i change the line from php.ini
[mail function]
; For Win32 only.
SMTP = smtp.gmail.com
smtp_port = 25
; For Win32 only.
;sendmail_from = jewelewu@gmail.com
SMTP server response: 550 relaying denied
what’s wrong?
hello author of this blog now i am create a independent widget so how can i send email using yahoo or hotmail because google problem is solve so send me answer to
anirudha.rockey@gmail.com
if you give me some ebook then thank you
Hello Roshan,
I am insing XAMPP, i set the SMTP = mail..com, smtp_port = 25 in php.ini file.
but receive an error “SMTP server response: 554 5.7.1 Header forgery attempt – rejected in C:\xampp\htdocs\sendmail.php”
how to resolve it, please help
error : SMTP server response: 553 Sorry, that domain isn’t in my list of allowed rcpthosts. in C:\AppServ\www\test.php on line 5
trying…
how to remove iframe attack in php..some of our clients sites are hacked by this iframe a code insterted below body tag….once it is removed again appeared
i am using wi fi at my hostel & i want to test this mail() so how can i find my ISP name??
hi
my internet connection is through proxy server – and ours is a lan connection
how do i configure the smtp server in such a case- cant i directly use localhost or can i use my ip address in the lan which i am in.
i am using xampp
The best practice for test environment windows platform would be using ini_set() within your PHP script:
ini_set(‘SMTP’,’smtp.domain.tld’)
You also have to set sendmail_from = you@your_isp.com property when setting your smtp staff in mail function in php.ini file, at list on wamp you have to do that.
Hi, Dear Roshan.
I use WAMP server in my local machine. I set the php.ini file as you have given in this tutorial and also tried from other sites. But I am not able to send mail from my local machine to other mail id.
Please help me out.
Thanks in advance.
i have configured my php.ini as per your direction but i got the follwoing warnning.
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()
how can i solve this problme, please help me
regards
Habiba
hello , i want to know the php code for sending data in the data base and in the same time same data to the mail… well i know how to send the data in database and mail function, but problem is only how to use them simultaneously through same submit button
thanks in advance
hanks for this post, budddy!
i’m using WAMP and it success to send.. thank you…
I tried to follow this as closely as possible, and it worked out pretty well. After step 4 I got the warning you mentioned, so I put in the headers and now I’m getting this message:
“Parse error: syntax error, unexpected ‘:’ in C:\AppServ\www\test.php on line 86″
Line 86 being:
“$headers = ‘MIME-Version\: 1.0? . “\r\n”;”
I’m not very good at this, but I’d really appreciate any suggestion about what I might be doing wrong.
I was following this guide, and after step four I got the warning message you said I might get, so I just went on with step five. The result was this:
“Parse error: syntax error, unexpected ‘:’ in C:\AppServ\www\test.php on line 86″
Line 86 contains this: $headers = ‘MIME-Version: 1.0? . “\r\n”;
I don’t know what to do, because removing the “:” only get’s me this error:
Parse error: syntax error, unexpected T_DNUMBER in C:\AppServ\www\test.php on line 86
Please help.
Great, this solved exactly the problems I had.
Thanks!
How can i send email not only for gmail account but to all accounts (yahoo, hotmail etc). Codes below are for gmail account only. This works perfectly at my localhost. Please help me.
yM: nphp101[at] yahoo [dot] c o m
Hey Roshan
I have Wamp installed at my pc and m sending mail through it
i have set smtp port
n everything works fine when i use outlook
otherwise it gives error :
mail() [function.mail]: SMTP server response: 553 sorry, Authentication failed or timed out. Please do get messages first to authenticate yourself.(#4.4.3)
Please help me for the same…
Thanx
oh this is cool. nice…keep it up!
Hi Roshan,
This Aditya started learning php recently so,please help me out with this problem.
Actually i am trying to test email in PHP through wamp server but i am getting the following error.
Warning: mail() [function.mail]: Failed to connect to mailserver at “you@yourdomain.com” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set() in C:\wamp\www\Simplemailprg.php on line 11
Mail failed
for your reference i am giving you the code i am using.
Thanks a lot. It worked perfectly. I had to change the SMTP address in php.ini file of “apache/bin” and “php” folders of XAMPP. Finally i could send a mail from my localhost.
Hi,
I am getting this error, any help would be appreciated.
Warning: mail() [function.mail]: SMTP server response: 550 must be authenticated
Thanks in adva
I didn’t know it would be so easy. Thanks.
Thnx a lot!!!
thanks for the info, been searching all these times for this kind of article
Did a Google search for “xampp email setup” and found the solution first try.
Many Thanks!
I´m using a WAMP Server in Windows XP e the tip helped me a lot.
Thanks.
Hi,
thank you, i will try it
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()
This is the error what i’m getting…
thank for this info…
Thanks for the tutorials. I am new for the PHP. I tried this tutorial for my feedback form in localhost. I got error in log file as ‘Connection reset by peer.’ Please help me what can i do more for that.
dude ,
$headers = ‘MIME-Version: 1.0? . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1? . “\r\n”;
$headers .= ‘From: sender@sender.com’ . “\r\n”;
mail(“you@yourdomain.com”,”test subject”,”test body”,$headers);
this gives me an error in dream weaver