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

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

  1. neil

    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?

  2. samit

    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

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

  4. poldek

    It’s works… thx :)

  5. sarith

    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

  6. sai

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

  7. Bilal Sammour

    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

  8. suresh

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

  9. bhagwan dass

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

  10. bhagwan dass

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

  11. geran

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

  12. Mads

    Quick and easy fix, thanks.

  13. fred Nwauzor

    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.

  14. Derek

    this solved my problem thanks

    ~belstein

  15. marjune

    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

  16. wira

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

  17. Kyle

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

  18. Jewel

    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

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

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

  21. Ashish

    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

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

  23. vasanthan

    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

  24. meghraj

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

  25. satyadeep

    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

  26. Rajesh

    The best practice for test environment windows platform would be using ini_set() within your PHP script:
    ini_set(‘SMTP’,’smtp.domain.tld’)

  27. vlad

    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.

  28. Sumit Joshi

    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.

  29. habiba

    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

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

  31. hanks for this post, budddy!

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

  33. WhydoIneedaname?

    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.

  34. Kohake

    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.

  35. Great, this solved exactly the problems I had.
    Thanks!

  36. nphp101

    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

  37. Hardik

    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

  38. oh this is cool. nice…keep it up!

  39. Aditya

    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.

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

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

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

  43. Akash

    Thnx a lot!!!

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

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

    Many Thanks!

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

  47. Hi,
    thank you, i will try it

  48. Rahul

    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…

  49. thank for this info…

  50. vsree

    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.

Comments are closed.