LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2011, 04:06 AM   #1
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
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.

Last edited by devUnix; 11-22-2011 at 04:11 AM.
 
Old 11-22-2011, 05:01 AM   #2
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 343Reputation: 343Reputation: 343Reputation: 343
Hi there,

Quote:
Originally Posted by devUnix View Post
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 View Post
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 View Post
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 View Post
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
 
Old 11-22-2011, 06:12 AM   #3
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by Doc CPU View Post
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().
 
Old 11-22-2011, 07:08 AM   #4
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 343Reputation: 343Reputation: 343Reputation: 343
Hi there,

Quote:
Originally Posted by devUnix View Post
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 View Post
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 View Post
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 View Post
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
 
Old 12-05-2011, 02:34 AM   #5
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Mail function phantom_cyph Programming 6 05-16-2008 12:45 AM
mail-function php sharp81 Linux - Software 3 02-17-2006 10:45 AM
HELP !!! php mail function akamaru607 Programming 22 08-26-2005 03:44 PM
PHP - mail function saravanan1979 Programming 2 08-06-2003 02:46 AM
Using the PHP mail() Function !?!? Hdata Programming 0 06-22-2003 07:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration