How to send emails from localhost (Windows XP Apache/PHP Server)

Sometimes we may need to send emails in our local PHP projects. In order to do that, we may install a E-Mail server, but that could be a little bit tedious. The easiest alternative is using sendmail and the smtp server of any of your mail accounts (gmail, yahoo, hotmail, ETC).

In this example I’m showing how to configure sendmail to make it working with a Gmail account. But as I said you can make it working with any smtp server you have already.

1st step: Download sendmail

Download sendmail and extract all the files in the folder “C:\sendmail\”

2nd step: configure sendmail.ini

Open the file “C:\sendmail\sendmail.ini” and add the following params in the corresponding lines:

smtp_server=smtp.gmail.com
smtp_port=587
auth_username[email protected]
auth_password=your_password
force_sender[email protected]

; configuration for fake sendmail

; if this file doesn't exist, sendmail.exe will look for the settings in
; the registry, under HKLM\Software\Sendmail

[sendmail]

; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory.  (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.

smtp_server=smtp.gmail.com

; smtp port (normally 25)

smtp_port=587

; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify

default_domain=

; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging

error_logfile=error.log

; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging

;debug_logfile=debug.log

; if your smtp server requires authentication, modify the following two lines

[email protected]
auth_password=your_password

; if your smtp server uses pop3 before smtp authentication, modify the 
; following three lines

pop3_server=
pop3_username=
pop3_password=

; to force the sender to always be the following email address, uncomment and
; populate with a valid email address.  this will only affect the "MAIL FROM"
; command, it won't modify the "From: " header of the message content

[email protected]

; sendmail will use your hostname and your default_domain in the ehlo/helo
; smtp greeting.  you can manually set the ehlo/helo name if required

;hostname=

3ns step: Configure php.ini

Open the php.ini, find the following lines and add the path to the sendmail executable file. Don’t forget to restart the Apache server after doing that.

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "C:\sendmail\sendmail.exe -t"

4th step: test it!

Just a little test to check if it’s actually working. Create a PHP script with the following code:

mail(
     '[email protected]',
     'Works!',
     'An email has been generated from your localhost, congratulations!');

Comments 160

    1. Post
      Author
      1. Its not working
        i put all code as you given information stil its give an error.

        Warning: mail() [function.mail]: “sendmail_from” not set in php.ini or custom “From:” header missing in C:\wamp\www\mail\download mail\mailfunction.php on line 2

          1. I think the mail function must have a fourth parameter like this: “From: sample email”. This should solve your issue. The “From:” part is obligatory

      2. HI, I am getting a blank page on executing the script you have given. And ofcourse no mail is sent. What could be possible reason.

    2. I have did all steps but that is not doing work for sending mail by local server please help me… i am using windows 7 ….please any body help me..

    3. All the stuff goes correct the mail() function returns 1( True) but still the mail didn’t reach to it’s address. I have tried multiple addresses also, but still it not sending mail(). Program runs fine but mail didn’t reach to address.

  1. OMG. This worked immediately for me. I’ve spent most of the day searching for the seemingly simple solution of having a working Apache/PHP/Windows 7 smtp solution, just for development purposes. A ton of false leads until this. Perfect, thanks!!
    -Buck

  2. Pingback: How to send SMTP mails with Postfix (Mac OS X 10.8) | Zend Developer

    1. Post
      Author

      There is no need to change the port in the php.ini, cause sendmail will take care of sending the mail. Have you followed all steps? It’s as easy as it seems. Are you getting any PHP/Apache error?

    2. I was having the same problem. My problem was that I changed the “sendmail_path” parameter on the PHP.INI file inside the PHP folder, but you must change the one inside the APACHE folder. I suppose you already solved this, but to whoever read this, look for the rigth PHP.INI file.

  3. i am using Wamp Server and
    i followed all the steps but i get following msg-

    ( ! ) 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() in C:\wamp\www\mail.php on line 5
    Call Stack
    # Time Memory Function Location
    1 0.0009 667288 {main}( ) ..\mail.php:0
    2 0.0010 667648 mail ( ) ..\mail.php:5

    so please help me for sending mail from localhost.
    i shall be highly obliged.

    1. Post
      Author

      I just recommend you to try it again reading carefully all the steps. You are getting an error on the default port 25. That’s probably because you forgot to update your php.ini setting to the sendmail_path = “C:\sendmail\sendmail.exe -t”, don’t forget to restart the apache server afterwards. If that’s not the problem remember that for Gmail you have to use the following port: smtp_port=587 in the
      file “C:\sendmail\sendmail.ini”

  4. i have done all these 4 steps then i have tested it…
    but

    unfortunately i have not receive mail into my email account….although there is not any kind of error there…

    i am too much confused …. kindly someone help me.

  5. i am using linux and want to setup smtp settings but in apache config files or php.ini files it just defines path to the sendmail but no smtp settings what file shall i edit on linux for this isit php.ini only or other files please elaborate a little thanks for your help

  6. Hi Ander, I was able to send the mail with the code provided by you at my home,but with the same code I could not send the mail at office network .Receiving no error on the page.
    Kindly clarify….Thanks in advance…

    1. Post
      Author

      You are not receiving any error? Humm… I would say that either it worked (but the mail was marked as spam/junk) or the “display_errors” is set to off in your php.ini

  7. I’m using Centos 6.3 and I followed all the above steps, I can send mails from the command line using sendmail but using the PHP I didn’t get any errors and no mails are sent?
    I have checked the spam folder also.
    and the display_errors in PHP is set to ON .
    Any help please ???

    1. Post
      Author

      Well you shouldn’t follow these steps if you are using Linux. This tutorial is about “how to send mails from windows”. There is plenty tutorials about how to make it work in linux, and probably it is even easier (cause linux is awesome)

  8. Hi Ander, I configured port settings to my outlook with the code provide by you.After running the script , the script tries to run for 30 secs and at the end I receive the error “Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\register.php on line 43”. But after 5 mins the receiver gets the mail successfully. …Kindly clarify… Thanks

  9. Well firstly thanks to u ANDER to posting such a help full post for us. 🙂 I would like to know that how can i send mail with file attachment by browsing from local disk. and also want to know that how to send one mail to multiple receiver. please help me.

    1. Post
      Author

      You can send mails to multiple recipients using the default mail() function:
      http://www.php.net/manual/en/function.mail.php

      Attaching files is a little bit more tricky. You will have to modify the headers manually. But you can use some libraries already defined like phpmailer:
      http://phpmailer.worxware.com/index.php?pg=sf&p=sfp

      If you are using zend framework, you can use my own implementation here:
      http://www.zenddeveloper.com/inserting-images-in-email-with-zend-framework/

  10. am using phpmailer class and when I try to send it displays error message “failed The following From address failed: [email protected]” when I remove the from $mail->From=$email; line it displays “failed The following From address failed: root@localhost” but I did the above steps. so what I have to do? But when I install argosoft mail server and I try to send to email account I created on the argosoft, I can .

  11. That good posting. Thank you I spend much time to find the solution now It can be work .Easy and simple configuration.

  12. hi,

    i am using smtp mail server ip address . every thing seems well but i haven’t got the mail to my mail box.

    i have done all the steps. need help ????

  13. Pingback: چگونه از لوکال هاست ایمیل ارسال کنم؟ | وبسایت علی زاهدی نژاد

  14. I’m not able to get this to work and I receive the following Exception message:

    exception message : I/O error 105.

    I have restarted Apache, Rebooted, Set user permissions on c:/sendmail/ and all the files within this directory.

    I have followed these steps exactly modifying what I need in my .ini files yet no luck 🙁

    Any suggestions to fix this welcome…I have used google for the past 4 hours and could not find a solution 🙁

  15. nyc one
    both seriously i keep getting dis same error

    ( ! ) 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() in C:\wamp\www\COSC400\sendmail\send.php on line 13
    Call Stack
    # Time Memory Function Location
    1 0.0006 363624 {main}( ) ..\send.php:0
    2 0.0007 363880 mail ( ) ..\send.php:13″
    and tired of doing tis same tin for almost a week help me out plsssssssssssssssss

    1. Post
      Author

      Are you sure you are updating the right php.ini?
      Have you Clicked on Wamp Icon -> PHP -> php.ini ?
      Have you tried disabling all firewalls/antivirus?
      Are you using PHP version 5.2 or greater?

      1. ( ! ) 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() in C:\wamp\www\COSC400\sendmail\send.php on line 13
        Call Stack
        # Time Memory Function Location
        1 0.0006 363624 {main}( ) ..\send.php:0
        2 0.0007 363880 mail ( ) ..\send.php:13″

        im getting these errors..please help me sir

  16. hi, that’s working with me but when i open the e mail that i wanted to send to i didn’n find any thing .
    can any body help me pleassssssssssssse
    thancks.

  17. help please i have a project that i have to introduce it by wednesday ander ,if you have any answer that can solve my problem please help me.

    1. Post
      Author
  18. yes after configurating the sendmail i tried to send one but i don’t understand what means this ‘generated from my localhost’

    1. Post
      Author

      So it’s working then… Lolitta, The function mail() is using 3 params:
      The email address: This is who will receive the email.
      The subject: The topic of the email.
      The mail body: The content of the email.

      In the previous test you just sent a mail to your email address with the subject “Works!” and the content of the email was “An email has been generated from your localhost”. This is just text, doesn’t really matter what are you putting there. So you will have to replace the subject and body messages depending what you need to send.

    1. Post
      Author
  19. I am using XAMPP 1.7.4 Version. Using gmail account i want send mails. i followed your tutorials but its not working please help me

  20. hi..!
    i am got this Waring message while i am executing
    pls help me………….!
    [31-May-2013 11:34:56] PHP 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() in C:\wamp\www\Mysites\mail.php on line 10

  21. i put all code but still error

    Warning: mail() [function.mail]: “sendmail_from” not set in php.ini or custom “From:” header missing in C:\wamp\www\mail\download mail\mailfunction.php on line 2

  22. hi, i try this code and .exe, i do all the instruction that have been said but suddenly i don’t even received any email to my mail.
    my wamp version is 2.2, my php version is 5.3.13.

    do you think what is the problem.?

    thanks.

  23. Warning: mail() [function.mail]: SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. qp10sm50694721pab.13 – gsmtp in

    Please Help

  24. I try this on windows 8 Pro, but it didn’t work..
    ————————————– Set the php.ini —————————————————
    ; For Unix only. You may supply arguments as well (default: “sendmail -t -i”).
    ; http://php.net/sendmail-path
    sendmail_path = “C:\sendmail\sendmail.exe -t”
    ————————————————————————————————————–
    i create a simple php mail, here’s the code:
    ( I set the sendmail configuration on the php),

    ———————————————————————————————–

    It doesn’t show any error message, but my [email protected] not receive the mail..

    Could you help me?

  25. ini_set(‘SMTP’,’smtp.gmail.com’);
    ini_set(‘smtp_port’,’587′);
    ini_set(‘sendmail_from’,’[email protected]’);
    ini_set(‘auth_username’,’[email protected]’);
    ini_set(‘auth_password’,’mypasswordstuff’);

    $headers = “From:[email protected]\r\n” .
    ‘X-Mailer: PHP/’ . phpversion() . “\r\n” .
    “MIME-Version: 1.0\r\n” .
    “Content-Type: text/html; charset=utf-8\r\n” .
    “Content-Transfer-Encoding: 8bit\r\n\r\n”;

    mail(“[email protected]”, “testing authentication”, “hello world”, $headers);

  26. One question. In step 3: configureing php.ini file..
    the comment in my php.ini on xampp/windows 7 reads that you need to set sendmail_path variableo nly on Unix OS as it does in your comment to. For Windows OS there are 3 different parameters that needs to be set as I found via googling. They are:
    SMPT
    smpt_port
    sendmail_from.

    Can you explain how to configure them, as i am not able to succeed with it after repeated attempts.

    Thanks.

  27. Apparently it worked for me, but the mail did not arrive at the destination!

    var_dump (mail (’[email protected]’, ‘Works!’, ‘An email has been generated from your localhost, congratulations!’));

    The above code will return true, but the E-mail is not arriving!

    I’m using xampp on windows 7

      1. when mail function returns true then it does’t guarantee that your email will arrive. you need to check your configurations again and also check your gmail account settings.

  28. hey friend …its not working for me…no eror wen executing file but email is not delivering….it immediately shows not delivered…pls help me……

  29. same here.. php mail() function is executing. but the mail is not getting delivered.
    Do I need to add tsl/ssl before smtp.google.com ?

    Please help.

  30. Post
    Author

    Have you checked your “Sent” folder in your gmail account? just to be sure nothing has been sent. If there was some problem sending the mail you can have a look at the file “sendmail.log” in the sendmail folder. It will show if there were issues when sending the mail.

  31. Hi,

    Thanks for your tutorial. but i have a question that can we send email from two accounts ? like if we add two accounts in sendemail.ini file and decide in our code that from which we want to send an email? or we have to override php mail function to accomplish this? Thanks

    1. Post
      Author

      As a quick solution, you can move the sendmail folder to your PHP project and create the file sendmail.ini with PHP. This way you can create the configuration on the fly.

    2. Post
      Author
  32. Warning: mail() [function.mail]: “sendmail_from” not set in php.ini or custom “From:” header missing in C:\xampp\htdocs\main\mail.php on line 7

    i need help

  33. Respected sir ,
    I have done all settings step by step according to this tutorial but i receiving same error as before setting please help me how can i fix this error. I’ ll be very thankful to you.
    this is my error (
    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() in E:\xampp\htdocs\forgotpassword\forgotpassword.php on line 51
    )

    1. Post
      Author
  34. Thanks a lot…Btw for those who are not getting any error and not receiveing the mail disable your anti virus and firewall.It worked for me that way.

  35. sir i have an error please tell me me that how can i fix it
    Warning: mail() [function.mail]: “sendmail_from” not set in php.ini or custom “From:” header missing in E:\xampp\htdocs\sms\mailme.php on line 5

  36. this is code which i using to test mail

    <?php
    $headers="Received: ".$received."\n";
    $to = "[email protected]";
    $from = "[email protected]";
    ini_set("sendmail_from",$from);
    $subject = "test";
    $content="test";
    $headers.="From:".$from."\n";
    $headers.="MIME-Version:1.0\n";
    $headers.="Content-Type:text/plain;charset=\"ISO-8859-1\"\n";
    $headers.="Content-Transfer-Encoding:quoted-printable\n";
    if(mail($to, $subject, $content, $headers)){
    echo "ok";
    }

    but showing an error

    Notice: Undefined variable: received in E:\xampp\htdocs\sms\mailme.php on line 2

    Warning: mail() [function.mail]: SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. f49sm5316098eec.7 – gsmtp in E:\xampp\htdocs\sms\mailme.php on line 12

  37. It can’t work i got this error

    “Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\vedzen_form\mail_test.php on line 4”

  38. 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() in C:\wamp\www\online_store\mail.php on line 5.

    how to resolve this problem in windows 7.

  39. i got error likk

    Warning: mail() [function.mail]: “sendmail_from” not set in php.ini or custom “From:” header missing in

  40. Hello..

    I tried this code. they execute successfully they didn’t give any error nor send e-mail to my gmail id..

    please need help.

  41. Hello..

    I tried this code. they execute successfully they didn’t give any error nor send e-mail to my gmail id..

    please need help.
    it gives error in C:\sendmail\error.txt

    14/01/27 14:51:46 : Socket Error # 11001Host not found.
    14/01/27 14:52:58 : Socket Error # 11001Host not found.
    14/01/27 14:55:33 : Socket Error # 11001Host not found.
    14/01/27 14:55:38 : Socket Error # 11001Host not found.

  42. Very grateful for this tutorial, just do it tightly everyone because that really works fine, but I am also looking forward to learning about other domains such as “hotmail and yahoo”. Thank You.

  43. sendmail_path = “C:\sendmail\sendmail.exe -t”

    this path is not there in my php.ini file..what to do?and one more thing is that im using windows..so should i update that path?

  44. sendmail_path = “C:\sendmail\sendmail.exe -t”
    I dont have this path in my php.ini file..What to do?
    and another thing is that im using windows.so instead od unix send mail path,should i use windows?

      1. YOU shouldn’t insult someone because of their knowledge issues, Its not about language.
        The question is very clear, Stay away If you don’t know the answer

  45. Hello,
    I have done all settings as per your tutorial. Its not working for me. I can’t get any errors.

    System Used
    1. Windows 8
    2. Xampp 1.7.7 +

    Need Help.

  46. please anyone help me ……………………. i getting these error ……………. code is not working well 4 me……
    “Warning: mail() [function.mail]: SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. st4sm97173363pab.34 – gsmtp in C:\wamp\www\php\mail.php on line 15
    Message could not be sent… “

  47. Ander,
    Nice work! I have spent the last few hours searching for a good solution to do email testing for my local dev environment. I started with PHPMailer which looks like a nice solution. The problem is that it bombed out complaining with SSL issues. I went and tried enabling the SSL in my php.ini – no luck. I then looked at my Apache config and then Apache wouldn’t start. I then started reading all about getting SSL enabled on your Apache site and I was ready to do something kludgy like substitute in file writes for my SendMail calls. I found your post in a last ditch effort. Within 5 minutes I had it working. Awesome.
    Thanks again,
    DaveO

  48. Ok I am lost, and its not working 🙁
    I did everything exactly…am using XAMP server.
    tell me what do i do after creating the script. please help its urgent

  49. I get executed this file..

    I get mail sent.. But in gmail didnt the message.. I configured like above all.. But i didnt get mail.. Please help me. Thank you.

  50. mail(‘[email protected]’, ‘Works!’, ‘An email has been generated from your localhost,Congratulations!’);
    echo ‘mail sent’; I get mail sent.. But in gmail didnt the message.. I configured like above all.. But i didnt get mail.. Please help me. Thank you.

  51. Hi all. I used method a hundreds time, but still I have not good result.
    I followed all the steps. but my mail function returns NO if I set my php.ini and sendmail.ini like above.
    I am using xampp v3.2.1.
    Please let me know where I did mistake?
    sendmail.ini:————————————————————-
    smtp_server=stmp.gmail.com
    smtp_port=587
    auth_username=******************@gmail.com
    auth_password=***************
    force_sender=******************@gmail.com
    php.ini:———————————————————————
    sendmail_path = “\”C:\xampp\sendmail\sendmail.exe\” -t”
    test.php:———————————————————————

  52. // Please specify your Mail Server oR other mail server which you are going to use.(e.g gmail, yahoo)

    ini_set(“SMTP”,”mail.YourDomain.com”);

    // Please specify an SMTP Number 25 and 8889 are valid SMTP Ports.

    ini_set(“smtp_port”,”25″);

  53. I am getting this warning, Warning: mail(): sendmail_from; not set in php.ini or custom ;From:; header missing in C:\wamp\www\mail\check-mail.php on line 10, Please help me to solve this. thanks.

  54. For those who had problems making it work. Make sure that when you create the new PHP file that you make sure to include the PHP declaration tags at the end (they were not on the instructions above). Your step 4 should look like this:

  55. Not working !
    I did all the things and i get no error just see a loading dialouge box which me to wait a moment.
    But i get no email. Neither in inbox nor in spam. 🙁

  56. Excellent tutorial
    i tried the same, but getting in error.log file as “Connection Closed Gracefully”
    Please help me

  57. Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\test-mail\mail.php on line 6

  58. ( ! ) Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. n84sm4225092pfa.45 – gsmtp in C:\wamp\www\test-mail\mail.php on line 19
    Call Stack
    # Time Memory Function Location
    1 0.0005 240056 {main}( ) ..\mail.php:0
    2 0.0005 241184 mail ( ) ..\mail.php:19

  59. I followed every step u hv writttn in here but m getting dis error. Warning: mail() [function.mail]: “sendmail_from” not set in php.ini or custom “From:” header missing in C:\wamp\www\maildemo.php on line 5
    and secondly where should i save dis php code so as to send mail… i have saved dis php code in wamp\www folder..?? suggest me how dis problem can be resolved…

  60. This works quite well, but it’s sending my body as an attachment, and furthermore, I have to allow logins from less secure apps to even receive it. Do you have a working fix for both of these issues?

  61. This post was written in 2012 and 7 years later, it is still extremely useful. I would say this is the meaning of LEGEND!

    Thanks a lot for this solution.

  62. Hello there,
    How to get the name of the sender who is sending the mail after submission on email???
    regards,
    thanks!!!

Leave a Reply to shrinivas Cancel reply

Your email address will not be published. Required fields are marked *