Sending e-mail from localhost in PHP in Windows Environment


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_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(“”,”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:’ . “\r\n”;
mail(“”,”test subject”,”test body”,$headers);

Well that’s all, the mail is sent to “” from the localhost.

Note : Some smtp server verifies the email address of the sender so the email address which is in the place of “” should be a valid and existing email address otherwise mail might not be sent to the “”.

If you have any question or feedback then write at bhattarairoshan at yahoo dot com.

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

105 Comments on “Sending e-mail from localhost in PHP in Windows Environment”

  • Siyab wrote on 27 December, 2007, 6:24

    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(! ]

  • Anthony wrote on 6 January, 2008, 17:27

    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 =

    I uncommented it and it worked without the error although doing step 5/ has the same effect.

  • sriram wrote on 23 January, 2008, 2:46

    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

  • Roshan wrote on 23 January, 2008, 6:42

    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.

  • jishi wrote on 30 January, 2008, 4:36

    how to send email automatically by setting a particular time in windows by using php

  • Roshan wrote on 30 January, 2008, 6:05

    In Linux you can use the cron-tab to execute a file in a particular time you can look at this post

    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.

  • agentcris wrote on 6 April, 2008, 16:06


    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

  • Roshan wrote on 6 April, 2008, 16:47

    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…

  • Ali wrote on 5 May, 2008, 11:39

    I have a problem that my php file does work on xampp but not in wamp……why ?

  • Roshan wrote on 5 May, 2008, 16:44

    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?

  • Sam wrote on 10 May, 2008, 18:34

    Can I use the gmail gateway at port 25. But where do i specify the user id and password. Something similar to what Windows Mail uses.

  • Roshan wrote on 11 May, 2008, 5:22

    I don’t think you can use the using this tips…better ask with your ISP provider what is their smtp server name to send email

  • Sam wrote on 11 May, 2008, 13:55

    I am a little confused, where do you specify the user id and password for the smtp server.

  • Roshan wrote on 11 May, 2008, 18:01

    In this step 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

  • D. Baishya wrote on 15 May, 2008, 13:14

    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.

  • Roshan wrote on 15 May, 2008, 18:12

    thanks for a such a lovely comment D.Baishya…you’ll be the first one to donate for this blog if you do so…

  • Yoosha wrote on 25 May, 2008, 15:04

    Thats not work!
    Error on host & port(Local)!

  • Roshan wrote on 25 May, 2008, 15:11

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

  • Shah Khan wrote on 26 May, 2008, 18:08

    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


  • Roshan wrote on 27 May, 2008, 5:06

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

  • Vladimir wrote on 1 June, 2008, 16:41

    Use Denwer. It use local folder to save sended mail. I remove my xampp and now I user Denwer!

  • Roshan wrote on 2 June, 2008, 4:31

    oh…..I didn’t know about devwer..have to check that out…

  • Darren Azzopardi wrote on 17 June, 2008, 15:25

    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! :)

  • Roshan wrote on 17 June, 2008, 16:08

    Hey Darren…Thanks a lot for motivation…which surely boost up to write more and more tutorial in this blog…

  • Bob Prouty wrote on 25 June, 2008, 0:21

    My ISP requires a username and password. How can I set this?

  • Shyam Timalsina wrote on 29 June, 2008, 3:11


    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

    Thanks again!

  • Praveen Gupta wrote on 11 July, 2008, 12:28


    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

  • erwin wrote on 19 July, 2008, 2:20

    do i still have to set up my microsoft outlook express? because it always pop when i click send.

  • Roshan wrote on 19 July, 2008, 4:49

    @erwin – no you don’t havwe to set up any thing in microsoft outlook express, it fairly simple technique from PHP

  • erwin wrote on 19 July, 2008, 5:18

    thanks for the reply…but how come outlook express always pop up when i click send…by the way im using WAMPserver…

  • ratna wrote on 15 August, 2008, 7:02 is completely php coded
    to check message of friends we need to refresh
    how to make it automatic
    mail me

  • Roshan wrote on 15 August, 2008, 15:21

    @ratna – I don’t think provide smtp facility for free web hosting account..

  • Avirup wrote on 15 September, 2008, 19:04


    At the PHP.ini file i have set SMTP as
    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

  • Roshan wrote on 16 September, 2008, 5:20

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

  • Pradeep wrote on 22 September, 2008, 10:24

    I have even tried the second code ie using header but following error occurs. Please help me.

    Parse error: parse error, unexpected ‘:’ in C:\Program Files\xampp\htdocs\Mountain Biking Nepal on server\Contact.php on line 75

  • Roshan wrote on 23 September, 2008, 6:14

    @pradeep….it is a parser error man….not smtp error . try to make the proper string…

  • Pradeep wrote on 23 September, 2008, 6:28

    This is the code i tried and i placed my email id instead of and

    And the Error comes like:

    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.

  • Pradeep wrote on 23 September, 2008, 6:55

    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.


  • Pradeep wrote on 23 September, 2008, 7:01

    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 works after i upload my website in the server which is not related with NTC.

    Hoping for you assistant again.

    Best Regards

  • Simerjit wrote on 23 September, 2008, 15:57

    Thanks alot buddy…it realy helped me alot..and now i can send emails from localhost..

  • chelsey wrote on 29 September, 2008, 14:36

    you are a STAR!!! thanks, perfect tutorial..up and running within 15 mins. best site I have found for this info, thanks again!!

  • swellbow wrote on 6 October, 2008, 22:34


  • Chathura wrote on 9 October, 2008, 4:13

    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

  • Chathura wrote on 9 October, 2008, 4:30

    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

  • xfree wrote on 5 November, 2008, 18:31

    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.

  • Rakesh Saini wrote on 15 November, 2008, 8:58

    i wan t to set a schudle in wamp server, how i do this
    like send mails to reminder to staff as job schudle

  • Roshan wrote on 15 November, 2008, 13:28

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

  • chaminda wrote on 24 December, 2008, 6:01

    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


  • Daniel wrote on 15 January, 2009, 19:51

    For everyone requiring SMTP authentification.. Check out this article:

    I had to download the, “socket”, “SMTP” and “Mail” PEAR packages, but now it works, and was fairly simple.

    Thanks for the tips, btw.

  • vasco wrote on 17 January, 2009, 17:09

    Very good!!

  • neil wrote on 11 February, 2009, 2:08

    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?

  • samit wrote on 17 February, 2009, 7:34

    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

  • Unni wrote on 18 February, 2009, 7:03

    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…

  • poldek wrote on 5 March, 2009, 20:35

    It’s works… thx :)

  • sarith wrote on 26 March, 2009, 13:40

    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 do i test it locally in my home pc and do u know abt database management for email clients

  • sai wrote on 1 April, 2009, 23:57

    if my internet connection is via proxy what should i keep as smtp server name..

  • Bilal Sammour wrote on 11 May, 2009, 8:17

    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

  • suresh wrote on 14 May, 2009, 6:35

    i want to send a email from local host using php in Linux can you help me please?

  • bhagwan dass wrote on 26 May, 2009, 9:01

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

  • bhagwan dass wrote on 26 May, 2009, 9:03

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

  • geran wrote on 14 June, 2009, 6:48

    tnx,,, its really working, i didnt use XAMPP, or any of them,
    just an ordinary server i used, apache and php…..

  • Mads wrote on 9 July, 2009, 0:22

    Quick and easy fix, thanks.

  • fred Nwauzor wrote on 13 July, 2009, 8:42

    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.

  • Derek wrote on 22 July, 2009, 9:23

    this solved my problem thanks


  • marjune wrote on 12 August, 2009, 9:02

    i follow all instructions but still i got this Warning: mail() [function.mail]: Failed to connect to mailserver at “” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set()

    pls help

  • wira wrote on 23 August, 2009, 5:25

    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???

  • Kyle wrote on 26 August, 2009, 18:34

    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.
    ini_set(‘SMTP’, ‘’);
    mail(to, subject, message);

  • Jewel wrote on 2 September, 2009, 6:17

    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_port = 25

    ; For Win32 only.
    ;sendmail_from =

  • Elvis wrote on 10 September, 2009, 14:26

    SMTP server response: 550 relaying denied
    what’s wrong?

  • anirudha wrote on 14 September, 2009, 15:36

    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
    if you give me some ebook then thank you

  • Ashish wrote on 16 September, 2009, 10:10

    Hello Roshan,

    I am insing XAMPP, i set the SMTP =, 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

  • free style wrote on 17 September, 2009, 5:23

    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

  • vasanthan wrote on 20 September, 2009, 20:36

    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

  • meghraj wrote on 27 September, 2009, 6:54

    i am using wi fi at my hostel & i want to test this mail() so how can i find my ISP name??

  • satyadeep wrote on 1 October, 2009, 21:56

    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

  • Rajesh wrote on 2 October, 2009, 21:33

    The best practice for test environment windows platform would be using ini_set() within your PHP script:

  • vlad wrote on 10 October, 2009, 5:45

    You also have to set sendmail_from = property when setting your smtp staff in mail function in php.ini file, at list on wamp you have to do that.

  • Sumit Joshi wrote on 14 October, 2009, 13:24

    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.

  • habiba wrote on 19 October, 2009, 5:58

    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 :-)


  • Bimla Burlakoti wrote on 22 October, 2009, 4:33

    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

  • muzafar wrote on 24 October, 2009, 9:33

    hanks for this post, budddy!

  • Sofian Oktavianus wrote on 3 November, 2009, 9:35

    i’m using WAMP and it success to send.. thank you…

  • WhydoIneedaname? wrote on 16 November, 2009, 15:18

    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.

  • Kohake wrote on 17 November, 2009, 9:06

    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.

  • Mc-S wrote on 4 December, 2009, 22:44

    Great, this solved exactly the problems I had.

  • nphp101 wrote on 18 December, 2009, 8:32

    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

  • Hardik wrote on 23 December, 2009, 12:40

    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…


  • Dravalley Support Community wrote on 30 December, 2009, 21:20

    oh this is cool. nice…keep it up!

  • Aditya wrote on 3 January, 2010, 19:09

    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 “” 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.

  • Sashi wrote on 5 January, 2010, 7:30

    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.

  • tulip wrote on 6 January, 2010, 17:48


    I am getting this error, any help would be appreciated.

    Warning: mail() [function.mail]: SMTP server response: 550 must be authenticated

    Thanks in adva

  • webb wrote on 21 January, 2010, 23:17

    I didn’t know it would be so easy. Thanks.

  • Akash wrote on 18 February, 2010, 14:28

    Thnx a lot!!!

  • Dodi Rahmaninoff wrote on 19 February, 2010, 21:00

    thanks for the info, been searching all these times for this kind of article

  • Brady Vitrano wrote on 20 February, 2010, 4:02

    Did a Google search for “xampp email setup” and found the solution first try.

    Many Thanks!

  • Fabio wrote on 24 February, 2010, 12:15

    I´m using a WAMP Server in Windows XP e the tip helped me a lot.

  • Mohammed Cherkaoui wrote on 25 February, 2010, 12:40

    thank you, i will try it

  • Rahul wrote on 18 March, 2010, 10:26

    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…

  • webspots wrote on 5 April, 2010, 9:05

    thank for this info…

  • vsree wrote on 22 April, 2010, 4:47

    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.

  • mblue wrote on 2 May, 2010, 11:30

    dude ,
    $headers = ‘MIME-Version: 1.0? . “\r\n”;
    $headers .= ‘Content-type: text/html; charset=iso-8859-1? . “\r\n”;
    $headers .= ‘From:’ . “\r\n”;
    mail(“”,”test subject”,”test body”,$headers);
    this gives me an error in dream weaver :(


  1. Sending email through your local host through WAMP or XAMPP
  2. Sending email through your localhost using WAMP or XAMPP. | Menekali
  3. Sending email from localhost using PHP in XAMPP,WAMP,PHPDEV in Windows
  4. Mail Senden
Copyright © 2015 Roshan Bhattarai's Blog. All rights reserved.
Powered by, Custom Theme and Calling Card Company.