May 13, 2013

Ketan Patel

Sending Mail from Localhost

Sending Mail from Localhost


For this solution you requires sendmail.exe (Its a CLI executable  which accepts email from PHP, connects to an SMTP server and sends email). Download the sendmail.zip and follow the steps given below:

Step:1 Configuration of sendmail file

  • First of all create a folder named “sendmail” in “C:\wamp\”.
  • Now extract these 4 files: “sendmail.exe”, “libeay32.dll”, “ssleay32.dll” and “sendmail.ini” in the newly created folder.
  • Open the “sendmail.ini” file and you have to 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


Here, no need to specify values for these properties:pop3_server, pop3_username, pop3_password, force_sender, force_recipient.


Step:2 Gmail Account Setting

Now what you need to do is enable IMAP Access in your Gmail.In order to do this Go to : Gmail’s Settings -> Forwarding and POP/IMAP -> IMAP Access & enable it.


Imap setting

Step:3 Localhost Server Setting

Enable the “ssl_module” module in Apache server:

sending mail from localhost


Enable “php_openssl” and “php_sockets” extensions for PHP compiler:

sending mail from localhost

Step:4 Configuration of php.ini

Now open php.ini from “C:\wamp\bin\apache\Apache{your version}\bin” and configure it .Now see the last line in the following code, remove prefix semicolon (;) from that line and provide the correct path.

[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.  Arguments are also allowed.default will be : "sendmail -t -i".
; http://php.net/sendmail-path
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t -i"

  • Now Restart WAMP Server.

Step:5  File creation testing

  • Create a PHP file and write the following code in it:
<?php
$to = 'recipient@gmail.com';
$subject = 'Testing sendmail';
$message = 'Hi, you received an email!';
$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 successfully..!!"; 
else
 echo "Email sending failed";?>

  • Next you have to make appropriate changes in $to and $headers variables to set recipient, sender and reply-to address. Save it as “sendmail.php”. (You can save it anywhere or inside any sub-folder in “C:\wamp\www”.)
  • Open this file in browser,and You are DONE..!!

Solutions of Some Known Issues:

  1. You have to disable the 2-steps-verification in Gmail if you are using GMail’s SMTP server to send email, otherwise you might get “Email sent” message but the email won’t get sent actually. (If you don’t know about 2-steps-verification, then don’t need to worry, it is disabled by default.) 
  2. Please make sure that path to the sendmail.exe contains no “space” (for example, “C:\Program Files\PHP\sendmail\”) otherwise Apache would not be able to locate it.
  3. If you are using Windows 8 Operating System,then make the following small change.
change smtp_ssl=ssl
to
smtp_ssl=none
in sendmail.ini and it’s all working on Win 8 too!

Ketan Patel

About Ketan Patel -

I have developed a wide range of websites using CorePHP, Opencart, CakePHP and CodeIgniter including sites for startup companies and small businesses. Apart from my blogging life, I like to read Novels, Listening music and Net surfing.

Subscribe to this Blog via Email :