简体   繁体   English

PHP邮件返回true,但未发送电子邮件-“ [127.0.0.1]拒绝连接”

[英]PHP mail returns true, but no email is being sent - “Connection refused by [127.0.0.1]”

I am working with a Redhat Linux server. 我正在使用Redhat Linux服务器。 A few months ago, I had written a simple user registration system in PHP5 using email as verification. 几个月前,我用PHP5用电子邮件作为验证编写了一个简单的用户注册系统。 I recall that it was working at that time. 我记得当时它在工作。 However, after recently testing the system, I find that the email is not actually being sent (I've tried a few email addresses from different domains, as well as checked spam folders). 但是,在最近对系统进行测试之后,我发现实际上并没有发送电子邮件(我尝试了来自不同域的一些电子邮件地址以及已检查的垃圾邮件文件夹)。

As a test case, I tried the simple code below (using my actual email address as $to): 作为测试用例,我尝试了以下简单代码(使用我的实际电子邮件地址为$ to):

<html>
<head>
<title>Sending email using PHP</title>
</head>
<body>
<?php
   $to = "myemail@gmail.com";
   $subject = "Subject";
   $message = "Message";
   $header = "From:webmaster@mydomain.com \r\n";
   $header .= "MIME-Version: 1.0\r\n";
   $header .= "Content-type: text/html\r\n";
   $ret = mail ($to,$subject,$message,$header);
   if( $ret == true )  
   {
      echo "Message sent successfully.";
   }
   else
   {
      echo "Message could not be sent.";
   }
?>
</body>
</html>

It echoes "Message sent successfully". 它回显“消息发送成功”。 I also tried using PHPMailer , which gives a successful message (I believe their implementation uses the mail() function as well). 我还尝试使用PHPMailer ,它给出了一条成功消息(我相信它们的实现也使用mail()函数)。

Looking at php.ini, there is the following: 查看php.ini,其中包含以下内容:

[mail function]
SMTP = localhost
smtp_port = 25
sendmail_path = /usr/sbin/sendmail -t -i

I am not that familiar with how SMTP servers work. 我不太了解SMTP服务器的工作方式。 What are some steps that I can take to troubleshoot this issue? 我可以采取哪些步骤来解决此问题? Is this simply a matter of contacting the server admin or is there something I can change myself (I have root access)? 这仅仅是联系服务器管理员的问题,还是我可以更改自己的权限(我具有root用户访问权限)?

Edit: 编辑:
From Andrzej's suggestion, I checked the maillog file and found these two lines from a recent attempt (I replaced my servername and email): 根据Andrzej的建议,我检查了邮件日志文件,并从最近的尝试中找到了这两行(我替换了服务器名和电子邮件):

Mar 11 17:11:30 myservername sendmail[23240]: s2BLBU2x023240: from=apache, size=149, class=0, nrcpts=1, msgid=<201403112111.s2BLBU2x023240@myservername.com>, relay=apache@localhost 3月11日17:11:30 myservername sendmail [23240]:s2BLBU2x023240:from = apache,size = 149,class = 0,nrcpts = 1,msgid = <201403112111.s2BLBU2x023240@myservername.com>,relay = apache @ localhost

Mar 11 17:11:30 myservername sendmail[23240]: s2BLBU2x023240: to=myemail, ctladdr=apache (48/48), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30149, relay=[127.0.0.1] [127.0.0.1], dsn=4.0.0, stat=Deferred: Connection refused by [127.0.0.1] 3月11日17:11:30 myservername sendmail [23240]:s2BLBU2x023240:to = myemail,ctladdr = apache(48/48),delay = 00:00:00,xdelay = 00:00:00,mailer = relay,pri = 30149,中继= [127.0.0.1] [127.0.0.1],dsn = 4.0.0,stat =延迟:连接被[127.0.0.1]拒绝

Deferred: Connection refused by [127.0.0.1] 推迟:[127.0.0.1]拒绝连接

Modern sendmail relays messages to local sendmail daemon running as root using SMTP connection to 127.0.0.1:25. 现代sendmail使用到127.0.0.1:25的SMTP连接将消息中继到以root用户身份运行的本地sendmail守护程序。 It has been done to avoid security risk of installing sendmail as set root uid program. 这样做是为了避免将sendmail作为set root uid程序安装的安全风险。

It seems that sendmail daemon/service has not been started (successfully) on your computer. 似乎您的计算机上尚未成功启动sendmail守护程序/服务。 Sendmail should report startup failure and its causes to the log file. Sendmail应该向日志文件报告启动失败及其原因。

It seems that service sendmail restart command starts sendmail on redhat. 似乎service sendmail restart命令在redhat上启动sendmail。

Instead of using mail/sendmail try using SMTP mail and see if that works. 与其使用邮件/发送邮件,不如尝试使用SMTP邮件,看看是否可行。 It could be that your sendmail isn't setup correctly on your server. 可能是您的sendmail没有在服务器上正确设置。

Use PHPMailer SMTP (See example): 使用PHPMailer SMTP(请参见示例):

http://phpmailer.worxware.com/index.php?pg=examplebsmtp http://phpmailer.worxware.com/index.php?pg=examplebsmtp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM