Browse code

smtp fix + tester

Jimako authored on 2026/07/10 14:03:10
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,1046 @@
1
+<?php
2
+if(!defined("_CHARSET")) exit( );
3
+////////////////////////////////////////////////////
4
+// SMTP - PHP SMTP class
5
+//
6
+// Version 1.02
7
+//
8
+// Define an SMTP class that can be used to connect
9
+// and communicate with any SMTP server. It implements
10
+// all the SMTP functions defined in RFC821 except TURN.
11
+//
12
+// Author: Chris Ryan
13
+//
14
+// License: LGPL, see LICENSE
15
+////////////////////////////////////////////////////
16
+
17
+/**
18
+ * SMTP is rfc 821 compliant and implements all the rfc 821 SMTP
19
+ * commands except TURN which will always return a not implemented
20
+ * error. SMTP also provides some utility methods for sending mail
21
+ * to an SMTP server.
22
+ * @package PHPMailer
23
+ * @author Chris Ryan
24
+ */
25
+class SMTP
26
+{
27
+    /**
28
+     *  SMTP server port
29
+     *  @var int
30
+     */
31
+    var $SMTP_PORT = 25;
32
+    
33
+    /**
34
+     *  SMTP reply line ending
35
+     *  @var string
36
+     */
37
+    var $CRLF = "\r\n";
38
+    
39
+    /**
40
+     *  Sets whether debugging is turned on
41
+     *  @var bool
42
+     */
43
+    var $do_debug;       # the level of debug to perform
44
+
45
+    /**#@+
46
+     * @access private
47
+     */
48
+    var $smtp_conn;      # the socket to the server
49
+    var $error;          # error if any on the last call
50
+    var $helo_rply;      # the reply the server sent to us for HELO
51
+    /**#@-*/
52
+
53
+    /**
54
+     * Initialize the class so that the data is in a known state.
55
+     * @access public
56
+     * @return void
57
+     */
58
+    function SMTP() {
59
+        $this->smtp_conn = 0;
60
+        $this->error = null;
61
+        $this->helo_rply = null;
62
+
63
+        $this->do_debug = 0;
64
+    }
65
+
66
+    /*************************************************************
67
+     *                    CONNECTION FUNCTIONS                  *
68
+     ***********************************************************/
69
+
70
+    /**
71
+     * Connect to the server specified on the port specified.
72
+     * If the port is not specified use the default SMTP_PORT.
73
+     * If tval is specified then a connection will try and be
74
+     * established with the server for that number of seconds.
75
+     * If tval is not specified the default is 30 seconds to
76
+     * try on the connection.
77
+     *
78
+     * SMTP CODE SUCCESS: 220
79
+     * SMTP CODE FAILURE: 421
80
+     * @access public
81
+     * @return bool
82
+     */
83
+    function Connect($host,$port=0,$tval=30) {
84
+        # set the error val to null so there is no confusion
85
+        $this->error = null;
86
+
87
+        # make sure we are __not__ connected
88
+        if($this->connected()) {
89
+            # ok we are connected! what should we do?
90
+            # for now we will just give an error saying we
91
+            # are already connected
92
+            $this->error =
93
+                array("error" => "Already connected to a server");
94
+            return false;
95
+        }
96
+
97
+        if(empty($port)) {
98
+            $port = $this->SMTP_PORT;
99
+        }
100
+
101
+        #connect to the smtp server
102
+        $this->smtp_conn = fsockopen($host,    # the host of the server
103
+                                     $port,    # the port to use
104
+                                     $errno,   # error number if any
105
+                                     $errstr,  # error message if any
106
+                                     $tval);   # give up after ? secs
107
+        # verify we connected properly
108
+        if(empty($this->smtp_conn)) {
109
+            $this->error = array("error" => "Failed to connect to server",
110
+                                 "errno" => $errno,
111
+                                 "errstr" => $errstr);
112
+            if($this->do_debug >= 1) {
113
+                echo "SMTP -> ERROR: " . $this->error["error"] .
114
+                         ": $errstr ($errno)" . $this->CRLF;
115
+            }
116
+            return false;
117
+        }
118
+
119
+        # sometimes the SMTP server takes a little longer to respond
120
+        # so we will give it a longer timeout for the first read
121
+        // Windows still does not have support for this timeout function
122
+        if(substr(PHP_OS, 0, 3) != "WIN")
123
+           socket_set_timeout($this->smtp_conn, $tval, 0);
124
+
125
+        # get any announcement stuff
126
+        $announce = $this->get_lines();
127
+
128
+        # set the timeout  of any socket functions at 1/10 of a second
129
+        //if(function_exists("socket_set_timeout"))
130
+        //   socket_set_timeout($this->smtp_conn, 0, 100000);
131
+
132
+        if($this->do_debug >= 2) {
133
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $announce;
134
+        }
135
+
136
+        return true;
137
+    }
138
+
139
+    /**
140
+     * Performs SMTP authentication.  Must be run after running the
141
+     * Hello() method.  Returns true if successfully authenticated.
142
+     * @access public
143
+     * @return bool
144
+     */
145
+    function Authenticate($username, $password) {
146
+        // Start authentication
147
+        fputs($this->smtp_conn,"AUTH LOGIN" . $this->CRLF);
148
+
149
+        $rply = $this->get_lines();
150
+        $code = substr($rply,0,3);
151
+
152
+        if($code != 334) {
153
+            $this->error =
154
+                array("error" => "AUTH not accepted from server",
155
+                      "smtp_code" => $code,
156
+                      "smtp_msg" => substr($rply,4));
157
+            if($this->do_debug >= 1) {
158
+                echo "SMTP -> ERROR: " . $this->error["error"] .
159
+                         ": " . $rply . $this->CRLF;
160
+            }
161
+            return false;
162
+        }
163
+
164
+        // Send encoded username
165
+        fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);
166
+
167
+        $rply = $this->get_lines();
168
+        $code = substr($rply,0,3);
169
+
170
+        if($code != 334) {
171
+            $this->error =
172
+                array("error" => "Username not accepted from server",
173
+                      "smtp_code" => $code,
174
+                      "smtp_msg" => substr($rply,4));
175
+            if($this->do_debug >= 1) {
176
+                echo "SMTP -> ERROR: " . $this->error["error"] .
177
+                         ": " . $rply . $this->CRLF;
178
+            }
179
+            return false;
180
+        }
181
+
182
+        // Send encoded password
183
+        fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);
184
+
185
+        $rply = $this->get_lines();
186
+        $code = substr($rply,0,3);
187
+
188
+        if($code != 235) {
189
+            $this->error =
190
+                array("error" => "Password not accepted from server",
191
+                      "smtp_code" => $code,
192
+                      "smtp_msg" => substr($rply,4));
193
+            if($this->do_debug >= 1) {
194
+                echo "SMTP -> ERROR: " . $this->error["error"] .
195
+                         ": " . $rply . $this->CRLF;
196
+            }
197
+            return false;
198
+        }
199
+
200
+        return true;
201
+    }
202
+
203
+    /**
204
+     * Returns true if connected to a server otherwise false
205
+     * @access private
206
+     * @return bool
207
+     */
208
+    function Connected() {
209
+        if(!empty($this->smtp_conn)) {
210
+            $sock_status = socket_get_status($this->smtp_conn);
211
+            if($sock_status["eof"]) {
212
+                # hmm this is an odd situation... the socket is
213
+                # valid but we aren't connected anymore
214
+                if($this->do_debug >= 1) {
215
+                    echo "SMTP -> NOTICE:" . $this->CRLF .
216
+                         "EOF caught while checking if connected";
217
+                }
218
+                $this->Close();
219
+                return false;
220
+            }
221
+            return true; # everything looks good
222
+        }
223
+        return false;
224
+    }
225
+
226
+    /**
227
+     * Closes the socket and cleans up the state of the class.
228
+     * It is not considered good to use this function without
229
+     * first trying to use QUIT.
230
+     * @access public
231
+     * @return void
232
+     */
233
+    function Close() {
234
+        $this->error = null; # so there is no confusion
235
+        $this->helo_rply = null;
236
+        if(!empty($this->smtp_conn)) {
237
+            # close the connection and cleanup
238
+            fclose($this->smtp_conn);
239
+            $this->smtp_conn = 0;
240
+        }
241
+    }
242
+
243
+
244
+    /***************************************************************
245
+     *                        SMTP COMMANDS                       *
246
+     *************************************************************/
247
+
248
+    /**
249
+     * Issues a data command and sends the msg_data to the server
250
+     * finializing the mail transaction. $msg_data is the message
251
+     * that is to be send with the headers. Each header needs to be
252
+     * on a single line followed by a <CRLF> with the message headers
253
+     * and the message body being seperated by and additional <CRLF>.
254
+     *
255
+     * Implements rfc 821: DATA <CRLF>
256
+     *
257
+     * SMTP CODE INTERMEDIATE: 354
258
+     *     [data]
259
+     *     <CRLF>.<CRLF>
260
+     *     SMTP CODE SUCCESS: 250
261
+     *     SMTP CODE FAILURE: 552,554,451,452
262
+     * SMTP CODE FAILURE: 451,554
263
+     * SMTP CODE ERROR  : 500,501,503,421
264
+     * @access public
265
+     * @return bool
266
+     */
267
+    function Data($msg_data) {
268
+        $this->error = null; # so no confusion is caused
269
+
270
+        if(!$this->connected()) {
271
+            $this->error = array(
272
+                    "error" => "Called Data() without being connected");
273
+            return false;
274
+        }
275
+
276
+        fputs($this->smtp_conn,"DATA" . $this->CRLF);
277
+
278
+        $rply = $this->get_lines();
279
+        $code = substr($rply,0,3);
280
+
281
+        if($this->do_debug >= 2) {
282
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
283
+        }
284
+
285
+        if($code != 354) {
286
+            $this->error =
287
+                array("error" => "DATA command not accepted from server",
288
+                      "smtp_code" => $code,
289
+                      "smtp_msg" => substr($rply,4));
290
+            if($this->do_debug >= 1) {
291
+                echo "SMTP -> ERROR: " . $this->error["error"] .
292
+                         ": " . $rply . $this->CRLF;
293
+            }
294
+            return false;
295
+        }
296
+
297
+        # the server is ready to accept data!
298
+        # according to rfc 821 we should not send more than 1000
299
+        # including the CRLF
300
+        # characters on a single line so we will break the data up
301
+        # into lines by \r and/or \n then if needed we will break
302
+        # each of those into smaller lines to fit within the limit.
303
+        # in addition we will be looking for lines that start with
304
+        # a period '.' and append and additional period '.' to that
305
+        # line. NOTE: this does not count towards are limit.
306
+
307
+        # normalize the line breaks so we know the explode works
308
+        $msg_data = str_replace("\r\n","\n",$msg_data);
309
+        $msg_data = str_replace("\r","\n",$msg_data);
310
+        $lines = explode("\n",$msg_data);
311
+
312
+        # we need to find a good way to determine is headers are
313
+        # in the msg_data or if it is a straight msg body
314
+        # currently I'm assuming rfc 822 definitions of msg headers
315
+        # and if the first field of the first line (':' sperated)
316
+        # does not contain a space then it _should_ be a header
317
+        # and we can process all lines before a blank "" line as
318
+        # headers.
319
+        $field = substr($lines[0],0,strpos($lines[0],":"));
320
+        $in_headers = false;
321
+        if(!empty($field) && !strstr($field," ")) {
322
+            $in_headers = true;
323
+        }
324
+
325
+        $max_line_length = 998; # used below; set here for ease in change
326
+
327
+        while(list(,$line) = @each($lines)) {
328
+            $lines_out = null;
329
+            if($line == "" && $in_headers) {
330
+                $in_headers = false;
331
+            }
332
+            # ok we need to break this line up into several
333
+            # smaller lines
334
+            while(strlen($line) > $max_line_length) {
335
+                $pos = strrpos(substr($line,0,$max_line_length)," ");
336
+
337
+                # Patch to fix DOS attack
338
+                if(!$pos) {
339
+                    $pos = $max_line_length - 1;
340
+                }
341
+
342
+                $lines_out[] = substr($line,0,$pos);
343
+                $line = substr($line,$pos + 1);
344
+                # if we are processing headers we need to
345
+                # add a LWSP-char to the front of the new line
346
+                # rfc 822 on long msg headers
347
+                if($in_headers) {
348
+                    $line = "\t" . $line;
349
+                }
350
+            }
351
+            $lines_out[] = $line;
352
+
353
+            # now send the lines to the server
354
+            while(list(,$line_out) = @each($lines_out)) {
355
+                if(strlen($line_out) > 0)
356
+                {
357
+                    if(substr($line_out, 0, 1) == ".") {
358
+                        $line_out = "." . $line_out;
359
+                    }
360
+                }
361
+                fputs($this->smtp_conn,$line_out . $this->CRLF);
362
+            }
363
+        }
364
+
365
+        # ok all the message data has been sent so lets get this
366
+        # over with aleady
367
+        fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);
368
+
369
+        $rply = $this->get_lines();
370
+        $code = substr($rply,0,3);
371
+
372
+        if($this->do_debug >= 2) {
373
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
374
+        }
375
+
376
+        if($code != 250) {
377
+            $this->error =
378
+                array("error" => "DATA not accepted from server",
379
+                      "smtp_code" => $code,
380
+                      "smtp_msg" => substr($rply,4));
381
+            if($this->do_debug >= 1) {
382
+                echo "SMTP -> ERROR: " . $this->error["error"] .
383
+                         ": " . $rply . $this->CRLF;
384
+            }
385
+            return false;
386
+        }
387
+        return true;
388
+    }
389
+
390
+    /**
391
+     * Expand takes the name and asks the server to list all the
392
+     * people who are members of the _list_. Expand will return
393
+     * back and array of the result or false if an error occurs.
394
+     * Each value in the array returned has the format of:
395
+     *     [ <full-name> <sp> ] <path>
396
+     * The definition of <path> is defined in rfc 821
397
+     *
398
+     * Implements rfc 821: EXPN <SP> <string> <CRLF>
399
+     *
400
+     * SMTP CODE SUCCESS: 250
401
+     * SMTP CODE FAILURE: 550
402
+     * SMTP CODE ERROR  : 500,501,502,504,421
403
+     * @access public
404
+     * @return string array
405
+     */
406
+    function Expand($name) {
407
+        $this->error = null; # so no confusion is caused
408
+
409
+        if(!$this->connected()) {
410
+            $this->error = array(
411
+                    "error" => "Called Expand() without being connected");
412
+            return false;
413
+        }
414
+
415
+        fputs($this->smtp_conn,"EXPN " . $name . $this->CRLF);
416
+
417
+        $rply = $this->get_lines();
418
+        $code = substr($rply,0,3);
419
+
420
+        if($this->do_debug >= 2) {
421
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
422
+        }
423
+
424
+        if($code != 250) {
425
+            $this->error =
426
+                array("error" => "EXPN not accepted from server",
427
+                      "smtp_code" => $code,
428
+                      "smtp_msg" => substr($rply,4));
429
+            if($this->do_debug >= 1) {
430
+                echo "SMTP -> ERROR: " . $this->error["error"] .
431
+                         ": " . $rply . $this->CRLF;
432
+            }
433
+            return false;
434
+        }
435
+
436
+        # parse the reply and place in our array to return to user
437
+        $entries = explode($this->CRLF,$rply);
438
+        while(list(,$l) = @each($entries)) {
439
+            $list[] = substr($l,4);
440
+        }
441
+
442
+        return $list;
443
+    }
444
+
445
+    /**
446
+     * Sends the HELO command to the smtp server.
447
+     * This makes sure that we and the server are in
448
+     * the same known state.
449
+     *
450
+     * Implements from rfc 821: HELO <SP> <domain> <CRLF>
451
+     *
452
+     * SMTP CODE SUCCESS: 250
453
+     * SMTP CODE ERROR  : 500, 501, 504, 421
454
+     * @access public
455
+     * @return bool
456
+     */
457
+    function Hello($host="") {
458
+        $this->error = null; # so no confusion is caused
459
+
460
+        if(!$this->connected()) {
461
+            $this->error = array(
462
+                    "error" => "Called Hello() without being connected");
463
+            return false;
464
+        }
465
+
466
+        # if a hostname for the HELO wasn't specified determine
467
+        # a suitable one to send
468
+        if(empty($host)) {
469
+            # we need to determine some sort of appopiate default
470
+            # to send to the server
471
+            $host = "localhost";
472
+        }
473
+
474
+        // Send extended hello first (RFC 2821)
475
+        if(!$this->SendHello("EHLO", $host))
476
+        {
477
+            if(!$this->SendHello("HELO", $host))
478
+                return false;
479
+        }
480
+
481
+        return true;
482
+    }
483
+
484
+    /**
485
+     * Sends a HELO/EHLO command.
486
+     * @access private
487
+     * @return bool
488
+     */
489
+    function SendHello($hello, $host) {
490
+        fputs($this->smtp_conn, $hello . " " . $host . $this->CRLF);
491
+
492
+        $rply = $this->get_lines();
493
+        $code = substr($rply,0,3);
494
+
495
+        if($this->do_debug >= 2) {
496
+            echo "SMTP -> FROM SERVER: " . $this->CRLF . $rply;
497
+        }
498
+
499
+        if($code != 250) {
500
+            $this->error =
501
+                array("error" => $hello . " not accepted from server",
502
+                      "smtp_code" => $code,
503
+                      "smtp_msg" => substr($rply,4));
504
+            if($this->do_debug >= 1) {
505
+                echo "SMTP -> ERROR: " . $this->error["error"] .
506
+                         ": " . $rply . $this->CRLF;
507
+            }
508
+            return false;
509
+        }
510
+
511
+        $this->helo_rply = $rply;
512
+        
513
+        return true;
514
+    }
515
+
516
+    /**
517
+     * Gets help information on the keyword specified. If the keyword
518
+     * is not specified then returns generic help, ussually contianing
519
+     * A list of keywords that help is available on. This function
520
+     * returns the results back to the user. It is up to the user to
521
+     * handle the returned data. If an error occurs then false is
522
+     * returned with $this->error set appropiately.
523
+     *
524
+     * Implements rfc 821: HELP [ <SP> <string> ] <CRLF>
525
+     *
526
+     * SMTP CODE SUCCESS: 211,214
527
+     * SMTP CODE ERROR  : 500,501,502,504,421
528
+     * @access public
529
+     * @return string
530
+     */
531
+    function Help($keyword="") {
532
+        $this->error = null; # to avoid confusion
533
+
534
+        if(!$this->connected()) {
535
+            $this->error = array(
536
+                    "error" => "Called Help() without being connected");
537
+            return false;
538
+        }
539
+
540
+        $extra = "";
541
+        if(!empty($keyword)) {
542
+            $extra = " " . $keyword;
543
+        }
544
+
545
+        fputs($this->smtp_conn,"HELP" . $extra . $this->CRLF);
546
+
547
+        $rply = $this->get_lines();
548
+        $code = substr($rply,0,3);
549
+
550
+        if($this->do_debug >= 2) {
551
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
552
+        }
553
+
554
+        if($code != 211 && $code != 214) {
555
+            $this->error =
556
+                array("error" => "HELP not accepted from server",
557
+                      "smtp_code" => $code,
558
+                      "smtp_msg" => substr($rply,4));
559
+            if($this->do_debug >= 1) {
560
+                echo "SMTP -> ERROR: " . $this->error["error"] .
561
+                         ": " . $rply . $this->CRLF;
562
+            }
563
+            return false;
564
+        }
565
+
566
+        return $rply;
567
+    }
568
+
569
+    /**
570
+     * Starts a mail transaction from the email address specified in
571
+     * $from. Returns true if successful or false otherwise. If True
572
+     * the mail transaction is started and then one or more Recipient
573
+     * commands may be called followed by a Data command.
574
+     *
575
+     * Implements rfc 821: MAIL <SP> FROM:<reverse-path> <CRLF>
576
+     *
577
+     * SMTP CODE SUCCESS: 250
578
+     * SMTP CODE SUCCESS: 552,451,452
579
+     * SMTP CODE SUCCESS: 500,501,421
580
+     * @access public
581
+     * @return bool
582
+     */
583
+    function Mail($from) {
584
+        $this->error = null; # so no confusion is caused
585
+
586
+        if(!$this->connected()) {
587
+            $this->error = array(
588
+                    "error" => "Called Mail() without being connected");
589
+            return false;
590
+        }
591
+
592
+        fputs($this->smtp_conn,"MAIL FROM:<" . $from . ">" . $this->CRLF);
593
+
594
+        $rply = $this->get_lines();
595
+        $code = substr($rply,0,3);
596
+
597
+        if($this->do_debug >= 2) {
598
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
599
+        }
600
+
601
+        if($code != 250) {
602
+            $this->error =
603
+                array("error" => "MAIL not accepted from server",
604
+                      "smtp_code" => $code,
605
+                      "smtp_msg" => substr($rply,4));
606
+            if($this->do_debug >= 1) {
607
+                echo "SMTP -> ERROR: " . $this->error["error"] .
608
+                         ": " . $rply . $this->CRLF;
609
+            }
610
+            return false;
611
+        }
612
+        return true;
613
+    }
614
+
615
+    /**
616
+     * Sends the command NOOP to the SMTP server.
617
+     *
618
+     * Implements from rfc 821: NOOP <CRLF>
619
+     *
620
+     * SMTP CODE SUCCESS: 250
621
+     * SMTP CODE ERROR  : 500, 421
622
+     * @access public
623
+     * @return bool
624
+     */
625
+    function Noop() {
626
+        $this->error = null; # so no confusion is caused
627
+
628
+        if(!$this->connected()) {
629
+            $this->error = array(
630
+                    "error" => "Called Noop() without being connected");
631
+            return false;
632
+        }
633
+
634
+        fputs($this->smtp_conn,"NOOP" . $this->CRLF);
635
+
636
+        $rply = $this->get_lines();
637
+        $code = substr($rply,0,3);
638
+
639
+        if($this->do_debug >= 2) {
640
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
641
+        }
642
+
643
+        if($code != 250) {
644
+            $this->error =
645
+                array("error" => "NOOP not accepted from server",
646
+                      "smtp_code" => $code,
647
+                      "smtp_msg" => substr($rply,4));
648
+            if($this->do_debug >= 1) {
649
+                echo "SMTP -> ERROR: " . $this->error["error"] .
650
+                         ": " . $rply . $this->CRLF;
651
+            }
652
+            return false;
653
+        }
654
+        return true;
655
+    }
656
+
657
+    /**
658
+     * Sends the quit command to the server and then closes the socket
659
+     * if there is no error or the $close_on_error argument is true.
660
+     *
661
+     * Implements from rfc 821: QUIT <CRLF>
662
+     *
663
+     * SMTP CODE SUCCESS: 221
664
+     * SMTP CODE ERROR  : 500
665
+     * @access public
666
+     * @return bool
667
+     */
668
+    function Quit($close_on_error=true) {
669
+        $this->error = null; # so there is no confusion
670
+
671
+        if(!$this->connected()) {
672
+            $this->error = array(
673
+                    "error" => "Called Quit() without being connected");
674
+            return false;
675
+        }
676
+
677
+        # send the quit command to the server
678
+        fputs($this->smtp_conn,"quit" . $this->CRLF);
679
+
680
+        # get any good-bye messages
681
+        $byemsg = $this->get_lines();
682
+
683
+        if($this->do_debug >= 2) {
684
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $byemsg;
685
+        }
686
+
687
+        $rval = true;
688
+        $e = null;
689
+
690
+        $code = substr($byemsg,0,3);
691
+        if($code != 221) {
692
+            # use e as a tmp var cause Close will overwrite $this->error
693
+            $e = array("error" => "SMTP server rejected quit command",
694
+                       "smtp_code" => $code,
695
+                       "smtp_rply" => substr($byemsg,4));
696
+            $rval = false;
697
+            if($this->do_debug >= 1) {
698
+                echo "SMTP -> ERROR: " . $e["error"] . ": " .
699
+                         $byemsg . $this->CRLF;
700
+            }
701
+        }
702
+
703
+        if(empty($e) || $close_on_error) {
704
+            $this->Close();
705
+        }
706
+
707
+        return $rval;
708
+    }
709
+
710
+    /**
711
+     * Sends the command RCPT to the SMTP server with the TO: argument of $to.
712
+     * Returns true if the recipient was accepted false if it was rejected.
713
+     *
714
+     * Implements from rfc 821: RCPT <SP> TO:<forward-path> <CRLF>
715
+     *
716
+     * SMTP CODE SUCCESS: 250,251
717
+     * SMTP CODE FAILURE: 550,551,552,553,450,451,452
718
+     * SMTP CODE ERROR  : 500,501,503,421
719
+     * @access public
720
+     * @return bool
721
+     */
722
+    function Recipient($to) {
723
+        $this->error = null; # so no confusion is caused
724
+
725
+        if(!$this->connected()) {
726
+            $this->error = array(
727
+                    "error" => "Called Recipient() without being connected");
728
+            return false;
729
+        }
730
+
731
+        fputs($this->smtp_conn,"RCPT TO:<" . $to . ">" . $this->CRLF);
732
+
733
+        $rply = $this->get_lines();
734
+        $code = substr($rply,0,3);
735
+
736
+        if($this->do_debug >= 2) {
737
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
738
+        }
739
+
740
+        if($code != 250 && $code != 251) {
741
+            $this->error =
742
+                array("error" => "RCPT not accepted from server",
743
+                      "smtp_code" => $code,
744
+                      "smtp_msg" => substr($rply,4));
745
+            if($this->do_debug >= 1) {
746
+                echo "SMTP -> ERROR: " . $this->error["error"] .
747
+                         ": " . $rply . $this->CRLF;
748
+            }
749
+            return false;
750
+        }
751
+        return true;
752
+    }
753
+
754
+    /**
755
+     * Sends the RSET command to abort and transaction that is
756
+     * currently in progress. Returns true if successful false
757
+     * otherwise.
758
+     *
759
+     * Implements rfc 821: RSET <CRLF>
760
+     *
761
+     * SMTP CODE SUCCESS: 250
762
+     * SMTP CODE ERROR  : 500,501,504,421
763
+     * @access public
764
+     * @return bool
765
+     */
766
+    function Reset() {
767
+        $this->error = null; # so no confusion is caused
768
+
769
+        if(!$this->connected()) {
770
+            $this->error = array(
771
+                    "error" => "Called Reset() without being connected");
772
+            return false;
773
+        }
774
+
775
+        fputs($this->smtp_conn,"RSET" . $this->CRLF);
776
+
777
+        $rply = $this->get_lines();
778
+        $code = substr($rply,0,3);
779
+
780
+        if($this->do_debug >= 2) {
781
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
782
+        }
783
+
784
+        if($code != 250) {
785
+            $this->error =
786
+                array("error" => "RSET failed",
787
+                      "smtp_code" => $code,
788
+                      "smtp_msg" => substr($rply,4));
789
+            if($this->do_debug >= 1) {
790
+                echo "SMTP -> ERROR: " . $this->error["error"] .
791
+                         ": " . $rply . $this->CRLF;
792
+            }
793
+            return false;
794
+        }
795
+
796
+        return true;
797
+    }
798
+
799
+    /**
800
+     * Starts a mail transaction from the email address specified in
801
+     * $from. Returns true if successful or false otherwise. If True
802
+     * the mail transaction is started and then one or more Recipient
803
+     * commands may be called followed by a Data command. This command
804
+     * will send the message to the users terminal if they are logged
805
+     * in.
806
+     *
807
+     * Implements rfc 821: SEND <SP> FROM:<reverse-path> <CRLF>
808
+     *
809
+     * SMTP CODE SUCCESS: 250
810
+     * SMTP CODE SUCCESS: 552,451,452
811
+     * SMTP CODE SUCCESS: 500,501,502,421
812
+     * @access public
813
+     * @return bool
814
+     */
815
+    function Send($from) {
816
+        $this->error = null; # so no confusion is caused
817
+
818
+        if(!$this->connected()) {
819
+            $this->error = array(
820
+                    "error" => "Called Send() without being connected");
821
+            return false;
822
+        }
823
+
824
+        fputs($this->smtp_conn,"SEND FROM:" . $from . $this->CRLF);
825
+
826
+        $rply = $this->get_lines();
827
+        $code = substr($rply,0,3);
828
+
829
+        if($this->do_debug >= 2) {
830
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
831
+        }
832
+
833
+        if($code != 250) {
834
+            $this->error =
835
+                array("error" => "SEND not accepted from server",
836
+                      "smtp_code" => $code,
837
+                      "smtp_msg" => substr($rply,4));
838
+            if($this->do_debug >= 1) {
839
+                echo "SMTP -> ERROR: " . $this->error["error"] .
840
+                         ": " . $rply . $this->CRLF;
841
+            }
842
+            return false;
843
+        }
844
+        return true;
845
+    }
846
+
847
+    /**
848
+     * Starts a mail transaction from the email address specified in
849
+     * $from. Returns true if successful or false otherwise. If True
850
+     * the mail transaction is started and then one or more Recipient
851
+     * commands may be called followed by a Data command. This command
852
+     * will send the message to the users terminal if they are logged
853
+     * in and send them an email.
854
+     *
855
+     * Implements rfc 821: SAML <SP> FROM:<reverse-path> <CRLF>
856
+     *
857
+     * SMTP CODE SUCCESS: 250
858
+     * SMTP CODE SUCCESS: 552,451,452
859
+     * SMTP CODE SUCCESS: 500,501,502,421
860
+     * @access public
861
+     * @return bool
862
+     */
863
+    function SendAndMail($from) {
864
+        $this->error = null; # so no confusion is caused
865
+
866
+        if(!$this->connected()) {
867
+            $this->error = array(
868
+                "error" => "Called SendAndMail() without being connected");
869
+            return false;
870
+        }
871
+
872
+        fputs($this->smtp_conn,"SAML FROM:" . $from . $this->CRLF);
873
+
874
+        $rply = $this->get_lines();
875
+        $code = substr($rply,0,3);
876
+
877
+        if($this->do_debug >= 2) {
878
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
879
+        }
880
+
881
+        if($code != 250) {
882
+            $this->error =
883
+                array("error" => "SAML not accepted from server",
884
+                      "smtp_code" => $code,
885
+                      "smtp_msg" => substr($rply,4));
886
+            if($this->do_debug >= 1) {
887
+                echo "SMTP -> ERROR: " . $this->error["error"] .
888
+                         ": " . $rply . $this->CRLF;
889
+            }
890
+            return false;
891
+        }
892
+        return true;
893
+    }
894
+
895
+    /**
896
+     * Starts a mail transaction from the email address specified in
897
+     * $from. Returns true if successful or false otherwise. If True
898
+     * the mail transaction is started and then one or more Recipient
899
+     * commands may be called followed by a Data command. This command
900
+     * will send the message to the users terminal if they are logged
901
+     * in or mail it to them if they are not.
902
+     *
903
+     * Implements rfc 821: SOML <SP> FROM:<reverse-path> <CRLF>
904
+     *
905
+     * SMTP CODE SUCCESS: 250
906
+     * SMTP CODE SUCCESS: 552,451,452
907
+     * SMTP CODE SUCCESS: 500,501,502,421
908
+     * @access public
909
+     * @return bool
910
+     */
911
+    function SendOrMail($from) {
912
+        $this->error = null; # so no confusion is caused
913
+
914
+        if(!$this->connected()) {
915
+            $this->error = array(
916
+                "error" => "Called SendOrMail() without being connected");
917
+            return false;
918
+        }
919
+
920
+        fputs($this->smtp_conn,"SOML FROM:" . $from . $this->CRLF);
921
+
922
+        $rply = $this->get_lines();
923
+        $code = substr($rply,0,3);
924
+
925
+        if($this->do_debug >= 2) {
926
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
927
+        }
928
+
929
+        if($code != 250) {
930
+            $this->error =
931
+                array("error" => "SOML not accepted from server",
932
+                      "smtp_code" => $code,
933
+                      "smtp_msg" => substr($rply,4));
934
+            if($this->do_debug >= 1) {
935
+                echo "SMTP -> ERROR: " . $this->error["error"] .
936
+                         ": " . $rply . $this->CRLF;
937
+            }
938
+            return false;
939
+        }
940
+        return true;
941
+    }
942
+
943
+    /**
944
+     * This is an optional command for SMTP that this class does not
945
+     * support. This method is here to make the RFC821 Definition
946
+     * complete for this class and __may__ be implimented in the future
947
+     *
948
+     * Implements from rfc 821: TURN <CRLF>
949
+     *
950
+     * SMTP CODE SUCCESS: 250
951
+     * SMTP CODE FAILURE: 502
952
+     * SMTP CODE ERROR  : 500, 503
953
+     * @access public
954
+     * @return bool
955
+     */
956
+    function Turn() {
957
+        $this->error = array("error" => "This method, TURN, of the SMTP ".
958
+                                        "is not implemented");
959
+        if($this->do_debug >= 1) {
960
+            echo "SMTP -> NOTICE: " . $this->error["error"] . $this->CRLF;
961
+        }
962
+        return false;
963
+    }
964
+
965
+    /**
966
+     * Verifies that the name is recognized by the server.
967
+     * Returns false if the name could not be verified otherwise
968
+     * the response from the server is returned.
969
+     *
970
+     * Implements rfc 821: VRFY <SP> <string> <CRLF>
971
+     *
972
+     * SMTP CODE SUCCESS: 250,251
973
+     * SMTP CODE FAILURE: 550,551,553
974
+     * SMTP CODE ERROR  : 500,501,502,421
975
+     * @access public
976
+     * @return int
977
+     */
978
+    function Verify($name) {
979
+        $this->error = null; # so no confusion is caused
980
+
981
+        if(!$this->connected()) {
982
+            $this->error = array(
983
+                    "error" => "Called Verify() without being connected");
984
+            return false;
985
+        }
986
+
987
+        fputs($this->smtp_conn,"VRFY " . $name . $this->CRLF);
988
+
989
+        $rply = $this->get_lines();
990
+        $code = substr($rply,0,3);
991
+
992
+        if($this->do_debug >= 2) {
993
+            echo "SMTP -> FROM SERVER:" . $this->CRLF . $rply;
994
+        }
995
+
996
+        if($code != 250 && $code != 251) {
997
+            $this->error =
998
+                array("error" => "VRFY failed on name '$name'",
999
+                      "smtp_code" => $code,
1000
+                      "smtp_msg" => substr($rply,4));
1001
+            if($this->do_debug >= 1) {
1002
+                echo "SMTP -> ERROR: " . $this->error["error"] .
1003
+                         ": " . $rply . $this->CRLF;
1004
+            }
1005
+            return false;
1006
+        }
1007
+        return $rply;
1008
+    }
1009
+
1010
+    /*******************************************************************
1011
+     *                       INTERNAL FUNCTIONS                       *
1012
+     ******************************************************************/
1013
+
1014
+    /**
1015
+     * Read in as many lines as possible
1016
+     * either before eof or socket timeout occurs on the operation.
1017
+     * With SMTP we can tell if we have more lines to read if the
1018
+     * 4th character is '-' symbol. If it is a space then we don't
1019
+     * need to read anything else.
1020
+     * @access private
1021
+     * @return string
1022
+     */
1023
+    function get_lines() {
1024
+        $data = "";
1025
+        while($str = fgets($this->smtp_conn,515)) {
1026
+            if($this->do_debug >= 4) {
1027
+                echo "SMTP -> get_lines(): \$data was \"$data\"" .
1028
+                         $this->CRLF;
1029
+                echo "SMTP -> get_lines(): \$str is \"$str\"" .
1030
+                         $this->CRLF;
1031
+            }
1032
+            $data .= $str;
1033
+            if($this->do_debug >= 4) {
1034
+                echo "SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF;
1035
+            }
1036
+            # if the 4th character is a space then we are done reading
1037
+            # so just break the loop
1038
+            if(substr($str,3,1) == " ") { break; }
1039
+        }
1040
+        return $data;
1041
+    }
1042
+
1043
+}
1044
+
1045
+
1046
+ ?>
0 1047
\ No newline at end of file