socket_read() trouble with PHP_BINARY_READ

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hello,

I'm writing a socket approach to send email directly via an SMTP server (since some hosts block sendmail trough php due to abuse). Now, I have the code, attached below: I have cut it down slightly so it would still be readable though. I'm very sure that none of the stuff I removed actually matters in the problem though (mostly error chechking, logging, debug stuff, etc).

Ok, back to the problem. If I reread my log, I see the following "output":
S: 220 server -- Server ESMTP (iPlanet Messaging Server 5.2)
C: HELO ip
S:
C: MAIL FROM: <me@xxxxxxxxxxx>
S: 250 server OK, server2 [ip].
C: RCPT TO: <you@xxxxxxxxxxx>
S:
C: RSET

Now, obviously, the server sends something back (I checked, manually, using telnet). So, I figured that the socket_read(socket, size, PHP_NORMAL_READ) was causing the problem. So I switched over to PHP_BINARY_READ to make sure I didn't miss anything (because it broke off eg. midways). So... after I changed that, I suddenly started getting these errors: Warning: socket_read() unable to read from socket [11]: Resource temporarily unavailable in /home/me/scripts/mail.php on line 27

This goes for each attempt to read (even the first). I'm stumped... and really don't know how to proceed now...

Does anyone have any clues?
very appreciated,

- Tul

P.S. see attached, code:
<?php
class socket_SMTP {
	var $socket			= null;
	
	function connect($host, $port, $user, $pass) {
		$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		if ($this->socket < 0) {
exit("socket_create() failed: reason: " . socket_strerror($this->socket) . "\n");
		}
	
		$res = socket_connect($this->socket, gethostbyname($host), $port);
		if(false === $res) {
			echo 'No connection';
			return false;
		}
		socket_set_nonblock($this->socket);
		$this->get();
		$this->send('HELO '.'my-servers-ip-addy-goes-here');
		$this->get();	// ignore res
	}
	
	function send($cmd) {
		socket_write($this->connection, $cmd."\r\n", strlen($cmd."\r\n"));
	}
	
	function get() {
		$ret = socket_read($this->connection, 1024, PHP_BINARY_READ);
		return $ret;
	}
	
	function sendMessage($from, $to, $message, $headers) {
		$headers = $this->safeData($headers);
		$message = $this->safeData($message);
		
		$this->send('MAIL FROM: <'.$from.'>');
		$this->send('RCPT TO: <'.$to.'>');
		$this->send('DATA');
		$this->send($headers);
		$this->send('');	// CRLF to distinguish between headers and message
		$this->send($message);
		$this->send('.');
		
		// sent message
		return true;
	}
	
	function disconnect() {
		$this->send('QUIT');
		socket_close($this->socket);
	}
}
$mail = new socket_SMTP();
$mail->connect('mail.server.com', '25', 'username', 'password');
$mail->sendMessage('me@xxxxxxxxxxx', 'you@xxxxxxxxxxx', 'Test message', 'Subject: whatever-test-mail'."\r\n");
$mail->disconnect();
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux