Sunday, February 23, 2014

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

Some extra solution :
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.

-------------------------------------------------------------------------------------------------------------------------------------

Another solutio :
you only have to modify 2 ini files: php.ini and sendmail.ini
1)look for mail function in php.ini(c:/xampp/php/php.ini)>>[mail function]
change the following::
SMTP=smtp.gmail.com
smtp_port=587

sendmail_from = from@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
NOTE: ensure that the path you specify for sendmail_oath is valid.In my case it is in C.
SAVE your changes.

2) next modify sendmail.ini(c:/xampp/sendmail/sendmail.ini)
comment the mercury as shown below
# Mercury
#account Mercury
#host localhost
#from postmaster@localhost
#auth off
# A freemail service example
#account Hotmail
#tls on
#tls_certcheck off
#host smtp.live.com
#from [exampleuser]@hotmail.com
#auth on
#user [exampleuser]@hotmail.com
#password [examplepassword]
then paste the following lines:
account Gmail
tls on
tls_certcheck off
host smtp.gmail.com
from x@gmail.com
auth on
user x@gmail.com
password x
port 587
# Set a default account
account default : Gmail
Again SAVE your changes.
Use the code below to test if it's working!
<?php
$subject="Test mail";
$to="someone@whatever.com";
$body="This is a test mail";
if (mail($to,$subject,$body))
echo "Mail sent successfully!";
else
echo"Mail not sent!";
?>

-------------------------------------------------------------------------------------

For those of you with the different type of ‘sendmail.ini’, make a backup copy of the sendmai.ini file, then replace the entire file with this:
;;; START SENDMAIL.INI FILE ;;;
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
pop3_server=
pop3_username=
pop3_password=
force_recipient=
hostname=
auth_username=[your-gmail-username]@gmail.com
auth_password=[your-gmail-password]
force_sender=[your-gmail-username]@gmail.com
;;; END SENDMAIL.INI FILE ;;;
Be sure to change the last three line above with the correct values. Save the file, then restart Apache for changes to take effect.

No comments: