【问题标题】:failed to connect to mail server in PHP无法在 PHP 中连接到邮件服务器
【发布时间】:2011-01-22 15:17:50
【问题描述】:

我写了一小段用于发送邮件的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 =

请建议我应该怎么做?在此先感谢大家。

【问题讨论】:

  • WAMP 不是服务器。 WAMP 是 Windows、Apache2 网络服务器、MySQL dbms 和 PHP 脚本引擎的组合的“代号”。
  • 请纠正格式。侧边栏上有说明。
  • 这些“[CODE]”是什么废话?

标签: php email


【解决方案1】:

您是否在该主机/端口组合上拥有 SMTP 服务器?您可以通过以下方式检查是否有东西在听:

$ telnet localhost 25

并查看telnet 是否实际连接。

【讨论】:

    【解决方案2】:

    您也不能保证您的邮件已经发送,因为脚本不会等待邮件服务器继续。

    【讨论】:

      【解决方案3】:

      忘记这一点,如果你在 localhost 和 Windows 机器上,那将无法正常工作。
      改用 PHPMailer
      我已经设法通过 GMail 服务发送电子邮件(仅用于测试)。这真的很简单......试试看 PHPMailer

      【讨论】:

        猜你喜欢
        • 2018-11-11
        • 2014-08-28
        • 2016-05-20
        • 1970-01-01
        • 2019-09-08
        • 2013-09-10
        • 2021-12-24
        相关资源
        最近更新 更多