define("smtp_host","127.0.0.1"); function get_num($fd) { $res=trim(fgets($fd)); if(is_bool($res) && $res==false) return -1; if(empty($res)) return -1; $res=strtok($res," "); return (INT)$res; } function bmail($from,$to,$subject,$body,$header=NULL) { $ip=(ip2long(smtp_host)==-1) ? gethostbyname(smtp_host):smtp_host; if(ip2long($ip)==-1) return false; $dummy1; $dummy2; if(!($fd=@fsockopen($ip,25,$dummy1,$dummy2,10))) return false; if(get_num($fd)!=220) { fclose($fd); return false; } if(!fwrite($fd,"HELO $ip\r\n")) { fclose($fd); return false; } if(get_num($fd)!=250) { fclose($fd); return false; } $from=trim($from); if(!fwrite($fd,"MAIL FROM:<$from>\r\n")) { fclose($fd); return false; } if(get_num($fd)!=250) { fclose($fd); return false; } $to=trim($to); $ato=split(", ",$to); for($t=current($ato);!empty($t);$t=next($ato)) { if(!fwrite($fd,"RCPT TO:<$t>\r\n")) { fclose($fd); return false; } if(get_num($fd)!=250) { fclose($fd); return false; } } if(!fwrite($fd,"DATA\r\n")) { fclose($fd); return false; } if(get_num($fd)!=354) { fclose($fd); return false; } if(!fwrite($fd,"From: $from\r\n")) { fclose($fd); return false; } if(!fwrite($fd,"To: $to\r\n")) { fclose($fd); return false; } $subject=trim($subject); if(!empty($subject)) { if(!fwrite($fd,"Subject: $subject\r\n")) { fclose($fd); return false; } } if(!empty($header)) { $header=trim($header); if(!fwrite($fd,"$header\r\n")) { fclose($fd); return false; } } if(!fwrite($fd,"\r\n")) { fclose($fd); return false; } $body=str_replace("\r\n.","\r\n..",trim($body)); if(!fwrite($fd,"$body\r\n")) { fclose($fd); return false; } if(!fwrite($fd,"\r\n.\r\n")) { fclose($fd); return false; } if(get_num($fd)!=250) { fclose($fd); return false; } if(!fwrite($fd,"QUIT\r\n")) { fclose($fd); return false; } fclose($fd); return true; } ?>