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.

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% [?]

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

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”

  • 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(www.puttingblogsfirst.com)! ]

  • 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 = me@example.com

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

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

    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

  • 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 smtp.gmail.com 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 smtp.gmail.com 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(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

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

  • 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

    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!

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

    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

  • 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

    rat32chat.freehostia.com 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 freehostia.com provide smtp facility for free web hosting account..

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

    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

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

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

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

    Regards
    Pradeep

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

  • 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

    thanks!

  • 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

    thanx!

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

    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.

  • 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 localmachine.how 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

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

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

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

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

Trackbacks

  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

Write a Comment

 


Copyright © 2009 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.