90 $this->helo_rply = null;
112 public function Connect($host, $port = 0, $tval = 30) {
117 if($this->connected()) {
119 $this->error = array(
"error" =>
"Already connected to a server");
128 $this->smtp_conn = @fsockopen($host,
134 if(empty($this->smtp_conn)) {
135 $this->error = array(
"error" =>
"Failed to connect to server",
137 "errstr" => $errstr);
138 if($this->do_debug >= 1) {
139 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": $errstr ($errno)" . $this->CRLF .
'<br />';
146 if(substr(PHP_OS, 0, 3) !=
"WIN")
147 socket_set_timeout($this->smtp_conn, $tval, 0);
150 $announce = $this->get_lines();
152 if($this->do_debug >= 2) {
153 echo
"SMTP -> FROM SERVER:" . $announce . $this->CRLF .
'<br />';
169 $this->error = null; # to avoid confusion
171 if(!$this->connected()) {
172 $this->error = array(
"error" =>
"Called StartTLS() without being connected");
176 fputs($this->smtp_conn,
"STARTTLS" . $this->CRLF);
178 $rply = $this->get_lines();
179 $code = substr($rply,0,3);
181 if($this->do_debug >= 2) {
182 echo
"SMTP -> FROM SERVER:" . $rply . $this->CRLF .
'<br />';
187 array(
"error" =>
"STARTTLS not accepted from server",
188 "smtp_code" => $code,
189 "smtp_msg" => substr($rply,4));
190 if($this->do_debug >= 1) {
191 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
197 if(!stream_socket_enable_crypto($this->smtp_conn,
true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
212 fputs($this->smtp_conn,
"AUTH LOGIN" . $this->CRLF);
214 $rply = $this->get_lines();
215 $code = substr($rply,0,3);
219 array(
"error" =>
"AUTH not accepted from server",
220 "smtp_code" => $code,
221 "smtp_msg" => substr($rply,4));
222 if($this->do_debug >= 1) {
223 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
229 fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
231 $rply = $this->get_lines();
232 $code = substr($rply,0,3);
236 array(
"error" =>
"Username not accepted from server",
237 "smtp_code" => $code,
238 "smtp_msg" => substr($rply,4));
239 if($this->do_debug >= 1) {
240 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
246 fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
248 $rply = $this->get_lines();
249 $code = substr($rply,0,3);
253 array(
"error" =>
"Password not accepted from server",
254 "smtp_code" => $code,
255 "smtp_msg" => substr($rply,4));
256 if($this->do_debug >= 1) {
257 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
271 if(!empty($this->smtp_conn)) {
272 $sock_status = socket_get_status($this->smtp_conn);
273 if($sock_status[
"eof"]) {
275 if($this->do_debug >= 1) {
276 echo
"SMTP -> NOTICE:" . $this->CRLF .
"EOF caught while checking if connected";
295 $this->helo_rply = null;
296 if(!empty($this->smtp_conn)) {
298 fclose($this->smtp_conn);
299 $this->smtp_conn = 0;
326 public function Data($msg_data) {
329 if(!$this->connected()) {
330 $this->error = array(
331 "error" =>
"Called Data() without being connected");
335 fputs($this->smtp_conn,
"DATA" . $this->CRLF);
337 $rply = $this->get_lines();
338 $code = substr($rply,0,3);
340 if($this->do_debug >= 2) {
341 echo
"SMTP -> FROM SERVER:" . $rply . $this->CRLF .
'<br />';
346 array(
"error" =>
"DATA command not accepted from server",
347 "smtp_code" => $code,
348 "smtp_msg" => substr($rply,4));
349 if($this->do_debug >= 1) {
350 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
367 $msg_data = str_replace(
"\r\n",
"\n",$msg_data);
368 $msg_data = str_replace(
"\r",
"\n",$msg_data);
369 $lines = explode(
"\n",$msg_data);
380 $field = substr($lines[0],0,strpos($lines[0],
":"));
382 if(!empty($field) && !strstr($field,
" ")) {
386 $max_line_length = 998;
388 while(list(,$line) = @each($lines)) {
390 if($line ==
"" && $in_headers) {
394 while(strlen($line) > $max_line_length) {
395 $pos = strrpos(substr($line,0,$max_line_length),
" ");
399 $pos = $max_line_length - 1;
400 $lines_out[] = substr($line,0,$pos);
401 $line = substr($line,$pos);
403 $lines_out[] = substr($line,0,$pos);
404 $line = substr($line,$pos + 1);
411 $line =
"\t" . $line;
414 $lines_out[] = $line;
417 while(list(,$line_out) = @each($lines_out)) {
418 if(strlen($line_out) > 0)
420 if(substr($line_out, 0, 1) ==
".") {
421 $line_out =
"." . $line_out;
424 fputs($this->smtp_conn,$line_out . $this->CRLF);
429 fputs($this->smtp_conn, $this->CRLF .
"." . $this->CRLF);
431 $rply = $this->get_lines();
432 $code = substr($rply,0,3);
434 if($this->do_debug >= 2) {
435 echo
"SMTP -> FROM SERVER:" . $rply . $this->CRLF .
'<br />';
440 array(
"error" =>
"DATA not accepted from server",
441 "smtp_code" => $code,
442 "smtp_msg" => substr($rply,4));
443 if($this->do_debug >= 1) {
444 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
466 if(!$this->connected()) {
467 $this->error = array(
468 "error" =>
"Called Hello() without being connected");
479 if(!$this->SendHello(
"EHLO", $host)) {
480 if(!$this->SendHello(
"HELO", $host)) {
493 private function SendHello($hello, $host) {
494 fputs($this->smtp_conn, $hello .
" " . $host . $this->CRLF);
496 $rply = $this->get_lines();
497 $code = substr($rply,0,3);
499 if($this->do_debug >= 2) {
500 echo
"SMTP -> FROM SERVER: " . $rply . $this->CRLF .
'<br />';
505 array(
"error" => $hello .
" not accepted from server",
506 "smtp_code" => $code,
507 "smtp_msg" => substr($rply,4));
508 if($this->do_debug >= 1) {
509 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
514 $this->helo_rply = $rply;
536 if(!$this->connected()) {
537 $this->error = array(
538 "error" =>
"Called Mail() without being connected");
542 $useVerp = ($this->do_verp ?
"XVERP" :
"");
543 fputs($this->smtp_conn,
"MAIL FROM:<" . $from .
">" . $useVerp . $this->CRLF);
545 $rply = $this->get_lines();
546 $code = substr($rply,0,3);
548 if($this->do_debug >= 2) {
549 echo
"SMTP -> FROM SERVER:" . $rply . $this->CRLF .
'<br />';
554 array(
"error" =>
"MAIL not accepted from server",
555 "smtp_code" => $code,
556 "smtp_msg" => substr($rply,4));
557 if($this->do_debug >= 1) {
558 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
576 public function Quit($close_on_error =
true) {
579 if(!$this->connected()) {
580 $this->error = array(
581 "error" =>
"Called Quit() without being connected");
586 fputs($this->smtp_conn,
"quit" . $this->CRLF);
589 $byemsg = $this->get_lines();
591 if($this->do_debug >= 2) {
592 echo
"SMTP -> FROM SERVER:" . $byemsg . $this->CRLF .
'<br />';
598 $code = substr($byemsg,0,3);
601 $e = array(
"error" =>
"SMTP server rejected quit command",
602 "smtp_code" => $code,
603 "smtp_rply" => substr($byemsg,4));
605 if($this->do_debug >= 1) {
606 echo
"SMTP -> ERROR: " . $e[
"error"] .
": " . $byemsg . $this->CRLF .
'<br />';
610 if(empty($e) || $close_on_error) {
632 if(!$this->connected()) {
633 $this->error = array(
634 "error" =>
"Called Recipient() without being connected");
638 fputs($this->smtp_conn,
"RCPT TO:<" . $to .
">" . $this->CRLF);
640 $rply = $this->get_lines();
641 $code = substr($rply,0,3);
643 if($this->do_debug >= 2) {
644 echo
"SMTP -> FROM SERVER:" . $rply . $this->CRLF .
'<br />';
647 if($code != 250 && $code != 251) {
649 array(
"error" =>
"RCPT not accepted from server",
650 "smtp_code" => $code,
651 "smtp_msg" => substr($rply,4));
652 if($this->do_debug >= 1) {
653 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
675 if(!$this->connected()) {
676 $this->error = array(
677 "error" =>
"Called Reset() without being connected");
681 fputs($this->smtp_conn,
"RSET" . $this->CRLF);
683 $rply = $this->get_lines();
684 $code = substr($rply,0,3);
686 if($this->do_debug >= 2) {
687 echo
"SMTP -> FROM SERVER:" . $rply . $this->CRLF .
'<br />';
692 array(
"error" =>
"RSET failed",
693 "smtp_code" => $code,
694 "smtp_msg" => substr($rply,4));
695 if($this->do_debug >= 1) {
696 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
723 if(!$this->connected()) {
724 $this->error = array(
725 "error" =>
"Called SendAndMail() without being connected");
729 fputs($this->smtp_conn,
"SAML FROM:" . $from . $this->CRLF);
731 $rply = $this->get_lines();
732 $code = substr($rply,0,3);
734 if($this->do_debug >= 2) {
735 echo
"SMTP -> FROM SERVER:" . $rply . $this->CRLF .
'<br />';
740 array(
"error" =>
"SAML not accepted from server",
741 "smtp_code" => $code,
742 "smtp_msg" => substr($rply,4));
743 if($this->do_debug >= 1) {
744 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply . $this->CRLF .
'<br />';
765 $this->error = array(
"error" =>
"This method, TURN, of the SMTP ".
766 "is not implemented");
767 if($this->do_debug >= 1) {
768 echo
"SMTP -> NOTICE: " . $this->error[
"error"] . $this->CRLF .
'<br />';
795 private function get_lines() {
797 while($str = @fgets($this->smtp_conn,515)) {
798 if($this->do_debug >= 4) {
799 echo
"SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF .
'<br />';
800 echo
"SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF .
'<br />';
803 if($this->do_debug >= 4) {
804 echo
"SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF .
'<br />';
807 if(substr($str,3,1) ==
" ") {
break; }