无法连接到PHP中的邮件服务器

时间:2011-01-22 15:17:51

标签: php email

我写了一小段用于发送邮件的PHP代码:

   <?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";
    ?>

执行后我收到了以下警告:

  

无法连接到“localhost”端口25的邮件服务器,验证php.ini中的“SMTP”和“smtp_port”设置,或者使用C:\ wamp \ www \ NewHishob \ PhpFiles \ send_mail.php中的ini_set()第11行   邮件失败

我使用的是WAMP服务器,有两个文件,INI-DIST和其他文件是INI-RECOMENDED。哪一个很重要?

我还浏览过php.ini-dist文件,对于SMTP,它包含以下内容:

[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 =

请建议我该怎么做?感谢所有提前。

3 个答案:

答案 0 :(得分:5)

在该主机/端口组合上是否有SMTP服务器?您可以通过执行以下操作来检查某些内容是否正在侦听:

$ telnet localhost 25

并查看telnet是否实际连接。

答案 1 :(得分:0)

此外,您无法保证已发送邮件,因为脚本不会等待邮件服务器继续。

答案 2 :(得分:0)

忘记这一点,如果你在localhost和Windows机器上,就行不通 改用PHPMailer ! 我已设法通过GMail服务发送电子邮件(仅用于测试)。这真的很简单......试试吧 PHPMailer