failed to connect to mail server in PHP
I have written a small piece of PHP code for sending mail:
<?php
//define the receiver of the email
$to = 'rohaanthakare@gmail.com';
//define the subject of the email
$subject = 'Hishob email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//send the email
$mail_sent = mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
after executing it I got following warning:
开发者_如何学编程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\NewHishob\PhpFiles\send_mail.php on line 11
Mail failed
I am using a WAMP server and there are two files, INI-DIST and other is INI-RECOMENDED. Which one is important?
I had also gone through php.ini-dist file and for SMTP it contains the following:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
;sendmail_from = me@example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =
Please suggest what I should do? Thanks to all in advance.
Do you have a SMTP server on that host/port combination ? You can check if something is listening there by doing:
$ telnet localhost 25
and seeing if telnet
actually connects.
Also you cannot guarantee that your mail have been sent, as the script doesn't wait for the mailserver to continue.
Forget about this, if you are on localhost and Windows machine, just won't work.
Use PHPMailer instead!
I have managed to sent email thru GMail service (just for testing). It is realy simple... Try it
PHPMailer
精彩评论