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 me@domain.com 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
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
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
Share on other sites

If you look at this: http://www.zend.com/pecl/tutorials/sockets.php, you'll see that the same socket is used for both reading and writing.
For debugging, write to a log file. I you ARE going to create a full blown smtp server, a log file is must anyway.
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.