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

StackOverflow https://stackoverflow.com/questions/22336990

  •  13-06-2023
  •  | 
  •  

문제

I am working with a Redhat Linux server. A few months ago, I had written a simple user registration system in PHP5 using email as verification. 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):

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

Looking at php.ini, there is the following:

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

I am not that familiar with how SMTP servers work. 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)?

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):

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

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]

도움이 되었습니까?

해결책

Deferred: Connection refused by [127.0.0.1]

Modern sendmail relays messages to local sendmail daemon running as root using SMTP connection to 127.0.0.1:25. It has been done to avoid security risk of installing sendmail as set root uid program.

It seems that sendmail daemon/service has not been started (successfully) on your computer. Sendmail should report startup failure and its causes to the log file.

It seems that service sendmail restart command starts sendmail on redhat.

다른 팁

Instead of using mail/sendmail try using SMTP mail and see if that works. It could be that your sendmail isn't setup correctly on your server.

Use PHPMailer SMTP (See example):

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top