1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
|
<?php
/*
* smtp.php
*
* @(#) $Header$
*
*/
class smtp_class
{
var $user="";
var $realm="";
var $password="";
var $workstation="";
var $authentication_mechanism="";
var $host_name="";
var $host_port=25;
var $ssl=0;
var $localhost="";
var $timeout=0;
var $data_timeout=0;
var $direct_delivery=0;
var $error="";
var $debug=0;
var $html_debug=0;
var $esmtp=1;
var $esmtp_host="";
var $esmtp_extensions=array();
var $maximum_piped_recipients=100;
var $exclude_address="";
var $getmxrr="GetMXRR";
var $pop3_auth_host="";
var $pop3_auth_port=110;
/* private variables - DO NOT ACCESS */
var $state="Disconnected";
var $connection=0;
var $pending_recipients=0;
var $next_token="";
var $direct_sender="";
var $connected_domain="";
var $result_code;
var $disconnected_error=0;
/* Private methods - DO NOT CALL */
Function Tokenize($string,$separator="")
{
if(!strcmp($separator,""))
{
$separator=$string;
$string=$this->next_token;
}
for($character=0;$character<strlen($separator);$character++)
{
if(GetType($position=strpos($string,$separator[$character]))=="integer")
$found=(IsSet($found) ? min($found,$position) : $position);
}
if(IsSet($found))
{
$this->next_token=substr($string,$found+1);
return(substr($string,0,$found));
}
else
{
$this->next_token="";
return($string);
}
}
Function OutputDebug($message)
{
$message.="\n";
if($this->html_debug)
$message=str_replace("\n","<br />\n",HtmlEntities($message));
echo $message;
flush();
}
Function SetDataAccessError($error)
{
$this->error=$error;
if(function_exists("socket_get_status"))
{
$status=socket_get_status($this->connection);
if($status["timed_out"])
$this->error.=": data access time out";
elseif($status["eof"])
{
$this->error.=": the server disconnected";
$this->disconnected_error=1;
}
}
}
Function GetLine()
{
for($line="";;)
{
if(feof($this->connection))
{
$this->error="reached the end of data while reading from the SMTP server conection";
return("");
}
if(GetType($data=@fgets($this->connection,100))!="string"
|| strlen($data)==0)
{
$this->SetDataAccessError("it was not possible to read line from the SMTP server");
return("");
}
$line.=$data;
$length=strlen($line);
if($length>=2
&& substr($line,$length-2,2)=="\r\n")
{
$line=substr($line,0,$length-2);
if($this->debug)
$this->OutputDebug("S $line");
return($line);
}
}
}
Function PutLine($line)
{
if($this->debug)
$this->OutputDebug("C $line");
if(!@fputs($this->connection,"$line\r\n"))
{
$this->SetDataAccessError("it was not possible to send a line to the SMTP server");
return(0);
}
return(1);
}
Function PutData(&$data)
{
if(strlen($data))
{
if($this->debug)
$this->OutputDebug("C $data");
if(!@fputs($this->connection,$data))
{
$this->SetDataAccessError("it was not possible to send data to the SMTP server");
return(0);
}
}
return(1);
}
Function VerifyResultLines($code,&$responses)
{
$responses=array();
Unset($this->result_code);
while(strlen($line=$this->GetLine($this->connection)))
{
if(IsSet($this->result_code))
{
if(strcmp($this->Tokenize($line," -"),$this->result_code))
{
$this->error=$line;
return(0);
}
}
else
{
$this->result_code=$this->Tokenize($line," -");
if(GetType($code)=="array")
{
for($codes=0;$codes<count($code) && strcmp($this->result_code,$code[$codes]);$codes++);
if($codes>=count($code))
{
$this->error=$line;
return(0);
}
}
else
{
if(strcmp($this->result_code,$code))
{
$this->error=$line;
return(0);
}
}
}
$responses[]=$this->Tokenize("");
if(!strcmp($this->result_code,$this->Tokenize($line," ")))
return(1);
}
return(-1);
}
Function FlushRecipients()
{
if($this->pending_sender)
{
if($this->VerifyResultLines("250",$responses)<=0)
return(0);
$this->pending_sender=0;
}
for(;$this->pending_recipients;$this->pending_recipients--)
{
if($this->VerifyResultLines(array("250","251"),$responses)<=0)
return(0);
}
return(1);
}
Function ConnectToHost($domain, $port, $resolve_message)
{
if($this->ssl)
{
$version=explode(".",function_exists("phpversion") ? phpversion() : "3.0.7");
$php_version=intval($version[0])*1000000+intval($version[1])*1000+intval($version[2]);
if($php_version<4003000)
return("establishing SSL connections requires at least PHP version 4.3.0");
if(!function_exists("extension_loaded")
|| !extension_loaded("openssl"))
return("establishing SSL connections requires the OpenSSL extension enabled");
}
if(ereg('^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$',$domain))
$ip=$domain;
else
{
if($this->debug)
$this->OutputDebug($resolve_message);
if(!strcmp($ip=@gethostbyname($domain),$domain))
return("could not resolve host \"".$domain."\"");
}
if(strlen($this->exclude_address)
&& !strcmp(@gethostbyname($this->exclude_address),$ip))
return("domain \"".$domain."\" resolved to an address excluded to be valid");
if($this->debug)
$this->OutputDebug("Connecting to host address \"".$ip."\" port ".$port."...");
if(($this->connection=($this->timeout ? @fsockopen(($this->ssl ? "ssl://" : "").$ip,$port,$errno,$error,$this->timeout) : @fsockopen(($this->ssl ? "ssl://" : "").$ip,$port))))
return("");
$error=($this->timeout ? strval($error) : "??");
switch($error)
{
case "-3":
return("-3 socket could not be created");
case "-4":
return("-4 dns lookup on hostname \"".$domain."\" failed");
case "-5":
return("-5 connection refused or timed out");
case "-6":
return("-6 fdopen() call failed");
case "-7":
return("-7 setvbuf() call failed");
}
return("could not connect to the host \"".$domain."\": ".$error);
}
Function SASLAuthenticate($mechanisms, $credentials, &$authenticated, &$mechanism)
{
$authenticated=0;
if(!function_exists("class_exists")
|| !class_exists("sasl_client_class"))
{
$this->error="it is not possible to authenticate using the specified mechanism because the SASL library class is not loaded";
return(0);
}
$sasl=new sasl_client_class;
$sasl->SetCredential("user",$credentials["user"]);
$sasl->SetCredential("password",$credentials["password"]);
if(IsSet($credentials["realm"]))
$sasl->SetCredential("realm",$credentials["realm"]);
if(IsSet($credentials["workstation"]))
$sasl->SetCredential("workstation",$credentials["workstation"]);
if(IsSet($credentials["mode"]))
$sasl->SetCredential("mode",$credentials["mode"]);
do
{
$status=$sasl->Start($mechanisms,$message,$interactions);
}
while($status==SASL_INTERACT);
switch($status)
{
case SASL_CONTINUE:
break;
case SASL_NOMECH:
if(strlen($this->authentication_mechanism))
{
$this->error="authenticated mechanism ".$this->authentication_mechanism." may not be used: ".$sasl->error;
return(0);
}
break;
default:
$this->error="Could not start the SASL authentication client: ".$sasl->error;
return(0);
}
if(strlen($mechanism=$sasl->mechanism))
{
if($this->PutLine("AUTH ".$sasl->mechanism.(IsSet($message) ? " ".base64_encode($message) : ""))==0)
{
$this->error="Could not send the AUTH command";
return(0);
}
if(!$this->VerifyResultLines(array("235","334"),$responses))
return(0);
switch($this->result_code)
{
case "235":
$response="";
$authenticated=1;
break;
case "334":
$response=base64_decode($responses[0]);
break;
default:
$this->error="Authentication error: ".$responses[0];
return(0);
}
for(;!$authenticated;)
{
do
{
$status=$sasl->Step($response,$message,$interactions);
}
while($status==SASL_INTERACT);
switch($status)
{
case SASL_CONTINUE:
if($this->PutLine(base64_encode($message))==0)
{
$this->error="Could not send the authentication step message";
return(0);
}
if(!$this->VerifyResultLines(array("235","334"),$responses))
return(0);
switch($this->result_code)
{
case "235":
$response="";
$authenticated=1;
break;
case "334":
$response=base64_decode($responses[0]);
break;
default:
$this->error="Authentication error: ".$responses[0];
return(0);
}
break;
default:
$this->error="Could not process the SASL authentication step: ".$sasl->error;
return(0);
}
}
}
return(1);
}
/* Public methods */
Function Connect($domain="")
{
if(strcmp($this->state,"Disconnected"))
{
$this->error="connection is already established";
return(0);
}
$this->disconnected_error=0;
$this->error=$error="";
$this->esmtp_host="";
$this->esmtp_extensions=array();
$hosts=array();
if($this->direct_delivery)
{
if(strlen($domain)==0)
return(1);
$hosts=$weights=$mxhosts=array();
$getmxrr=$this->getmxrr;
if(function_exists($getmxrr)
&& $getmxrr($domain,$hosts,$weights))
{
for($host=0;$host<count($hosts);$host++)
$mxhosts[$weights[$host]]=$hosts[$host];
KSort($mxhosts);
for(Reset($mxhosts),$host=0;$host<count($mxhosts);Next($mxhosts),$host++)
$hosts[$host]=$mxhosts[Key($mxhosts)];
}
else
{
if(strcmp(@gethostbyname($domain),$domain)!=0)
$hosts[]=$domain;
}
}
else
{
if(strlen($this->host_name))
$hosts[]=$this->host_name;
if(strlen($this->pop3_auth_host))
{
$user=$this->user;
if(strlen($user)==0)
{
$this->error="it was not specified the POP3 authentication user";
return(0);
}
$password=$this->password;
if(strlen($password)==0)
{
$this->error="it was not specified the POP3 authentication password";
return(0);
}
$domain=$this->pop3_auth_host;
$this->error=$this->ConnectToHost($domain, $this->pop3_auth_port, "Resolving POP3 authentication host \"".$domain."\"...");
if(strlen($this->error))
return(0);
if(strlen($response=$this->GetLine())==0)
return(0);
if(strcmp($this->Tokenize($response," "),"+OK"))
{
$this->error="POP3 authentication server greeting was not found";
return(0);
}
if(!$this->PutLine("USER ".$this->user)
|| strlen($response=$this->GetLine())==0)
return(0);
if(strcmp($this->Tokenize($response," "),"+OK"))
{
$this->error="POP3 authentication user was not accepted: ".$this->Tokenize("\r\n");
return(0);
}
if(!$this->PutLine("PASS ".$password)
|| strlen($response=$this->GetLine())==0)
return(0);
if(strcmp($this->Tokenize($response," "),"+OK"))
{
$this->error="POP3 authentication password was not accepted: ".$this->Tokenize("\r\n");
return(0);
}
fclose($this->connection);
$this->connection=0;
}
}
if(count($hosts)==0)
{
$this->error="could not determine the SMTP to connect";
return(0);
}
for($host=0, $error="not connected";strlen($error) && $host<count($hosts);$host++)
{
$domain=$hosts[$host];
$error=$this->ConnectToHost($domain, $this->host_port, "Resolving SMTP server domain \"$domain\"...");
}
if(strlen($error))
{
$this->error=$error;
return(0);
}
$timeout=($this->data_timeout ? $this->data_timeout : $this->timeout);
if($timeout
&& function_exists("socket_set_timeout"))
socket_set_timeout($this->connection,$timeout,0);
if($this->debug)
$this->OutputDebug("Connected to SMTP server \"".$domain."\".");
if(!strcmp($localhost=$this->localhost,"")
&& !strcmp($localhost=getenv("SERVER_NAME"),"")
&& !strcmp($localhost=getenv("HOST"),""))
$localhost="localhost";
$success=0;
if($this->VerifyResultLines("220",$responses)>0)
{
$fallback=1;
if($this->esmtp
|| strlen($this->user))
{
if($this->PutLine("EHLO $localhost"))
{
if(($success_code=$this->VerifyResultLines("250",$responses))>0)
{
$this->esmtp_host=$this->Tokenize($responses[0]," ");
for($response=1;$response<count($responses);$response++)
{
$extension=strtoupper($this->Tokenize($responses[$response]," "));
$this->esmtp_extensions[$extension]=$this->Tokenize("");
}
$success=1;
$fallback=0;
}
else
{
if($success_code==0)
{
$code=$this->Tokenize($this->error," -");
switch($code)
{
case "421":
$fallback=0;
break;
}
}
}
}
else
$fallback=0;
}
if($fallback)
{
if($this->PutLine("HELO $localhost")
&& $this->VerifyResultLines("250",$responses)>0)
$success=1;
}
if($success
&& strlen($this->user)
&& strlen($this->pop3_auth_host)==0)
{
if(!IsSet($this->esmtp_extensions["AUTH"]))
{
$this->error="server does not require authentication";
$success=0;
}
else
{
if(strlen($this->authentication_mechanism))
$mechanisms=array($this->authentication_mechanism);
else
{
$mechanisms=array();
for($authentication=$this->Tokenize($this->esmtp_extensions["AUTH"]," ");strlen($authentication);$authentication=$this->Tokenize(" "))
$mechanisms[]=$authentication;
}
$credentials=array(
"user"=>$this->user,
"password"=>$this->password
);
if(strlen($this->realm))
$credentials["realm"]=$this->realm;
if(strlen($this->workstation))
$credentials["workstation"]=$this->workstation;
$success=$this->SASLAuthenticate($mechanisms,$credentials,$authenticated,$mechanism);
if(!$success
&& !strcmp($mechanism,"PLAIN"))
{
/*
* Author: Russell Robinson, 25 May 2003, http://www.tectite.com/
* Purpose: Try various AUTH PLAIN authentication methods.
*/
$mechanisms=array("PLAIN");
$credentials=array(
"user"=>$this->user,
"password"=>$this->password
);
if(strlen($this->realm))
{
/*
* According to: http://www.sendmail.org/~ca/email/authrealms.html#authpwcheck_method
* some sendmails won't accept the realm, so try again without it
*/
$success=$this->SASLAuthenticate($mechanisms,$credentials,$authenticated,$mechanism);
}
if(!$success)
{
/*
* It was seen an EXIM configuration like this:
* user^password^unused
*/
$credentials["mode"]=SASL_PLAIN_EXIM_DOCUMENTATION_MODE;
$success=$this->SASLAuthenticate($mechanisms,$credentials,$authenticated,$mechanism);
}
if(!$success)
{
/*
* ... though: http://exim.work.de/exim-html-3.20/doc/html/spec_36.html
* specifies: ^user^password
*/
$credentials["mode"]=SASL_PLAIN_EXIM_MODE;
$success=$this->SASLAuthenticate($mechanisms,$credentials,$authenticated,$mechanism);
}
}
if($success
&& strlen($mechanism)==0)
{
$this->error="it is not supported any of the authentication mechanisms required by the server";
$success=0;
}
}
}
}
if($success)
{
$this->state="Connected";
$this->connected_domain=$domain;
}
else
{
fclose($this->connection);
$this->connection=0;
}
return($success);
}
Function MailFrom($sender)
{
if($this->direct_delivery)
{
switch($this->state)
{
case "Disconnected":
$this->direct_sender=$sender;
return(1);
case "Connected":
$sender=$this->direct_sender;
break;
default:
$this->error="direct delivery connection is already established and sender is already set";
return(0);
}
}
else
{
if(strcmp($this->state,"Connected"))
{
$this->error="connection is not in the initial state";
return(0);
}
}
$this->error="";
if(!$this->PutLine("MAIL FROM:<$sender>"))
return(0);
if(!IsSet($this->esmtp_extensions["PIPELINING"])
&& $this->VerifyResultLines("250",$responses)<=0)
return(0);
$this->state="SenderSet";
if(IsSet($this->esmtp_extensions["PIPELINING"]))
$this->pending_sender=1;
$this->pending_recipients=0;
return(1);
}
Function SetRecipient($recipient)
{
if($this->direct_delivery)
{
if(GetType($at=strrpos($recipient,"@"))!="integer")
return("it was not specified a valid direct recipient");
$domain=substr($recipient,$at+1);
switch($this->state)
{
case "Disconnected":
if(!$this->Connect($domain))
return(0);
if(!$this->MailFrom(""))
{
$error=$this->error;
$this->Disconnect();
$this->error=$error;
return(0);
}
break;
case "SenderSet":
case "RecipientSet":
if(strcmp($this->connected_domain,$domain))
{
$this->error="it is not possible to deliver directly to recipients of different domains";
return(0);
}
break;
default:
$this->error="connection is already established and the recipient is already set";
return(0);
}
}
else
{
switch($this->state)
{
case "SenderSet":
case "RecipientSet":
break;
default:
$this->error="connection is not in the recipient setting state";
return(0);
}
}
$this->error="";
if(!$this->PutLine("RCPT TO:<$recipient>"))
return(0);
if(IsSet($this->esmtp_extensions["PIPELINING"]))
{
$this->pending_recipients++;
if($this->pending_recipients>=$this->maximum_piped_recipients)
{
if(!$this->FlushRecipients())
return(0);
}
}
else
{
if($this->VerifyResultLines(array("250","251"),$responses)<=0)
return(0);
}
$this->state="RecipientSet";
return(1);
}
Function StartData()
{
if(strcmp($this->state,"RecipientSet"))
{
$this->error="connection is not in the start sending data state";
return(0);
}
$this->error="";
if(!$this->PutLine("DATA"))
return(0);
if($this->pending_recipients)
{
if(!$this->FlushRecipients())
return(0);
}
if($this->VerifyResultLines("354",$responses)<=0)
return(0);
$this->state="SendingData";
return(1);
}
Function PrepareData(&$data,&$output,$preg=1)
{
if($preg
&& function_exists("preg_replace"))
$output=preg_replace(array("/\n\n|\r\r/","/(^|[^\r])\n/","/\r([^\n]|\$)/D","/(^|\n)\\./"),array("\r\n\r\n","\\1\r\n","\r\n\\1","\\1.."),$data);
else
$output=ereg_replace("(^|\n)\\.","\\1..",ereg_replace("\r([^\n]|\$)","\r\n\\1",ereg_replace("(^|[^\r])\n","\\1\r\n",ereg_replace("\n\n|\r\r","\r\n\r\n",$data))));
}
Function SendData($data)
{
if(strcmp($this->state,"SendingData"))
{
$this->error="connection is not in the sending data state";
return(0);
}
$this->error="";
return($this->PutData($data));
}
Function EndSendingData()
{
if(strcmp($this->state,"SendingData"))
{
$this->error="connection is not in the sending data state";
return(0);
}
$this->error="";
if(!$this->PutLine("\r\n.")
|| $this->VerifyResultLines("250",$responses)<=0)
return(0);
$this->state="Connected";
return(1);
}
Function ResetConnection()
{
switch($this->state)
{
case "Connected":
return(1);
case "SendingData":
$this->error="can not reset the connection while sending data";
return(0);
case "Disconnected":
$this->error="can not reset the connection before it is established";
return(0);
}
$this->error="";
if(!$this->PutLine("RSET")
|| $this->VerifyResultLines("250",$responses)<=0)
return(0);
$this->state="Connected";
return(1);
}
Function Disconnect($quit=1)
{
if(!strcmp($this->state,"Disconnected"))
{
$this->error="it was not previously established a SMTP connection";
return(0);
}
$this->error="";
if(!strcmp($this->state,"Connected")
&& $quit
&& (!$this->PutLine("QUIT")
|| ($this->VerifyResultLines("221",$responses)<=0
&& !$this->disconnected_error)))
return(0);
if($this->disconnected_error)
$this->disconnected_error=0;
else
fclose($this->connection);
$this->connection=0;
$this->state="Disconnected";
if($this->debug)
$this->OutputDebug("Disconnected.");
return(1);
}
Function SendMessage($sender,$recipients,$headers,$body)
{
if(($success=$this->Connect()))
{
if(($success=$this->MailFrom($sender)))
{
for($recipient=0;$recipient<count($recipients);$recipient++)
{
if(!($success=$this->SetRecipient($recipients[$recipient])))
break;
}
if($success
&& ($success=$this->StartData()))
{
for($header_data="",$header=0;$header<count($headers);$header++)
$header_data.=$headers[$header]."\r\n";
if(($success=$this->SendData($header_data."\r\n")))
{
$this->PrepareData($body,$body_data);
$success=$this->SendData($body_data);
}
if($success)
$success=$this->EndSendingData();
}
}
$error=$this->error;
$disconnect_success=$this->Disconnect($success);
if($success)
$success=$disconnect_success;
else
$this->error=$error;
}
return($success);
}
};
?>
|