2
votes

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 = "[email protected]";
   $subject = "Subject";
   $message = "Message";
   $header = "From:[email protected] \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=<[email protected]>, 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]

2
Here's a quote from PHP documentation: It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination. You need to check what happens on the server because the code is apparently valid.Gil
Have you checked the log file? /var/log/maillog on Redhat (?)AnFi
@AndrzejA.Filip thanks, see my edit.Aaron
As is the case with 99.9% of questions relating to PHP's mail() fn, the issue here is nothing to do with PHP's mail() fn. The problem is with the MTA config. Using a SMTP capable library might allow you to connect to a properly configured and managed MTA (setting one up requires real expertise) and a side benefit both phpmailer and swifmailer will produce properly encoded and formatted messages (what you are attempting to shove down the throat of sendmail here is not pretty).symcbean

2 Answers

4
votes

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.

0
votes

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