Jump to content

socket questions


tomfmason

Recommended Posts

Ok I am attempt to write a script that will recieve mail. This is just a testing script. I am wanting to accept the incomming mail and write it to a text file, so that I will see the format and then decide how to process it from there. The problem is that when I send an email to [email protected] I get nothing. No error message or anything. Am I not commuicating with the server properly or ?

here is the socket.php
[code]
<?php
set_time_limit(0);
$port = 25;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket == false) {
    die("Error in createing the socket, the error code is " . socket_last_error() . "
    and the error message is " . socket_strerror(socket_last_error()));
}
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
    die("Unable to set socket option. The error is " . socket_last_error() . "
     and the error message is " . socket_strerror(socket_last_error()));
}
$bind = socket_bind($socket, NULL, $port);

if ($bind == false) {
    socket_close($socket);
die("socket_bind failed. The error code is " . socket_last_error() . "
     and the error message is " . socket_strerror(socket_last_error()));
}
$listen = socket_listen($socket, SOMAXCONN);
if ($listen == false) {
    socket_close($socket);
    die("socket_listen failed. The error code is " . socket_last_error() ."
     and the error message is " . socket_strerror(socket_last_error()));
}
while(true){
   $accept = socket_accept($socket);
   $read = socket_read($accept, 1024, PHP_NORMAL_READ);
   if ($read == NULL) {
       die("Unable to read the sent file");
   socket_close($socket);
   }
   if ($read = 'Exit') {
       socket_close($socket);
   }else{
      $filename = "C:\home\admin\test\test.txt";
  $fp = fopen($filename, "x+b");
  fwrite($fp, $read, 1024);
  fclose($fp);
   }
}
socket_close($socket);
?>[/code]

Am I on the right track or am I way off? Any suggestions would be great.

Thanks,
Tom
Link to comment
https://forums.phpfreaks.com/topic/18139-socket-questions/
Share on other sites

Hey weird, exactly what I'm trying to do. And I've never seen a question like this and here we are a day apart.
I think you'll need to read up on the SMTP protocol on Wikipedia, there is a series of messages sent back and forward before you get the message body.
Link to comment
https://forums.phpfreaks.com/topic/18139-socket-questions/#findComment-77766
Share on other sites

Ok I checked out the SMTP protocol and I am running into another issue. I know now that I have to use [code=php:0]sock_write()[/code] to send, something like this  220 mysite.com ESMTP, to the client. But how can I do this if I do not have there ip and port number.

here is what I have so far(dealing with this issue)

[code=php:0]
//the code above here is the same as my orginal post
while (true) {
    $accept = socket_accept($socket);
    socket_write($accept, '220 mysite.com ESMTP', 1024);
[/code]

So what this says to me is that it will write that message back to my server. So how do I get the ip of the sender? Would I use [code=php:0]$_SERVER['REMOTE_ADDR'];[/code] to obtain the ip from the sender?

Then do I need to create a socket to speak back to the sender. Or ?

Any suggestions would be great.

Thanks,
Tom
Link to comment
https://forums.phpfreaks.com/topic/18139-socket-questions/#findComment-77824
Share on other sites

Here is what I have so far. This is the most difficult script that I have tried to write to date. This is only because I cannot test it. If I am wrong I get nothing. No errors or anything.

Here we go. This is the same as above execpt for the while loop
[code=php:0]
$sender = array();
$data = array();
while (true) {
     $sender['ip'] = $_SERVER['REMOTE_ADDR'];
     $sender['port'] = $_SERVER['REMOTE_PORT'];
     $sender['sock'] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     $b = socket_bind($sender['sock'], $sender['ip'], $sender['port']);
     socket_write($sender['sock'], '220 mysite.com ESMPT', 1024);
     socket_recv($socket, $data[0], 1024, 0);
     if ($data[0] == 'QUIT') {
        socket_close($sender['sock']);
     }
     list($sender['greet'], $sender['domain']) = explode(" ", $data[0]);
     $ip = gethostbyname($domain);
     if ($ip !== $sender['ip']);
        socket_write($sender['sock'], 'QUIT' 1024);
        socket_close($sender['sock']);
     }else{
        socket_write($sender['sock'], 'Hellow ' . $sender['domain'] . '', 1024);
     }
     //continue with some more
}[/code]

Ok does that look right. I still don't know if a socket can support two way communication or not.

Any suggestions would be great.

Thanks,
Tom
     
Link to comment
https://forums.phpfreaks.com/topic/18139-socket-questions/#findComment-77874
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.