tomfmason Posted August 20, 2006 Share Posted August 20, 2006 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]<?phpset_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 Quote Link to comment Share on other sites More sharing options...
ShogunWarrior Posted August 20, 2006 Share Posted August 20, 2006 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. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 20, 2006 Author Share Posted August 20, 2006 thanks. I will check into that Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 21, 2006 Author Share Posted August 21, 2006 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 postwhile (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 Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 21, 2006 Author Share Posted August 21, 2006 Is a socket capable of to way communication or no? What I mean is do I need to create a new socket for writing to the client? Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 21, 2006 Author Share Posted August 21, 2006 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 Quote Link to comment Share on other sites More sharing options...
448191 Posted August 21, 2006 Share Posted August 21, 2006 You won't be able to use any port lower than 1024 if you don't have root access on the servermachine.If you can somehow 'trick' the client to use a higher port, you'd be fine. Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 21, 2006 Author Share Posted August 21, 2006 I have root access. By the way great pic ^ Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 21, 2006 Author Share Posted August 21, 2006 So am I right so far? Do I need to create another socket to write to the client or is the first socket capable of two way communication?Any help would be greatly appericated.Thanks, Tom Quote Link to comment Share on other sites More sharing options...
tomfmason Posted August 21, 2006 Author Share Posted August 21, 2006 I guess I will just have to keep messing with it untill I get it..lol. After I am done with this, every other script I write will seem easy in comparison.. Quote Link to comment Share on other sites More sharing options...
448191 Posted August 21, 2006 Share Posted August 21, 2006 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. Quote Link to comment Share on other sites More sharing options...
ShogunWarrior Posted August 21, 2006 Share Posted August 21, 2006 I tried sockets on my local PC but when I used [b]telnet[/b] to access my 'PHP Server' [b]socket_read[/b] would only read one character from [b]telnet[/b], I'm not sure what the problem was, so I haven't continued with it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.