Source

linux.org.il-site / site / inc / mail.php

Full commit
<?

// A very simple-minded SMTP client, written since our WWW server
// runs chrooted.

function smtp_code($res)
{
	if (ereg("^([0-9]+) ", $res, $matches))
		return $matches[1];
}

function add_cr($str)
{
	return preg_replace("/(?<!\r)\n/s", "\r\n", $str);
}

function mail2($to, $subject, $message, $headers)
{
	$fp = fsockopen("localhost", 25);
	if (!$fp)
		return false;

	if (smtp_code(fgets($fp, 1024)) != 220)
		return false;
	
	fputs($fp, "HELO localhost\r\n");
	if (smtp_code(fgets($fp, 1024)) != 250)
		return false;
		
	fputs($fp, "MAIL FROM: www@localhost\r\n");
	if (smtp_code(fgets($fp, 1024)) != 250)
		return false;
	fputs($fp, "RCPT TO: $to\r\n");
	if (smtp_code(fgets($fp, 1024)) != 250)
		return false;
	fputs($fp, "DATA\r\n");
	if (smtp_code(fgets($fp, 1024)) != 354)
		return false;
	fputs($fp, "Subject: $subject\r\n");
	$headers = add_cr($headers);
	$message = add_cr($message);
	if ($headers)
		fputs($fp, "$headers\r\n");
	fputs($fp, "\r\n$message\r\n");
	fputs($fp, ".\r\n");
	if (smtp_code(fgets($fp, 1024)) != 250)
		return false;
	
	return true;
}

?>