Category Archives: Uncategorized - Page 8

Send email from localhost/WAMP Server using sendmail

Please try the previous solution [PHPMailer] first to send email from localhost. It is much more efficient.

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 “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: [your_gmail_account_username]@gmail.com' . "\r\n" .
                'MIME-Version: 1.0' . "\r\n" .
                'Content-type: text/html; charset=utf-8';
    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 and sender (“From” header) addresses. 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 🙂
Update (2-Dec-2012)
Please ensure that 2-steps-verification is disabled in GMail if you are using GMail’s SMTP server to send email (sending email using GMail), otherwise you might get “Email sent” message but the email won’t get sent actually. (If you don’t know about 2-steps-verification, you don’t need to worry about it, it is disabled by default.) Thanks to Naveen.
Update (25-Mar-2013)
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"). Thanks to Douglas.
Update (3-Apr-2013)
Windows 8 users, please check Mark’s comment if email sending is failing (may be because of …
Socket Error # 10060
Connection timed out
).
Update (3-Apr-2013)
I think all possible errors are now covered in the comments section, so I am closing the posting of comments for this article. If you are getting any error or a message like “Email sending failed.”, you can send email to me at nikunjbhatt84@gmail.com. Uncomment the “debug_logfile” and “error_logfile” settings and specify “debug.log” and “error.log” (without quotes) respectively for these 2 settings. Then run the send-mail.php script in browser; if there is any problem/error, the error.log will get created in the “sendmail” folder. Please attach those 2 files as well as, php.ini (C:\wamp\bin\apache\ApacheX.Y.Z\bin\), sendmail.ini, and send-mail.php, along with the email; I will check all these files and reply with suggestion if any. Thanks you all for the overwhelming responses.
Update (22-Jun-2013)
sendmail.exe may not work on slow internet connection like dial-up and 2G/GPRS. So you should try this solution with broadband or 3G or any other faster internet connection. I tested this solution with Tata Docomo 2G SIM into Motorola Milestone XT800 as well as into a 3G dongle, but email wasn’t sending. The error logged into the sendmail’s error.log file was: Socket Error # 10060<EOL>Connection timed out. All worked fine after I disconnected Tata Docomo 2G and tried with BSNL Broadband and Idea 3G (on the same dongle). So, I assume Tata Docomo 2G/GPRS, BSNL Dial-up, Airtel GPRS etc. slow connections would not work when using sendmail.exe, may be because of inability of synchronization of communication between sendmail.exe and the mail server on slow internet connection.
Update (06-Dec-2013)
For Windows 8 users:
From Shridharan Perumal:

Few months ago, using your tutorial, I configured mail server in WAMP succesfully. I reinstalled WAMP and tried the same and did as mentioned in the comments area but it didn’t worked (Socket Error # 10060). When Googled, I didn’t got any proper info to solve this. At last, I think I found a reason for this, this is due to Google’s modification. Configuring Stunnel is the solution for this. Here is the post that helped me — http://yogeshchaugule.com/blog/2013/configure-sendmail-wamp.
Update (31-Aug-2014)
From Imanuel Gittens:
After a bit of tinkering I finally got it to work. I had to enable less secure apps in my google account settings to get it to work. Just thought I should let you know in case you wanted to add it to your article. This is the link https://www.google.com/settings/security/lesssecureapps.
Thanks. You really helped me out.