summaryrefslogtreecommitdiffstats
path: root/src/etc/inc/smtp.inc
blob: 48872d8050ac06348f220de3c87bd76ab1c7955b (plain)
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
<?php
/*
 * smtp.php
 *
 * @(#) $Header: /opt2/ena/metal/smtp/smtp.php,v 1.50 2016/01/19 00:16:06 mlemos Exp $
 *
 */

/*
{metadocument}<?xml version="1.0" encoding="ISO-8859-1"?>
<class>

	<package>net.manuellemos.smtp</package>

	<version>@(#) $Id: smtp.php,v 1.50 2016/01/19 00:16:06 mlemos Exp $</version>
	<copyright>Copyright (C) Manuel Lemos 1999-2011</copyright>
	<title>Sending e-mail messages via SMTP protocol</title>
	<author>Manuel Lemos</author>
	<authoraddress>mlemos-at-acm.org</authoraddress>

	<documentation>
		<idiom>en</idiom>
		<purpose>Sending e-mail messages via SMTP protocol</purpose>
		<translation>If you are interested in translating the documentation of
			this class to your own idiom, please <link>
				<data>contact the author</data>
				<url>mailto:<getclassproperty>authoraddress</getclassproperty></url>
			</link>.</translation>
		<support>Technical support for using this class may be obtained in the
			<tt>smtpclass</tt> support forum. Just go to the support forum pages
			page to browse the forum archives and post support request
			messages:<paragraphbreak />
			<link>
				<data>http://www.phpclasses.org/discuss/package/14/</data>
				<url>http://www.phpclasses.org/discuss/package/14/</url>
			</link></support>
		<usage>To use this class just create a new object, set any variables
			to configure its options and call the
			<functionlink>SendMessage</functionlink> function to send a
			message.<paragraphbreak />It is not recommended that you use this
			class alone unless you have deep understanding of Internet mail
			standards on how to compose compliant e-mail messages. Instead, use
			the <link>
				<data>MIME message composing and sending class</data>
				<url>http://www.phpclasses.org/mimemessage</url>
			</link> and its sub-class SMTP message together with this SMTP class
			to properly compose e-mail messages, so your messages are not
			discarded for not being correctly composed.</usage>
	</documentation>

{/metadocument}
*/

class smtp_class
{
/*
{metadocument}
	<variable>
		<name>user</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Define the authorized user when sending messages to a SMTP
				server.</purpose>
			<usage>Set this variable to the user name when the SMTP server
				requires authentication.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $user="";

/*
{metadocument}
	<variable>
		<name>realm</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Define the authentication realm when sending messages to a
				SMTP server.</purpose>
			<usage>Set this variable when the SMTP server requires
				authentication and if more than one authentication realm is
				supported.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $realm="";

/*
{metadocument}
	<variable>
		<name>password</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Define the authorized user password when sending messages
				to a SMTP server.</purpose>
			<usage>Set this variable to the user password when the SMTP server
				requires authentication.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $password="";

/*
{metadocument}
	<variable>
		<name>workstation</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Define the client workstation name when sending messages
				to a SMTP server.</purpose>
			<usage>Set this variable to the client workstation when the SMTP
				server requires authentication identifiying the origin workstation
				name.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $workstation="";
	
/*
{metadocument}
	<variable>
		<name>authentication_mechanism</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Force the use of a specific authentication mechanism.</purpose>
			<usage>Set it to an empty string to let the class determine the
				authentication mechanism to use automatically based on the
				supported mechanisms by the server and by the SASL client library
				classes.<paragraphbreak />
				Set this variable to a specific mechanism name if you want to
				override the automatic authentication mechanism selection.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $authentication_mechanism="";

/*
{metadocument}
	<variable>
		<name>host_name</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Define the SMTP server host name.</purpose>
			<usage>Set to the host name of the SMTP server to which you want to
				relay the messages.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $host_name="";

/*
{metadocument}
	<variable>
		<name>host_port</name>
		<type>INTEGER</type>
		<value>25</value>
		<documentation>
			<purpose>Define the SMTP server host port.</purpose>
			<usage>Set to the TCP port of the SMTP server host to connect.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $host_port=25;

/*
{metadocument}
	<variable>
		<name>socks_host_name</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Define the SOCKS server host name.</purpose>
			<usage>Set to the SOCKS server host name through which the SMTP
				connection should be routed. Leave it empty if you do not want the
				connections to be established through a SOCKS server.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $socks_host_name = '';

/*
{metadocument}
	<variable>
		<name>socks_host_port</name>
		<type>INTEGER</type>
		<value>1080</value>
		<documentation>
			<purpose>Define the SOCKS server host port.</purpose>
			<usage>Set to the port of the SOCKS server host through which the
				the SMTP connection should be routed.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $socks_host_port=1080;

/*
{metadocument}
	<variable>
		<name>socks_version</name>
		<type>STRING</type>
		<value>5</value>
		<documentation>
			<purpose>Set the SOCKS protocol version.</purpose>
			<usage>Change this value if SOCKS server you want to use is
				listening to a different port.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $socks_version='5';

/*
{metadocument}
	<variable>
		<name>http_proxy_host_name</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Define the HTTP proxy server host name.</purpose>
			<usage>Set to the HTTP proxy server host name through which the
				SMTP connection should be routed. Leave it empty if you do not
				want the connections to be established through an HTTP proxy.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $http_proxy_host_name = '';

/*
{metadocument}
	<variable>
		<name>http_proxy_host_port</name>
		<type>INTEGER</type>
		<value>80</value>
		<documentation>
			<purpose>Define the HTTP proxy server host port.</purpose>
			<usage>Set to the port of the HTTP proxy server host through which
				the SMTP connection should be routed.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $http_proxy_host_port=80;

/*
{metadocument}
	<variable>
		<name>user_agent</name>
		<type>STRING</type>
		<value>SMTP Class (http://www.phpclasses.org/smtpclass $Revision: 1.50 $)</value>
		<documentation>
			<purpose>Set the user agent used when connecting via an HTTP proxy.</purpose>
			<usage>Change this value only if for some reason you want emulate a
				certain e-mail client.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $user_agent='SMTP Class (http://www.phpclasses.org/smtpclass $Revision: 1.50 $)';

/*
{metadocument}
	<variable>
		<name>ssl</name>
		<type>BOOLEAN</type>
		<value>0</value>
		<documentation>
			<purpose>Define whether the connection to the SMTP server should be
				established securely using SSL protocol.</purpose>
			<usage>Set to <booleanvalue>1</booleanvalue> if the SMTP server
				requires secure connections using SSL protocol.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $ssl=0;

/*
{metadocument}
	<variable>
		<name>start_tls</name>
		<type>BOOLEAN</type>
		<value>0</value>
		<documentation>
			<purpose>Define whether the connection to the SMTP server should use
				encryption after the connection is established using TLS
				protocol.</purpose>
			<usage>Set to <booleanvalue>1</booleanvalue> if the SMTP server
				requires that authentication be done securely starting the TLS
				protocol after the connection is established.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $start_tls = 0;

/*
{metadocument}
	<variable>
		<name>localhost</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Name of the local host computer</purpose>
			<usage>Set to the name of the computer connecting to the SMTP
				server from the local network.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $localhost="";

/*
{metadocument}
	<variable>
		<name>timeout</name>
		<type>INTEGER</type>
		<value>0</value>
		<documentation>
			<purpose>Specify the connection timeout period in seconds.</purpose>
			<usage>Leave it set to <integervalue>0</integervalue> if you want
				the connection attempts to wait forever. Change this value if for
				some reason the timeout period seems insufficient or otherwise it
				seems too long.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $timeout=0;

/*
{metadocument}
	<variable>
		<name>data_timeout</name>
		<type>INTEGER</type>
		<value>0</value>
		<documentation>
			<purpose>Specify the timeout period in seconds to wait for data from
				the server.</purpose>
			<usage>Leave it set to <integervalue>0</integervalue> if you want
				to use the same value defined in the
				<variablelink>timeout</variablelink> variable. Change this value
				if for some reason the default data timeout period seems
				insufficient or otherwise it seems too long.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $data_timeout=0;

/*
{metadocument}
	<variable>
		<name>direct_delivery</name>
		<type>BOOLEAN</type>
		<value>0</value>
		<documentation>
			<purpose>Boolean flag that indicates whether the message should be
				sent in direct delivery mode, i.e. the message is sent to the SMTP
				server associated to the domain of the recipient instead of
				relaying to the server specified by the
				<variablelink>host_name</variablelink> variable.</purpose>
			<usage>Set this to <tt><booleanvalue>1</booleanvalue></tt> if you
				want to send urgent messages directly to the recipient domain SMTP
				server.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $direct_delivery=0;

/*
{metadocument}
	<variable>
		<name>error</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Message that describes the error when a call to a class
				function fails.</purpose>
			<usage>Check this variable when an error occurs to understand what
				happened.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $error="";

/*
{metadocument}
	<variable>
		<name>debug</name>
		<type>BOOLEAN</type>
		<value>0</value>
		<documentation>
			<purpose>Specify whether it is necessary to output SMTP connection
				debug information.</purpose>
			<usage>Set this variable to
				<tt><booleanvalue>1</booleanvalue></tt> if you need to see
				the progress of the SMTP connection and protocol dialog when you
				need to understand the reason for delivery problems.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $debug=0;

/*
{metadocument}
	<variable>
		<name>html_debug</name>
		<type>BOOLEAN</type>
		<value>0</value>
		<documentation>
			<purpose>Specify whether the debug information should be outputted in
				HTML format.</purpose>
			<usage>Set this variable to
				<tt><booleanvalue>1</booleanvalue></tt> if you need to see
				the debug output in a Web page.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $html_debug=0;

/*
{metadocument}
	<variable>
		<name>esmtp</name>
		<type>BOOLEAN</type>
		<value>1</value>
		<documentation>
			<purpose>Specify whether the class should attempt to use ESMTP
				extensions supported by the server.</purpose>
			<usage>Set this variable to
				<tt><booleanvalue>0</booleanvalue></tt> if for some reason you
				want to avoid benefitting from ESMTP extensions.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $esmtp=1;

/*
{metadocument}
	<variable>
		<name>esmtp_extensions</name>
		<type>HASH</type>
		<value></value>
		<documentation>
			<purpose>Associative array with the list of ESMTP extensions
				supported by the SMTP server.</purpose>
			<usage>Check this variable after connecting to the SMTP server to
				determine which ESMTP extensions are supported.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $esmtp_extensions=array();

/*
{metadocument}
	<variable>
		<name>exclude_address</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Specify an address that should be considered invalid
				when resolving host name addresses.</purpose>
			<usage>In some networks any domain name that does not exist is
				resolved as a sub-domain of the default local domain. If the DNS is
				configured in such way that it always resolves any sub-domain of
				the default local domain to a given address, it is hard to
				determine whether a given domain does not exist.<paragraphbreak />
				If your network is configured this way, you may set this variable
				to the address that all sub-domains of the default local domain
				resolves, so the class can assume that such address is invalid.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $exclude_address="";

/*
{metadocument}
	<variable>
		<name>getmxrr</name>
		<type>STRING</type>
		<value>getmxrr</value>
		<documentation>
			<purpose>Specify the name of the function that is called to determine
				the SMTP server address of a given domain.</purpose>
			<usage>Change this to a working replacement of the PHP
				<tt>getmxrr()</tt> function if this is not working in your system
					and you want to send messages in direct delivery mode.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $getmxrr="GetMXRR";

/*
{metadocument}
	<variable>
		<name>pop3_auth_host</name>
		<type>STRING</type>
		<value></value>
		<documentation>
			<purpose>Specify the server address for POP3 based authentication.</purpose>
			<usage>Set this variable to the address of the POP3 server if the
				SMTP server requires POP3 based authentication.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	var $pop3_auth_host="";

/*
{metadocument}
	<variable>
		<name>pop3_auth_port</name>
		<type>INTEGER</type>
		<value>110</value>
		<documentation>
			<purpose>Specify the server port for POP3 based authentication.</purpose>
			<usage>Set this variable to the port of the POP3 server if the
				SMTP server requires POP3 based authentication.</usage>
		</documentation>
	</variable>
{/metadocument}
*/
	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;
	var $esmtp_host="";
	var $maximum_piped_recipients=100;

	/* 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;
			}
		}
		return($this->error);
	}

	Function SetError($error)
	{
		return($this->error=$error);
	}

	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 Resolve($domain, &$ip, $server_type)
	{
		if(preg_match('/^[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('Resolving '.$server_type.' server domain "'.$domain.'"...');
			if(!strcmp($ip=@gethostbyname($domain),$domain))
				$ip="";
		}
		if(strlen($ip)==0
		|| (strlen($this->exclude_address)
		&& !strcmp(@gethostbyname($this->exclude_address),$ip)))
			return($this->SetError("could not resolve the host domain \"".$domain."\""));
		return('');
	}

	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(strlen($this->Resolve($domain, $ip, 'SMTP')))
			return($this->error);
		if(strlen($this->socks_host_name))
		{
			switch($this->socks_version)
			{
				case '4':
					$version = 4;
					break;
				case '5':
					$version = 5;
					break;
				default:
					return('it was not specified a supported SOCKS protocol version');
					break;
			}
			$host_ip = $ip;
			$host_port = $port;
			if(strlen($this->error = $this->Resolve($this->socks_host_name, $ip, 'SOCKS')))
				return($this->error);
			if($this->ssl)
				$ip="ssl://".($socks_host = $this->socks_host_name);
			else
				$socks_host = $ip;
			if($this->debug)
				$this->OutputDebug("Connecting to SOCKS server \"".$socks_host."\" port ".$this->http_proxy_host_port."...");
			if(($this->connection=($this->timeout ? fsockopen($ip, $this->socks_host_port, $errno, $error, $this->timeout) : fsockopen($ip, $this->socks_host_port, $errno, $error))))
			{
				$timeout=($this->data_timeout ? $this->data_timeout : $this->timeout);
				if($timeout
				&& function_exists("socket_set_timeout"))
					socket_set_timeout($this->connection,$timeout,0);
				if(strlen($this->socks_host_name))
				{
					if($this->debug)
						$this->OutputDebug('Connected to the SOCKS server '.$this->socks_host_name);
					$send_error = 'it was not possible to send data to the SOCKS server';
					$receive_error = 'it was not possible to receive data from the SOCKS server';
					switch($version)
					{
						case 4:
							$command = 1;
							$user = '';
							if(!fputs($this->connection, chr($version).chr($command).pack('nN', $host_port, ip2long($host_ip)).$user.Chr(0)))
								$error = $this->SetDataAccessError($send_error);
							else
							{
								$response = fgets($this->connection, 9);
								if(strlen($response) != 8)
									$error = $this->SetDataAccessError($receive_error);
								else
								{
									$socks_errors = array(
										"\x5a"=>'',
										"\x5b"=>'request rejected',
										"\x5c"=>'request failed because client is not running identd (or not reachable from the server)',
										"\x5d"=>'request failed because client\'s identd could not confirm the user ID string in the request',
									);
									$error_code = $response[1];
									$error = (IsSet($socks_errors[$error_code]) ? $socks_errors[$error_code] : 'unknown');
									if(strlen($error))
										$error = 'SOCKS error: '.$error;
								}
							}
							break;
						case 5:
							if($this->debug)
								$this->OutputDebug('Negotiating the authentication method ...');
							$methods = 1;
							$method = 0;
							if(!fputs($this->connection, chr($version).chr($methods).chr($method)))
								$error = $this->SetDataAccessError($send_error);
							else
							{
								$response = fgets($this->connection, 3);
								if(strlen($response) != 2)
									$error = $this->SetDataAccessError($receive_error);
								elseif(Ord($response[1]) != $method)
									$error = 'the SOCKS server requires an authentication method that is not yet supported';
								else
								{
									if($this->debug)
										$this->OutputDebug('Connecting to SMTP server IP '.$host_ip.' port '.$host_port.'...');
									$command = 1;
									$address_type = 1;
									if(!fputs($this->connection, chr($version).chr($command)."\x00".chr($address_type).pack('Nn', ip2long($host_ip), $host_port)))
										$error = $this->SetDataAccessError($send_error);
									else
									{
										$response = fgets($this->connection, 11);
										if(strlen($response) != 10)
											$error = $this->SetDataAccessError($receive_error);
										else
										{
											$socks_errors = array(
												"\x00"=>'',
												"\x01"=>'general SOCKS server failure',
												"\x02"=>'connection not allowed by ruleset',
												"\x03"=>'Network unreachable',
												"\x04"=>'Host unreachable',
												"\x05"=>'Connection refused',
												"\x06"=>'TTL expired',
												"\x07"=>'Command not supported',
												"\x08"=>'Address type not supported'
											);
											$error_code = $response[1];
											$error = (IsSet($socks_errors[$error_code]) ? $socks_errors[$error_code] : 'unknown');
											if(strlen($error))
												$error = 'SOCKS error: '.$error;
										}
									}
								}
							}
							break;
						default:
							$error = 'support for SOCKS protocol version '.$this->socks_version.' is not yet implemented';
							break;
					}
					if(strlen($this->error = $error))
					{
						fclose($this->connection);
						return($error);
					}
				}
				return('');
			}
		}
		elseif(strlen($this->http_proxy_host_name))
		{
			if(strlen($error = $this->Resolve($this->http_proxy_host_name, $ip, 'SMTP')))
				return($error);
			if($this->ssl)
				$ip = 'ssl://'.($proxy_host = $this->http_proxy_host_name);
			else
				$proxy_host = $ip;
			if($this->debug)
				$this->OutputDebug("Connecting to HTTP proxy server \"".$ip."\" port ".$this->http_proxy_host_port."...");
			if(($this->connection=($this->timeout ? @fsockopen($ip, $this->http_proxy_host_port, $errno, $error, $this->timeout) : @fsockopen($ip, $this->http_proxy_host_port, $errno, $error))))
			{
				if($this->debug)
					$this->OutputDebug('Connected to HTTP proxy host "'.$this->http_proxy_host_name.'".');
				$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->PutLine('CONNECT '.$domain.':'.$port.' HTTP/1.0')
				&& $this->PutLine('User-Agent: '.$this->user_agent)
				&& $this->PutLine(''))
				{
					if(GetType($response = $this->GetLine()) == 'string')
					{
						if(!preg_match('/^http\\/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)$/i', $response,$matches))
							return($this->SetError("3 it was received an unexpected HTTP response status"));
						$error = $matches[1];
						switch($error)
						{
							case '200':
								for(;;)
								{
									if(GetType($response = $this->GetLine()) != 'string')
										break;
									if(strlen($response) == 0)
										return('');
								}
								break;
							default:
								$this->error = 'the HTTP proxy returned error '.$error.' '.$matches[2];
								break;
						}
					}
				}
				if($this->debug)
					$this->OutputDebug("Disconnected.");
				fclose($this->connection);
				$this->connection = 0;
				return($this->error);
			}
		}
		else
		{
			if($this->ssl)
				$ip = 'ssl://'.($host = $domain);
			elseif($this->start_tls)
				$ip = $host = $domain;
			else
				$host = $ip;
			if($this->debug)
				$this->OutputDebug("Connecting to SMTP server \"".$host."\" port ".$port."...");
			if(($this->connection=($this->timeout ? @fsockopen($ip, $port, $errno, $error, $this->timeout) : @fsockopen($ip, $port, $errno, $error))))
				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);
	}
	
	Function StartSMTP($localhost)
	{
		$success = 1;
		$this->esmtp_extensions = array();
		$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;
		}
		return($success);
	}

	/* Public methods */

/*
{metadocument}
	<function>
		<name>Connect</name>
		<type>BOOLEAN</type>
		<documentation>
			<purpose>Connect to an SMTP server.</purpose>
			<usage>Call this function as first step to send e-mail messages.</usage>
			<returnvalue>The function returns
				<tt><booleanvalue>1</booleanvalue></tt> if the connection is
					successfully established.</returnvalue>
		</documentation>
		<argument>
			<name>domain</name>
			<type>STRING</type>
			<defaultvalue></defaultvalue>
			<documentation>
				<purpose>Specify the domain of the recipient when using the direct
					delivery mode.</purpose>
			</documentation>
		</argument>
		<do>
{/metadocument}
*/
	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("HTTP_HOST"),"")
		&& !strcmp($localhost=getenv("HOST"),""))
			$localhost="localhost";
		$success=0;
		if($this->VerifyResultLines("220",$responses)>0)
		{
			$success = $this->StartSMTP($localhost);
			if($this->start_tls)
			{
				if(!IsSet($this->esmtp_extensions["STARTTLS"]))
				{
					$this->error="server does not support starting TLS";
					$success=0;
				}
				elseif(!function_exists('stream_socket_enable_crypto'))
				{
					$this->error="this PHP installation or version does not support starting TLS";
					$success=0;
				}
				elseif($success = ($this->PutLine('STARTTLS')
				&& $this->VerifyResultLines('220',$responses)>0))
				{
					if($this->debug)
						$this->OutputDebug('Starting TLS cryptograpic protocol');
					if(!($success = @stream_socket_enable_crypto($this->connection, 1, STREAM_CRYPTO_METHOD_TLS_CLIENT)))
						$this->error = 'could not start TLS connection encryption protocol';
					else
					{
						if($this->debug)
							$this->OutputDebug('TLS started');
						$success = $this->StartSMTP($localhost);
					}
				}
			}
			if($success
			&& strlen($this->user)
			&& strlen($this->pop3_auth_host)==0)
			{
				if(!IsSet($this->esmtp_extensions["AUTH"]))
				{
					$this->error="server does not require authentication";
					if(IsSet($this->esmtp_extensions["STARTTLS"]))
						$this->error .= ', it probably requires starting TLS';
					$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);
	}
/*
{metadocument}
		</do>
	</function>
{/metadocument}
*/

/*
{metadocument}
	<function>
		<name>MailFrom</name>
		<type>BOOLEAN</type>
		<documentation>
			<purpose>Set the address of the message sender.</purpose>
			<usage>Call this function right after establishing a connection with
				the <functionlink>Connect</functionlink> function.</usage>
			<returnvalue>The function returns
				<tt><booleanvalue>1</booleanvalue></tt> if the sender address is
					successfully set.</returnvalue>
		</documentation>
		<argument>
			<name>sender</name>
			<type>STRING</type>
			<documentation>
				<purpose>E-mail address of the sender.</purpose>
			</documentation>
		</argument>
		<do>
{/metadocument}
*/
	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);
	}
/*
{metadocument}
		</do>
	</function>
{/metadocument}
*/

/*
{metadocument}
	<function>
		<name>SetRecipient</name>
		<type>BOOLEAN</type>
		<documentation>
			<purpose>Set the address of a message recipient.</purpose>
			<usage>Call this function repeatedly for each recipient right after
				setting the message sender with the
				<functionlink>MailFrom</functionlink> function.</usage>
			<returnvalue>The function returns
				<tt><booleanvalue>1</booleanvalue></tt> if the recipient address is
					successfully set.</returnvalue>
		</documentation>
		<argument>
			<name>recipient</name>
			<type>STRING</type>
			<documentation>
				<purpose>E-mail address of a recipient.</purpose>
			</documentation>
		</argument>
		<do>
{/metadocument}
*/
	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);
	}
/*
{metadocument}
		</do>
	</function>
{/metadocument}
*/

/*
{metadocument}
	<function>
		<name>StartData</name>
		<type>BOOLEAN</type>
		<documentation>
			<purpose>Tell the SMTP server that the message data will start being
				sent.</purpose>
			<usage>Call this function right after you are done setting all the
				message recipients with the
				<functionlink>SetRecipient</functionlink> function.</usage>
			<returnvalue>The function returns
				<tt><booleanvalue>1</booleanvalue></tt> if the server is ready to
				start receiving the message data.</returnvalue>
		</documentation>
		<do>
{/metadocument}
*/
	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);
	}
/*
{metadocument}
		</do>
	</function>
{/metadocument}
*/

/*
{metadocument}
	<function>
		<name>PrepareData</name>
		<type>STRING</type>
		<documentation>
			<purpose>Prepare message data to normalize line breaks and escaping
				lines that contain single dots.</purpose>
			<usage>Call this function if the message data you want to send may
				contain line breaks that are not the
				<stringvalue>&#13;&#10;</stringvalue> sequence or it may contain
				lines that just have a single dot.</usage>
			<returnvalue>Resulting normalized messages data.</returnvalue>
		</documentation>
		<argument>
			<name>data</name>
			<type>STRING</type>
			<documentation>
				<purpose>Message data to be prepared.</purpose>
			</documentation>
		</argument>
		<do>
{/metadocument}
*/
	Function PrepareData($data)
	{
		return(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));
	}
/*
{metadocument}
		</do>
	</function>
{/metadocument}
*/

/*
{metadocument}
	<function>
		<name>SendData</name>
		<type>BOOLEAN</type>
		<documentation>
			<purpose>Send message data.</purpose>
			<usage>Call this function repeatedly for all message data blocks
				to be sent right after	start sending message data with the
				<functionlink>StartData</functionlink> function.</usage>
			<returnvalue>The function returns
				<tt><booleanvalue>1</booleanvalue></tt> if the message data was
				sent to the SMTP server successfully.</returnvalue>
		</documentation>
		<argument>
			<name>data</name>
			<type>STRING</type>
			<documentation>
				<purpose>Message data to be sent.</purpose>
			</documentation>
		</argument>
		<do>
{/metadocument}
*/
	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));
	}
/*
{metadocument}
		</do>
	</function>
{/metadocument}
*/

/*
{metadocument}
	<function>
		<name>EndSendingData</name>
		<type>BOOLEAN</type>
		<documentation>
			<purpose>Tell the server that all the message data was sent.</purpose>
			<usage>Call this function when you are done with sending the message
				data with the	<functionlink>SendData</functionlink> function.</usage>
			<returnvalue>The function returns
				<tt><booleanvalue>1</booleanvalue></tt> if the server accepted the
				message.</returnvalue>
		</documentation>
		<do>
{/metadocument}
*/
	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);
	}
/*
{metadocument}
		</do>
	</function>
{/metadocument}
*/

/*
{metadocument}
	<function>
		<name>ResetConnection</name>
		<type>BOOLEAN</type>
		<documentation>
			<purpose>Reset an already established SMTP connection to the initial
				state.</purpose>
			<usage>Call this function when there was an error sending a message
				and you need to skip to sending another message without
				disconnecting.</usage>
			<returnvalue>The function returns
				<tt><booleanvalue>1</booleanvalue></tt> if the connection was
				resetted successfully.</returnvalue>
		</documentation>
		<do>
{/metadocument}
*/
	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);
	}
/*
{metadocument}
		</do>
	</function>
{/metadocument}
*/

/*
{metadocument}
	<function>
		<name>Disconnect</name>
		<type>BOOLEAN</type>
		<documentation>
			<purpose>Terminate a previously opened connection.</purpose>
			<usage>Call this function after you are done sending your
				messages.</usage>
			<returnvalue>The function returns
				<tt><booleanvalue>1</booleanvalue></tt> if the connection was
					successfully closed.</returnvalue>
		</documentation>
		<argument>
			<name>quit</name>
			<type>BOOLEAN</type>
			<defaultvalue>1</defaultvalue>
			<documentation>
				<purpose>Boolean option that tells whether the class should
					perform the final connection quit handshake, or just close the
					connection without waiting.</purpose>
			</documentation>
		</argument>
		<do>
{/metadocument}
*/
	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);
	}
/*
{metadocument}
		</do>
	</function>
{/metadocument}
*/

/*
{metadocument}
	<function>
		<name>SendMessage</name>
		<type>BOOLEAN</type>
		<documentation>
			<purpose>Send a message in a single call.</purpose>
			<usage>Call this function if you want to send a single messages to a
				small number of recipients in a single call.</usage>
			<returnvalue>The function returns
				<tt><booleanvalue>1</booleanvalue></tt> if the message was sent
				successfully.</returnvalue>
		</documentation>
		<argument>
			<name>sender</name>
			<type>STRING</type>
			<documentation>
				<purpose>E-mail address of the sender.</purpose>
			</documentation>
		</argument>
		<argument>
			<name>recipients</name>
			<type>STRING</type>
			<documentation>
				<purpose>Array with a list of the e-mail addresses of the
					recipients of the message.</purpose>
			</documentation>
		</argument>
		<argument>
			<name>headers</name>
			<type>ARRAY</type>
			<documentation>
				<purpose>Array with a list of the header lines of the message.</purpose>
			</documentation>
		</argument>
		<argument>
			<name>body</name>
			<type>STRING</type>
			<documentation>
				<purpose>Body data of the message.</purpose>
			</documentation>
		</argument>
		<do>
{/metadocument}
*/
	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";
					$success=($this->SendData($header_data."\r\n")
						&& $this->SendData($this->PrepareData($body))
						&& $this->EndSendingData());
				}
			}
			$error=$this->error;
			$disconnect_success=$this->Disconnect($success);
			if($success)
				$success=$disconnect_success;
			else
				$this->error=$error;
		}
		return($success);
	}
/*
{metadocument}
		</do>
	</function>
{/metadocument}
*/

};

/*

{metadocument}
</class>
{/metadocument}

*/

?>
OpenPOWER on IntegriCloud