Sending e-mail from localhost in PHP in Windows Environment
- Wednesday, December 26, 2007, 17:22
- how-to, php, tips and technique
- 64 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.
The wireless internet service providers provide all kinds of the facilities to enjoy the high quality of high speed internet. There is a need of developing the most exclusive website design templates for building more wonderful websites. There is need of getting some skills for installing the wireless routers in the professional manner. The cingular wireless networks are very important in providing the most exclusive services of internet. There are several reliable companies which are main source of wireless internet coverage in all over the world. The networks of voip company is designed to facilitate the customers with internet phone services in the cheap rates.Popularity: 44% [?]
Related Posts
» Cool out of Office Auto Replies
» Some Magical and weired bug in Microsoft Windows
» Few Programming Jokes
» Solving European characters (Western charset) problem with Ajax and PHP
64 Comments on “Sending e-mail from localhost in PHP in Windows Environment”
Trackbacks
- Sending email through your local host through WAMP or XAMPP
- Sending email through your localhost using WAMP or XAMPP. | Menekali
- Sending email from localhost using PHP in XAMPP,WAMP,PHPDEV in Windows






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…..