LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP mail() Function Failing (https://www.linuxquestions.org/questions/programming-9/php-mail-function-failing-914865/)

devUnix 11-22-2011 03:06 AM

PHP mail() Function Failing
 
Hi,


I have been unable to figure out why PHP's mail() function is not working for me. I am able to send emails using VB Script on the same box. I have configured IIS and SMTP on this Windows XP Pro box.

Note: This example is given on the official manual (website php.net) of PHP:

Code:

<?php
$to      = 'user@myDomain.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: me@myDomain.com' . "\r\n" .
    'Reply-To: me@myDomain.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

$status = mail($to, $subject, $message, $headers);
if($status){echo "Sent";} else {echo "Failed";}
?>


Here is the VB Script that works fine for me on the same box:

Code:

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = "me@MyDomain.com"
objMessage.To = "user@MyDomain.com"
objMessage.TextBody = "This is some sample message text."
objMessage.Send

I used this HTML version on some other box before, and it used to work fine for me:

Code:

<?php

$message = "<html><body><h4>" . "Hello" . "</h4></body></html>";

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: me@MyDomain.com\r\n";


$status = @mail("user@MyDomain.com", "Testing Message", $message, $headers);



if ($status) {
                echo "Sent";
}

else {

        echo "Failed";

}


?>

So, what could be wrong with the PHP's mail() function on my box?

Since the project is PHP based, I need to employ mail() to get the work (sending email alerts) done.

Please Note: IIS and SMTP are working fine as the VB Script code given above is able to send emails. I am not sending emails to any outside network. It is for the LAN / Intranet only. I have used mail() several times before and it always worked. On this box it is not working. What could I be missing?

My php.ini settings look like this:

Code:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

I changed localhost to 127.0.0.1 and to the actual IP as well. I have changed it to some other SMTP Server that we are using. But none has worked.

Doc CPU 11-22-2011 04:01 AM

Hi there,

Quote:

Originally Posted by devUnix (Post 4530513)
I have been unable to figure out why PHP's mail() function is not working for me. I am able to send emails using VB Script on the same box. I have configured IIS and SMTP on this Windows XP Pro box.

I'm not sure, but: Does VBScript really use the SMTP? I could imagine that it uses a direct function call of IIS to send mail. Which arouses the question if your SMTP really works.
Can you establish a telnet session to port 25 on localhost? If yes, do you see the greeting message of an SMTP server? Does your SMTP require authentication (which PHP's mail() doesn't support)?

Quote:

Originally Posted by devUnix (Post 4530513)
I used this HTML version on some other box before, and it used to work fine for me:

Just to be sure: Does the example from php.net work for you? And what is your actual PHP code now?

Quote:

Originally Posted by devUnix (Post 4530513)
Since the project is PHP based, I need to employ mail() to get the work (sending email alerts) done.

Well, you could also use a publicly available PHP class to do the job; you might even use fsockopen() and "speak" SMTP yourself (I'm not recommending that, however).

Quote:

Originally Posted by devUnix (Post 4530513)
My php.ini settings look like this:

Code:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25


That looks fine from here. You might try port 587 instead, which is commonly used for SMTP nowadays, but 25 should usually work.
Doesn't IIS have any logging mechanism? It might be very helpful to have a log of the SMTP dialog.

[X] Doc CPU

devUnix 11-22-2011 05:12 AM

Quote:

Originally Posted by Doc CPU (Post 4530537)
Just to be sure: Does the example from php.net work for you? And what is your actual PHP code now?


Here it is:

Code:

http://php.net/manual/en/function.mail.php
I only changed email addresses in the example given above.

I have used the HTML version several times before but on a different box and LAN. No PHP mail() is working for me on this box.

telnet to localhost : I did it now but it does not seem to work. I tried both: telnet localhost 587 and telnet localhost 25 on the command line as well as with Hyper Terminal. On the command prompt it reports:

Code:

C:\>telnet localhost
Connecting To localhost...Could not open connection to the host, on port 23: Connect failed



The IIS log says:
Code:

#Software: Microsoft Internet Information Services 5.1
#Version: 1.0
#Date: 2011-11-22 07:28:29
#Fields: time c-ip cs-method cs-uri-stem sc-status
07:28:29 127.0.0.1 GET /sendEmail.php 200

Socket is something alien to me. I am not sure if it can alone help me. I have got some manual on this page on fsockopen().

Doc CPU 11-22-2011 06:08 AM

Hi there,

Quote:

Originally Posted by devUnix (Post 4530579)
Code:

http://php.net/manual/en/function.mail.php
I only changed email addresses in the example given above.
I have used the HTML version several times before but on a different box and LAN. No PHP mail() is working for me on this box.

okay, I see. So you used the original code from php.net's sample. I just guessed you might have changed something and built a mistake in doing that.

Quote:

Originally Posted by devUnix (Post 4530579)
telnet to localhost : I did it now but it does not seem to work. I tried both: telnet localhost 587 and telnet localhost 25 on the command line as well as with Hyper Terminal. On the command prompt it reports:

Code:

C:\>telnet localhost
Connecting To localhost...Could not open connection to the host, on port 23: Connect failed


The example you quoted is for a connection to the standard telnet port 23, however, because you forgot to add the port number on the command line. But if you couldn't get a connection with
Code:

telnet localhost 25
it means that there isn't an SMTP server that accepts a connection. Then it's pointless to look for errors in your script; it might be perfectly okay.

Quote:

Originally Posted by devUnix (Post 4530579)
The IIS log says:
Code:

#Software: Microsoft Internet Information Services 5.1
#Version: 1.0
#Date: 2011-11-22 07:28:29
#Fields: time c-ip cs-method cs-uri-stem sc-status
07:28:29 127.0.0.1 GET /sendEmail.php 200


That's obviously from the web server's part of the log (HTTP), not the SMTP. It says that 127.0.0.1 (localhost) requested the document /sendEmail.php at 07:28:29 this morning, and the request was successful (status 200).
Are you sure the SMTP server of IIS is really set up correctly? The telnet probing looks like it's not.

Quote:

Originally Posted by devUnix (Post 4530579)
Socket is something alien to me. I am not sure if it can alone help me. I have got some manual on this page on fsockopen().

Forget about sockets for the moment; I just mentioned that for the sake of completeness, because it's another possible approach, even though it requires a lot of coding.

[X] Doc CPU

devUnix 12-05-2011 01:34 AM

I changed the php.ini and set "SMTP = myCompany'sSMTPserverName" but initially it did not work. I then checked certain services such as RPC and found them stopped. I restarted those stopped services this emails went across.

So, since then I have been using the same SMTP Server. But just now I changed the value to "localhost" to see if the local SMTP is working now. But it is not.

I am not sure what could be wrong with the SMTP on this local box. I re-installed it a couple of times.

So, the local SMTP is the culprit. I am not sure how to set it right.


All times are GMT -5. The time now is 12:12 AM.