Jump to content

mail() function connecting to a looping cgi program looking at port 25


ocpaul20

Recommended Posts

On a linux machine, I want to run a php program which loops looking for anything coming in on a particular port.

 

In this case it is port 25, the smtp port of mail.

I have no mail server running.

 

I have written a php program using sockets which does this and I have written another program to send it stuff. Classic client -> server idea. Both work and communicate with each other run as cgi in 2 terminal windows.

 

My problem is that when I run a program from my browser which calls the mail() function, it returns false.

 

this is the program I run from a browser which I want to connect to another program running in a terminal window looking for input from port 25.

 

error_reporting(E_ALL);
ini_set("SMTP","/root/httpd/localhost/httpdocs/dummy_smtp_server.php"); // run as a cgi in a terminal window
ini_set("smtp_port","25");

$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$m = mail($to, $subject, $message, $headers);
echo "<br>returned from mail: ";
var_dump($m);

 

in my php.ini file I have this

[mail function]
; For Win32 only.
SMTP = localhost

smtp_port = 25

; For Win32 only.
sendmail_from = me@localhost.com

; For Unix only. 
sendmail_path = "/root/httpd/localhost/httpdocs/dummy_smtp_server.php"

 

So, my question is - does anyone have any idea what I can do to get this working please or why it doesn't work?

 

Thanks

Paul

Link to comment
Share on other sites

I have already said that I can run another terminal and communicate with the port 25 server.

 

running telnet localhost 25 in a terminal does communicate successfully with the server program running in another console.

 

It is the mail() function that I am having trouble running in a php program  (shown above) from the browser. It will not connect/send mail to a server program running in a console watching for incoming characters on port 25.

Link to comment
Share on other sites

  • 3 weeks later...

any more ideas please anyone?

 

It would really help if I had a mickey mouse program like this that I could call using mail() from my application that just sat in the background and accepted mails and then responded with the correct code to make the application think the mail had been sent.

 

Surely someone has played with sockets and was able to do this kind of thing?

 

The programming part is not hard, but the way to get it to work on a unix machine that is beating me.

Link to comment
Share on other sites

OK, well I have found the solution and it is that the mail() statement sends to whatever is in the php.ini file variiable

 

sendmail_path = "/opt/lampp/bin/php -f /root/httpd/utils/httpdocs/trapmail.php -c /usr/httpd"

 

I put this and wrote the program trapmail.php. The email comes in in stdin so you need to read that to get the output from mail(). You also need to return 0; at the end to say to mail() that it was successful.

 

$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);

 

It seems that there are lots of examples of this on the internet, so I read a few articles and got it sorted.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.