88 function Connect($host, $port = 0, $tval = 30)
90 # set the error val to null so there is no confusion
93 # make sure we are __not__ connected
94 if ($this->connected())
96 # ok we are connected! what should we do?
97 # for now we will just give an error saying we
98 # are already connected
99 $this->error = array(
"error" =>
"Already connected to a server");
108 #connect to the smtp server
109 $this->smtp_conn = fsockopen($host, # the host of the server
110 $port, # the port to use
111 $errno, # error number
if any
112 $errstr, # error message
if any
113 $tval); # give up after ? secs
114 # verify we connected properly
115 if (empty($this->smtp_conn))
119 "error" =>
"Failed to connect to server",
124 if ($this->do_debug >= 1)
126 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": $errstr ($errno)" .
$this->CRLF;
132 # sometimes the SMTP server takes a little longer to respond
133 # so we will give it a longer timeout for the first read
135 if (substr(PHP_OS, 0, 3) !=
"WIN")
136 socket_set_timeout($this->smtp_conn, $tval, 0);
138 # get any announcement stuff
141 # set the timeout of any socket functions at 1/10 of a second
145 if ($this->do_debug >= 2)
147 echo
"SMTP -> FROM SERVER:" . $this->CRLF . $announce;
162 fputs($this->smtp_conn,
"AUTH LOGIN" . $this->CRLF);
165 $code = substr($rply, 0, 3);
171 "error" =>
"AUTH not accepted from server",
172 "smtp_code" => $code,
173 "smtp_msg" => substr($rply, 4)
176 if ($this->do_debug >= 1)
178 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply .
$this->CRLF;
185 fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
188 $code = substr($rply, 0, 3);
194 "error" =>
"Username not accepted from server",
195 "smtp_code" => $code,
196 "smtp_msg" => substr($rply, 4)
199 if ($this->do_debug >= 1)
201 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply .
$this->CRLF;
208 fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
211 $code = substr($rply, 0, 3);
217 "error" =>
"Password not accepted from server",
218 "smtp_code" => $code,
219 "smtp_msg" => substr($rply, 4)
222 if ($this->do_debug >= 1)
224 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply .
$this->CRLF;
308 $this->error =
null; # so no confusion is caused
310 if (!$this->connected())
312 $this->error = array(
"error" =>
"Called Data() without being connected");
316 fputs($this->smtp_conn,
"DATA" . $this->CRLF);
319 $code = substr($rply, 0, 3);
321 if ($this->do_debug >= 2)
323 echo
"SMTP -> FROM SERVER:" . $this->CRLF . $rply;
330 "error" =>
"DATA command not accepted from server",
331 "smtp_code" => $code,
332 "smtp_msg" => substr($rply, 4)
335 if ($this->do_debug >= 1)
337 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply .
$this->CRLF;
343 # the server is ready to accept data!
344 # according to rfc 821 we should not send more than 1000
346 # characters on a single line so we will break the data up
347 # into lines by \r and/or \n then if needed we will break
348 # each of those into smaller lines to fit within the limit.
349 # in addition we will be looking for lines that start with
350 # a period '.' and append and additional period '.' to that
351 # line. NOTE: this does not count towards are limit.
353 # normalize the line breaks so we know the explode works
354 $msg_data = str_replace(
"\r\n",
"\n", $msg_data);
355 $msg_data = str_replace(
"\r",
"\n", $msg_data);
356 $lines = explode(
"\n", $msg_data);
358 # we need to find a good way to determine is headers are
359 # in the msg_data or if it is a straight msg body
360 # currently I'm assuming rfc 822 definitions of msg headers
361 # and if the first field of the first line (':' sperated)
362 # does not contain a space then it _should_ be a header
363 # and we can process all lines before a blank "" line as
365 $field = substr($lines[0], 0, strpos($lines[0],
":"));
368 if (!empty($field) && !strstr($field,
" "))
373 $max_line_length = 998; # used below;
set here
for ease in change
375 while (list(, $line) = @each($lines))
379 if ($line ==
"" && $in_headers)
384 # ok we need to break this line up into several
386 while (strlen($line) > $max_line_length)
388 $pos = strrpos(substr($line, 0, $max_line_length),
" ");
389 $lines_out[] = substr($line, 0, $pos);
390 $line = substr($line, $pos + 1);
392 # if we are processing headers we need to
393 # add a LWSP-char to the front of the new line
394 # rfc 822 on long msg headers
397 $line =
"\t" . $line;
401 $lines_out[] = $line;
403 # now send the lines to the server
404 while (list(, $line_out) = @each($lines_out))
406 if (strlen($line_out) > 0)
408 if (substr($line_out, 0, 1) ==
".")
410 $line_out =
"." . $line_out;
414 fputs($this->smtp_conn, $line_out . $this->CRLF);
418 # ok all the message data has been sent so lets get this
420 fputs($this->smtp_conn, $this->CRLF .
"." . $this->CRLF);
423 $code = substr($rply, 0, 3);
425 if ($this->do_debug >= 2)
427 echo
"SMTP -> FROM SERVER:" . $this->CRLF . $rply;
434 "error" =>
"DATA not accepted from server",
435 "smtp_code" => $code,
436 "smtp_msg" => substr($rply, 4)
439 if ($this->do_debug >= 1)
441 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply .
$this->CRLF;
468 $this->error =
null; # so no confusion is caused
470 if (!$this->connected())
472 $this->error = array(
"error" =>
"Called Expand() without being connected");
476 fputs($this->smtp_conn,
"EXPN " . $name . $this->CRLF);
479 $code = substr($rply, 0, 3);
481 if ($this->do_debug >= 2)
483 echo
"SMTP -> FROM SERVER:" . $this->CRLF . $rply;
490 "error" =>
"EXPN not accepted from server",
491 "smtp_code" => $code,
492 "smtp_msg" => substr($rply, 4)
495 if ($this->do_debug >= 1)
497 echo
"SMTP -> ERROR: " . $this->error[
"error"] .
": " . $rply .
$this->CRLF;
503 # parse the reply and place in our array to return to user
504 $entries = explode($this->CRLF, $rply);
506 while (list(, $l) = @each($entries))
508 $list[] = substr($l, 4);