Thursday, 27 June 2013

Send email from localhost/WAMP Server using sendmail


The new WAMP Server’s PHP and stand-alone PHP v5.3+, are now NOT available with some of the extensions required to implement the previous solution [PHPMailer] (there is a workaround to install the missing extension(s), described in the linked article). So an alternate and much less efficient (slower, but negligible low-speed for development platform (WAMP/localhost)) solution can be implemented, as below:

Solution:
This solution requires sendmail.exe (a Command Line Interface (CLI) executable which accepts email from PHP, connects to an SMTP server and sends email). You will not require to use it by command, don’t bother about it :-) Download the sendmail.zip and follow these steps:
  • Create a folder named “sendmail” in “C:\wamp\”.
  • Extract these 4 files in “sendmail” folder: “sendmail.exe”, “libeay32.dll”, “ssleay32.dll” and “sendmail.ini”.
  • Open the “sendmail.ini” file and configure it as following
    • smtp_server=smtp.gmail.com
    • smtp_port=465
    • smtp_ssl=ssl
    • default_domain=localhost
    • error_logfile=error.log
    • debug_logfile=debug.log
    • auth_username=[your_gmail_account_username]@gmail.com
    • auth_password=[your_gmail_account_password]
    • pop3_server=
    • pop3_username=
    • pop3_password=
    • force_sender=
    • force_recipient=
    • hostname=localhost
    You do not need to specify any value for these properties: pop3_server, pop3_username, pop3_password, force_sender, force_recipient. The error_logfile and debug_logfile settings should be kept blank if you have already sent successful email(s) otherwise size of this file will keep increasing. Enable these log file settings if you don’t get able to send email using sendmail.
  • Enable IMAP Access in your GMail’s Settings -> Forwarding and POP/IMAP -> IMAP Access:
  • Enable “ssl_module” module in Apache server:
  • Enable “php_openssl” and “php_sockets” extensions for PHP compiler:
  • Open php.ini from “C:\wamp\bin\apache\Apache2.2.17\bin” and configure it as following (The php.ini at “C:\wamp\bin\php\php5.3.x” would not work) (You just need to configure the last line in the following code, prefix semicolon (;) against other lines):
    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    ;SMTP =
    ; http://php.net/smtp-port
    ;smtp_port = 25
    
    ; For Win32 only.
    ; http://php.net/sendmail-from
    ;sendmail_from = you@domain.com
    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ; http://php.net/sendmail-path
    sendmail_path = "C:\wamp\sendmail\sendmail.exe -t -i"
  • Restart WAMP Server.
  • Create a PHP file and write the following code in it:
    <?php
    $to       = 'recipient@yahoo.com';
    $subject  = 'Testing sendmail.exe';
    $message  = 'Hi, you just received an email using sendmail!';
    $headers  = 'From: sender@gmail.com' . "\r\n" .
                'Reply-To: sender@gmail.com' . "\r\n" .
                'MIME-Version: 1.0' . "\r\n" .
                'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
    if(mail($to, $subject, $message, $headers))
        echo "Email sent";
    else
        echo "Email sending failed";
    ?>
  • Make appropriate changes in $to and $headers variables to set recipient, sender and reply-to address. Save it as “send-mail.php”. (You can save it anywhere or inside any sub-folder in “C:\wamp\www”.)
  • Open this file in browser, it MUST work now :-)

94 comments:

  1. Failed to connect to mailserver at "" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

    ReplyDelete
  2. Hi Snatosh,

    You need to comment smtp_port = 25 as describe above. please make double sure about it. and restart wamp. it should work :)

    Thanks,
    Arpan

    ReplyDelete
  3. Hi. i followed all the steps.but it is still not working

    ReplyDelete
    Replies
    1. if you are getting Socket Error # 10060Connection timed out
      plz change sendmail.ini from
      smtp_ssl=ssl
      to
      smtp_ssl=none
      and it will work on Win 8 too!!

      Delete
  4. I think it's not related to OS.. It should work.
    Have you facing any error? Plz "Enable these log file settings if you don’t get able to send email using sendmail" as describe in above

    ReplyDelete
  5. hi... i have win8 and get this type of error so what should i do


    Failed to connect to mailserver at "" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

    ReplyDelete
    Replies
    1. Hi Ram,

      I think you should double check following step.Please check it and let me know.

      Open php.ini from “C:\wamp\bin\apache\Apache2.2.17\bin” and configure it as following (The php.ini at “C:\wamp\bin\php\php5.3.x” would not work) (You just need to configure the last line in the following code, prefix semicolon (;) against other lines):
      [mail function]
      ; For Win32 only.
      ; http://php.net/smtp
      ;SMTP =
      ; http://php.net/smtp-port
      ;smtp_port = 25

      ; For Win32 only.
      ; http://php.net/sendmail-from
      ;sendmail_from = you@domain.com
      ; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
      ; http://php.net/sendmail-path
      sendmail_path = "C:\wamp\sendmail\sendmail.exe -t -i"

      Delete
  6. hi the mail script work but i didn't receive any mail

    ReplyDelete
    Replies
    1. Hi yugal,

      Please check in span. if scrip run, email is in your inbox or spam folder

      Thanks,
      Arpan

      Delete
  7. Hello...... Sir please help me to fix it out.

    I have used each and every step which you have been described above but still getting "Email sending failed" error.

    so please help me!!
    Thanks in advance :)

    ReplyDelete
    Replies
    1. Hi Vipin,

      What is your wamp's PHP version?

      Delete
    2. I am also getting the same problem my wamp's PHP version is 5.3.10

      Delete
    3. plz check all steps again..once you done with perfet installation you should get message "Mail Sent".

      Delete
  8. hello Sir,
    PHP version- 5.4.16
    wamp- 2.4

    i had gone through all the above steps, and no error message or warning are showing except "Mail sent"(this message in if part of mail()), but i didn't receive any mail in any folder.

    Kindly share ur ideas
    Thanks

    ReplyDelete
    Replies
    1. Hi Avinash,

      Can u plz let me know your sendmail_path? and what's value of it?

      Delete
  9. my php.ini looks like:
    sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t -i"
    Watch the extra "\ and -i" at the begin and end of the line

    ; sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
    Had to comment this out ie put a ; in front of the line.

    Finally i've got it working, tnx for info

    ReplyDelete
    Replies
    1. Thanks for your comment.. it will help to others..

      Delete
  10. This script is running but i didn't receive any mail ..please help me

    ReplyDelete
    Replies
    1. script is running and no error means working.. plz check your spam folder and plz do debugging and let me know in detail where exactly it stopped executing so i can help you...

      Delete
  11. This comment has been removed by the author.

    ReplyDelete
  12. ( ! ) Warning: 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\sendmail.php on line 10
    Call Stack
    # Time Memory Function Location
    1 0.0010 141144 {main}( ) ..\sendmail.php:0
    2 0.0010 142088 mail ( ) ..\sendmail.php:10

    ReplyDelete
  13. i am using 32 bit window 7 with wamp server of 32 bit

    ReplyDelete
  14. Comment your smtp_port. please verify below code as smtp_port should be commented.
    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    ;SMTP =
    ; http://php.net/smtp-port
    ;smtp_port = 25

    ReplyDelete
    Replies
    1. what we have to do if we want to upload this script online then what will be the change in this script please reply must....

      Delete
    2. same process you should follow...

      Delete
  15. I have run this code . it shows email sent but i didt receive any mail ?

    ReplyDelete
    Replies
    1. Email sent event only fire when everything is fine and email should sent. plz chk in spam folder

      Delete
  16. Any one please Tell me the solution.

    ReplyDelete
    Replies
    1. Email sent event only fire when everything is fine and email should sent. plz chk in spam folder

      Delete
    2. i have checked my spam too i didt get any mail

      Delete
    3. plz chk your error.log file in your sendmail folder. and let me know if you got any error?

      Delete
  17. Thanks, worked like charm...

    ReplyDelete
  18. Hi I am getting this error: Socket Error # 10060Connection timed out. Any suggestion what I need to change?

    ReplyDelete
    Replies
    1. if you are getting Socket Error # 10060Connection timed out
      plz change sendmail.ini from
      smtp_ssl=ssl
      to
      smtp_ssl=none
      and it will work for you..

      Delete
  19. ----DEBUG-----

    14/04/13 00:24:43 ** --- MESSAGE BEGIN ---
    14/04/13 00:24:43 ** To: boxygillett@live.co.uk
    14/04/13 00:24:43 ** Subject: Testing sendmail.exe
    14/04/13 00:24:43 ** X-PHP-Originating-Script: 0:send-mail.php
    14/04/13 00:24:43 ** From: sender@gmail.com
    14/04/13 00:24:43 ** Reply-To: sender@gmail.com
    14/04/13 00:24:43 ** MIME-Version: 1.0
    14/04/13 00:24:43 ** Content-type: text/html; charset=iso-8859-1
    14/04/13 00:24:43 ** X-Mailer: PHP/5.4.12
    14/04/13 00:24:43 **
    14/04/13 00:24:43 ** Hi, you just received an email using sendmail!
    14/04/13 00:24:43 ** --- MESSAGE END ---
    14/04/13 00:24:44 ** Connecting to smtp.gmail.com:465
    14/04/13 00:24:44 ** Disconnected.
    14/04/13 00:24:44 ** Disconnected.
    14/04/13 00:24:44 ** Disconnected.
    14/04/13 00:24:44 ** Socket Error # 10060Connection timed out.

    ----EORROR-----

    14/04/13 00:24:44 : Socket Error # 10060Connection timed out.

    ReplyDelete
    Replies
    1. When I change ssl to none as stated above all my page does have loading symbol going round and round and round nothing happens what so ever

      in the apache php ini and also the php ini I have everything ; out apart from the path which is like this...

      sendmail_path = "C:\wamp\sendmail\sendmail.exe -t -i"

      Other than that everything is exactly how you said above yet a change ssl to ssl and I just get message failed and get the errors above.

      Help please :'(

      Delete
    2. Can you please replace your code :-

      $headers = 'From: sender@gmail.com' . "\r\n" .
      'Reply-To: sender@gmail.com' . "\r\n" .
      'MIME-Version: 1.0' . "\r\n" .
      'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
      'X-Mailer: PHP/' . phpversion();

      With below code.(don't forget to change email)

      $headers = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      // Additional headers
      $headers .= 'From: my website name ' . "\r\n";

      Delete
  20. Sir, i did all the above steps as you have mentioned. but still i get the following 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()"" ......Please help me out..

    ReplyDelete
    Replies
    1. it seems Below code is not commented:-
      [mail function]
      ; For Win32 only.
      ; http://php.net/smtp
      ;SMTP =
      ; http://php.net/smtp-port
      ;smtp_port = 25

      Can you plz comment it as mentioned in above code.

      Delete
    2. i tried this but got same 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\test\send_mail.php on line 10"

      Delete
  21. sir send mail folder and php send file is in same folder ...
    because mail cannot be send msg

    ReplyDelete
  22. hello sir, i am not getting mail spam n inbox

    ReplyDelete
  23. i am not getting error.log file in sendmail.php.........any other solution sir plz

    ReplyDelete
  24. no sir......but not getting mail on gmail

    ReplyDelete
    Replies
    1. Plz check your spam folder and plz do debugging and let me know in detail where exactly it stopped executing so i can help you...
      plz chk your error.log file in your sendmail folder. and let me know if you got any error?

      Delete
  25. This comment has been removed by the author.

    ReplyDelete
  26. Hello Sir please help me to fix it out.

    I followed each and every step which you have mentioned above but still getting am "Email sending failed" error.

    i am using php version 5.5.12

    so please help me!!
    Thanks in advance :)

    ReplyDelete
    Replies
    1. Can you please let me know the error from the error log file? so i can help you...

      Delete
  27. Replies
    1. Can you please let me know the error from the error log file? so i can help you...

      Delete
  28. error occur Email sending failed

    ReplyDelete
    Replies
    1. Can you please let me know the error from the error log file? so i can help you...

      Delete
  29. thnx a lot ..it is a great help for person like me who is a beginner in web development :)

    ReplyDelete
  30. Its working great.. Very Helpful.. I got email from my localserver.. Thanks a lot..

    ReplyDelete
  31. Replies
    1. Thanks for comment about Send email from localhost/WAMP

      Delete
  32. Hi I am getting this error: Socket Error # 10060Connection timed out..plx help

    ReplyDelete
    Replies
    1. Hi Shahzad,

      Please make following change
      In sendmail.ini replace below line from
      smtp_ssl=ssl
      to
      smtp_ssl=none

      If it can't resolve your issue then
      Can you please verify SMTP port in your sendmail.ini file. Also compare the settings again.

      Delete
  33. Good job. It really helps me.

    ReplyDelete
  34. Error On
    Enabling “ssl_module” module in Apache server
    Enabling “php_openssl” and “php_sockets” extensions for PHP compiler
    plzz help

    ReplyDelete
    Replies
    1. I think something went wrong with your configuration
      Please open php.ini manually and make below changes

      Find below line and remove semicolon from above line
      ;extension=php_sockets.dll
      ;extension=php_openssl.dll

      and restart WAMP

      If you still get same error please Once again check all configuration properly and change accordingly

      Delete
  35. Hai,

    am using
    windows 8
    WampServer Version 2.5

    i got the following 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\test\send_mail.php on line 10


    i read previous comments for this issue to change the code in php.ini
    [mail function]
    ; For Win32 only.
    ; http://php.net/smtp
    ;SMTP =
    ; http://php.net/smtp-port
    ;smtp_port = 25

    ; For Win32 only.
    ; http://php.net/sendmail-from
    ;sendmail_from = you@domain.com
    ; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
    ; http://php.net/sendmail-path
    ;sendmail_path = "C:\wamp\sendmail\sendmail.exe -t -i"

    i tried this. even though i got the same error.. plz help me to solve the issue!

    ReplyDelete
  36. Hai,

    Now i just recheck the steps which you said above and restart the wamp. i got the message Email sent. But i did n't get any mail. even i checked my spam folder too, i did't get any mail.

    ReplyDelete
  37. Hello
    Sir..
    Its Not Working...
    ( ! ) 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\mail.php on line 10
    Call Stack
    # Time Memory Function Location
    1 0.0010 365816 {main}( ) ..\mail.php:0
    2 0.0010 366488 mail ( ) ..\mail.php:10
    Email sending failed

    ReplyDelete
  38. Hello,,
    Sir,

    My program doesn't work mail function. It show the error like

    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\yedu\mail.php on line 16

    ...
    plz help what can i do ?

    ReplyDelete
  39. Sir, i am getting error "Connection Closed Gracefully" in error.log file
    what is the problem ?

    ReplyDelete
    Replies
    1. HI,

      Sendmail has to be run as an administrator. Please follow below steps :

      1 Right click on sendmail.exe

      2 Properties

      3 Compatibility

      4 Change the configuration for all users

      5 Execute as Windows XP or whatever you have

      6 Execute as adminitrator

      And save :)

      Delete
  40. Hi. Same error "Connection Closed Gracefully". I am sing Win10 and wamp. In #5, there is no compatiblilyt mode for Win10. and where is #6?

    ReplyDelete
    Replies
    1. When you click on sendmail.exe, you can see an option "Run as administrator". So first you should click on it and then you should follow step 1 to 5. I think OS doesn't matter here.

      Delete
  41. Email sending failed

    ReplyDelete
    Replies
    1. Can you please give me exact error which you are getting?

      Delete
  42. Connection Closed Gracefully.

    ReplyDelete
    Replies
    1. Did you get this message? At which step?

      Delete
  43. i will get a one error that is Warning: mail(): Failed to connect to mailserver at "localhost" port 587, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in

    ReplyDelete
    Replies
    1. Please check your configuration. It's only problem of configurations.

      Delete
  44. Hello Sir,
    Kindly Helps to create a Php mail for sending and receiving is very diificult .
    Pls help for give correct code

    ReplyDelete
    Replies
    1. Can you please give me an more details. what error you are getting? what you have tried?

      Delete
  45. email sending failed

    ReplyDelete
  46. hello sir, i am not getting mail spam n inbox

    this is error.log file
    16-04-27 20:01:06 : Socket Error # 11001Host not found.
    16-04-27 20:01:14 : Socket Error # 11001Host not found.

    ReplyDelete
    Replies
    1. It means either you have given incorrect host name for SMTP server OR your mail server is not discoverable using the host name. So, either you need to specify correct host name OR you can try host’s IP address instead of domain name. These are the most probable reasons but there could be other reasons for this error.

      Delete
  47. i followed same steps but still i m getting error
    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\cind.php on line 8
    Email sending failed

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. it seems Below code is not commented:-
      [mail function]
      ; For Win32 only.
      ; http://php.net/smtp
      ;SMTP =
      ; http://php.net/smtp-port
      ;smtp_port = 25

      Delete
  48. Hello Sir please help me to fix it out.

    I followed each and every step which you have mentioned above but still getting am "Email sent successfully.. but not getting mail on gmail i also check in spam folder..
    plz sir help me.

    i am using php version 5.5.12 and os windows 10

    ReplyDelete
    Replies
    1. If the path to the sendmail.exe contains “space” (for example, “C:\Program Files\PHP\sendmail\”) then PHP would not be able to locate it. So, either store the sendmail.exe at a “non-spaced” location/path OR write the path in DOS style “C:\Progra~1\PHP\sendmail\” in the php.ini (sendmail_path = "C:\Progra~1\PHP\sendmail\sendmail.exe -t -i").

      Delete
  49. thanks was very helpful. what of using yahoomail for the smtp authentication.

    ReplyDelete
  50. I can't get email even there is no error.Help me!!!

    ReplyDelete