summaryrefslogtreecommitdiff
path: root/vendor/thecodingmachine/safe/generated/imap.php
blob: 917a8e2017daa8f476b39683680936ec8e97b516 (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
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
<?php

namespace Safe;

use Safe\Exceptions\ImapException;

/**
 * Convert an 8bit string to a quoted-printable string (according to
 * RFC2045, section 6.7).
 *
 * @param string $string The 8bit string to convert
 * @return string Returns a quoted-printable string.
 * @throws ImapException
 *
 */
function imap_8bit(string $string): string
{
    error_clear_last();
    $result = \imap_8bit($string);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Appends a string message to the specified folder.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $folder The mailbox name, see imap_open for more
 * information
 * @param string $message The message to be append, as a string
 *
 * When talking to the Cyrus IMAP server, you must use "\r\n" as
 * your end-of-line terminator instead of "\n" or the operation will
 * fail
 * @param string $options If provided, the options will also be written
 * to the folder
 * @param string $internal_date If this parameter is set, it will set the INTERNALDATE on the appended message.  The parameter should be a date string that conforms to the rfc2060 specifications for a date_time value.
 * @throws ImapException
 *
 */
function imap_append($imap, string $folder, string $message, string $options = null, string $internal_date = null): void
{
    error_clear_last();
    if ($internal_date !== null) {
        $result = \imap_append($imap, $folder, $message, $options, $internal_date);
    } elseif ($options !== null) {
        $result = \imap_append($imap, $folder, $message, $options);
    } else {
        $result = \imap_append($imap, $folder, $message);
    }
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Decodes the given BASE-64 encoded string.
 *
 * @param string $string The encoded text
 * @return string Returns the decoded message as a string.
 * @throws ImapException
 *
 */
function imap_base64(string $string): string
{
    error_clear_last();
    $result = \imap_base64($string);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Convert an 8bit string to a base64 string according to RFC2045, Section 6.8.
 *
 * @param string $string The 8bit string
 * @return string Returns a base64 encoded string.
 * @throws ImapException
 *
 */
function imap_binary(string $string): string
{
    error_clear_last();
    $result = \imap_binary($string);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * imap_body returns the body of the message,
 * numbered message_num in the current
 * mailbox.
 *
 * imap_body will only return a verbatim copy of the
 * message body. To extract single parts of a multipart MIME-encoded
 * message you have to use imap_fetchstructure to
 * analyze its structure and imap_fetchbody to
 * extract a copy of a single body component.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $message_num The message number
 * @param int $flags The optional flags are a bit mask
 * with one or more of the following:
 *
 *
 *
 * FT_UID - The message_num is a UID
 *
 *
 *
 *
 * FT_PEEK - Do not set the \Seen flag if not already set
 *
 *
 *
 *
 * FT_INTERNAL - The return string is in internal format, will
 * not canonicalize to CRLF.
 *
 *
 *
 * @return string Returns the body of the specified message, as a string.
 * @throws ImapException
 *
 */
function imap_body($imap, int $message_num, int $flags = 0): string
{
    error_clear_last();
    $result = \imap_body($imap, $message_num, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Read the structure of a specified body section of a specific message.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $message_num The message number
 * @param string $section The body section to read
 * @return \stdClass Returns the information in an object.
 * For a detailed description
 * of the object structure and properties see
 * imap_fetchstructure.
 * @throws ImapException
 *
 */
function imap_bodystruct($imap, int $message_num, string $section): \stdClass
{
    error_clear_last();
    $result = \imap_bodystruct($imap, $message_num, $section);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Checks information about the current mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @return \stdClass Returns the information in an object with following properties:
 *
 *
 *
 * Date - current system time formatted according to RFC2822
 *
 *
 *
 *
 * Driver - protocol used to access this mailbox:
 * POP3, IMAP, NNTP
 *
 *
 *
 *
 * Mailbox - the mailbox name
 *
 *
 *
 *
 * Nmsgs - number of messages in the mailbox
 *
 *
 *
 *
 * Recent - number of recent messages in the mailbox
 *
 *
 *
 *
 * Returns FALSE on failure.
 * @throws ImapException
 *
 */
function imap_check($imap): \stdClass
{
    error_clear_last();
    $result = \imap_check($imap);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * This function causes a store to delete the specified
 * flag to the flags set for the
 * messages in the specified sequence.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $sequence A sequence of message numbers. You can enumerate desired messages
 * with the X,Y syntax, or retrieve all messages
 * within an interval with the X:Y syntax
 * @param string $flag The flags which you can unset are "\\Seen", "\\Answered", "\\Flagged",
 * "\\Deleted", and "\\Draft" (as defined by RFC2060)
 * @param int $options options are a bit mask and may contain
 * the single option:
 *
 *
 *
 * ST_UID - The sequence argument contains UIDs
 * instead of sequence numbers
 *
 *
 *
 * @throws ImapException
 *
 */
function imap_clearflag_full($imap, string $sequence, string $flag, int $options = 0): void
{
    error_clear_last();
    $result = \imap_clearflag_full($imap, $sequence, $flag, $options);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Closes the imap stream.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $flags If set to CL_EXPUNGE, the function will silently
 * expunge the mailbox before closing, removing all messages marked for
 * deletion. You can achieve the same thing by using
 * imap_expunge
 * @throws ImapException
 *
 */
function imap_close($imap, int $flags = 0): void
{
    error_clear_last();
    $result = \imap_close($imap, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Creates a new mailbox specified by mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $mailbox The mailbox name, see imap_open for more
 * information. Names containing international characters should be
 * encoded by imap_utf7_encode
 * @throws ImapException
 *
 */
function imap_createmailbox($imap, string $mailbox): void
{
    error_clear_last();
    $result = \imap_createmailbox($imap, $mailbox);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Deletes the specified mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $mailbox The mailbox name, see imap_open for more
 * information
 * @throws ImapException
 *
 */
function imap_deletemailbox($imap, string $mailbox): void
{
    error_clear_last();
    $result = \imap_deletemailbox($imap, $mailbox);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * This function fetches mail headers for the given
 * sequence and returns an overview of their
 * contents.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $sequence A message sequence description. You can enumerate desired messages
 * with the X,Y syntax, or retrieve all messages
 * within an interval with the X:Y syntax
 * @param int $flags sequence will contain a sequence of message
 * indices or UIDs, if this parameter is set to
 * FT_UID.
 * @return array Returns an array of objects describing one message header each.
 * The object will only define a property if it exists. The possible
 * properties are:
 *
 *
 *
 * subject - the messages subject
 *
 *
 *
 *
 * from - who sent it
 *
 *
 *
 *
 * to - recipient
 *
 *
 *
 *
 * date - when was it sent
 *
 *
 *
 *
 * message_id - Message-ID
 *
 *
 *
 *
 * references - is a reference to this message id
 *
 *
 *
 *
 * in_reply_to - is a reply to this message id
 *
 *
 *
 *
 * size - size in bytes
 *
 *
 *
 *
 * uid - UID the message has in the mailbox
 *
 *
 *
 *
 * msgno - message sequence number in the mailbox
 *
 *
 *
 *
 * recent - this message is flagged as recent
 *
 *
 *
 *
 * flagged -  this message is flagged
 *
 *
 *
 *
 * answered -  this message is flagged as answered
 *
 *
 *
 *
 * deleted -  this message is flagged for deletion
 *
 *
 *
 *
 * seen -  this message is flagged as already read
 *
 *
 *
 *
 * draft -  this message is flagged as being a draft
 *
 *
 *
 *
 * udate -  the UNIX timestamp of the arrival date
 *
 *
 *
 * The function returns FALSE on failure.
 * @throws ImapException
 *
 */
function imap_fetch_overview($imap, string $sequence, int $flags = 0): array
{
    error_clear_last();
    $result = \imap_fetch_overview($imap, $sequence, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Fetch of a particular section of the body of the specified messages.
 * Body parts are not decoded by this function.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $message_num The message number
 * @param string $section The part number. It is a string of integers delimited by period which
 * index into a body part list as per the IMAP4 specification
 * @param int $flags A bitmask with one or more of the following:
 *
 *
 *
 * FT_UID - The message_num is a UID
 *
 *
 *
 *
 * FT_PEEK - Do not set the \Seen flag if
 * not already set
 *
 *
 *
 *
 * FT_INTERNAL - The return string is in
 * internal format, will not canonicalize to CRLF.
 *
 *
 *
 * @return string Returns a particular section of the body of the specified messages as a
 * text string.
 * @throws ImapException
 *
 */
function imap_fetchbody($imap, int $message_num, string $section, int $flags = 0): string
{
    error_clear_last();
    $result = \imap_fetchbody($imap, $message_num, $section, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * This function causes a fetch of the complete, unfiltered RFC2822 format header of the specified
 * message.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $message_num The message number
 * @param int $flags The possible flags are:
 *
 *
 *
 * FT_UID - The message_num
 * argument is a UID
 *
 *
 *
 *
 * FT_INTERNAL - The return string
 * is in "internal" format, without any attempt to
 * canonicalize to CRLF newlines
 *
 *
 *
 *
 * FT_PREFETCHTEXT - The RFC822.TEXT
 * should be pre-fetched at the same time.  This avoids an
 * extra RTT on an IMAP connection if a full message text is
 * desired (e.g. in a "save to local file" operation)
 *
 *
 *
 * @return string Returns the header of the specified message as a text string.
 * @throws ImapException
 *
 */
function imap_fetchheader($imap, int $message_num, int $flags = 0): string
{
    error_clear_last();
    $result = \imap_fetchheader($imap, $message_num, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Fetch the MIME headers of a particular section of the body of the specified messages.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $message_num The message number
 * @param string $section The part number. It is a string of integers delimited by period which
 * index into a body part list as per the IMAP4 specification
 * @param int $flags A bitmask with one or more of the following:
 *
 *
 *
 * FT_UID - The message_num is a UID
 *
 *
 *
 *
 * FT_PEEK - Do not set the \Seen flag if
 * not already set
 *
 *
 *
 *
 * FT_INTERNAL - The return string is in
 * internal format, will not canonicalize to CRLF.
 *
 *
 *
 * @return string Returns the MIME headers of a particular section of the body of the specified messages as a
 * text string.
 * @throws ImapException
 *
 */
function imap_fetchmime($imap, int $message_num, string $section, int $flags = 0): string
{
    error_clear_last();
    $result = \imap_fetchmime($imap, $message_num, $section, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Fetches all the structured information for a given message.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $message_num The message number
 * @param int $flags This optional parameter only has a single option,
 * FT_UID, which tells the function to treat the
 * message_num argument as a
 * UID.
 * @return \stdClass Returns an object with properties listed in the table below.
 *
 *
 *
 * Returned Object for imap_fetchstructure
 *
 *
 *
 *
 * type
 * Primary body type
 *
 *
 * encoding
 * Body transfer encoding
 *
 *
 * ifsubtype
 * TRUE if there is a subtype string
 *
 *
 * subtype
 * MIME subtype
 *
 *
 * ifdescription
 * TRUE if there is a description string
 *
 *
 * description
 * Content description string
 *
 *
 * ifid
 * TRUE if there is an identification string
 *
 *
 * id
 * Identification string
 *
 *
 * lines
 * Number of lines
 *
 *
 * bytes
 * Number of bytes
 *
 *
 * ifdisposition
 * TRUE if there is a disposition string
 *
 *
 * disposition
 * Disposition string
 *
 *
 * ifdparameters
 * TRUE if the dparameters array exists
 *
 *
 * dparameters
 * An array of objects where each object has an
 * "attribute" and a "value"
 * property corresponding to the parameters on the
 * Content-disposition MIME
 * header.
 *
 *
 * ifparameters
 * TRUE if the parameters array exists
 *
 *
 * parameters
 * An array of objects where each object has an
 * "attribute" and a "value"
 * property.
 *
 *
 * parts
 * An array of objects identical in structure to the top-level
 * object, each of which corresponds to a MIME body
 * part.
 *
 *
 *
 *
 *
 *
 * Primary body type (value may vary with used library, use of constants is recommended)
 *
 *
 * ValueTypeConstant
 *
 *
 * 0textTYPETEXT
 * 1multipartTYPEMULTIPART
 * 2messageTYPEMESSAGE
 * 3applicationTYPEAPPLICATION
 * 4audioTYPEAUDIO
 * 5imageTYPEIMAGE
 * 6videoTYPEVIDEO
 * 7modelTYPEMODEL
 * 8otherTYPEOTHER
 *
 *
 *
 *
 *
 * Transfer encodings (value may vary with used library, use of constants is recommended)
 *
 *
 * ValueTypeConstant
 *
 *
 * 07bitENC7BIT
 * 18bitENC8BIT
 * 2BinaryENCBINARY
 * 3Base64ENCBASE64
 * 4Quoted-PrintableENCQUOTEDPRINTABLE
 * 5otherENCOTHER
 *
 *
 *
 * @throws ImapException
 *
 */
function imap_fetchstructure($imap, int $message_num, int $flags = 0): \stdClass
{
    error_clear_last();
    $result = \imap_fetchstructure($imap, $message_num, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Purges the cache of entries of a specific type.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $flags Specifies the cache to purge. It may one or a combination
 * of the following constants:
 * IMAP_GC_ELT (message cache elements),
 * IMAP_GC_ENV (envelope and bodies),
 * IMAP_GC_TEXTS (texts).
 * @throws ImapException
 *
 */
function imap_gc($imap, int $flags): void
{
    error_clear_last();
    $result = \imap_gc($imap, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Gets the ACL for a given mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $mailbox The mailbox name, see imap_open for more
 * information
 * @return array Returns an associative array of "folder" =&gt; "acl" pairs.
 * @throws ImapException
 *
 */
function imap_getacl($imap, string $mailbox): array
{
    error_clear_last();
    $result = \imap_getacl($imap, $mailbox);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Gets information on the mailboxes.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $reference reference should normally be just the server
 * specification as described in imap_open
 * @param string $pattern Specifies where in the mailbox hierarchy
 * to start searching.
 *
 * There are two special characters you can
 * pass as part of the pattern:
 * '*' and '%'.
 * '*' means to return all mailboxes. If you pass
 * pattern as '*', you will
 * get a list of the entire mailbox hierarchy.
 * '%'
 * means to return the current level only.
 * '%' as the pattern
 * parameter will return only the top level
 * mailboxes; '~/mail/%' on UW_IMAPD will return every mailbox in the ~/mail directory, but none in subfolders of that directory.
 * @return array Returns an array of objects containing mailbox information. Each
 * object has the attributes name, specifying
 * the full name of the mailbox; delimiter,
 * which is the hierarchy delimiter for the part of the hierarchy
 * this mailbox is in; and
 * attributes. Attributes
 * is a bitmask that can be tested against:
 *
 *
 *
 * LATT_NOINFERIORS - This mailbox not contains, and may not contain any
 * "children" (there are no mailboxes below this one). Calling
 * imap_createmailbox will not work on this mailbox.
 *
 *
 *
 *
 * LATT_NOSELECT - This is only a container,
 * not a mailbox - you cannot open it.
 *
 *
 *
 *
 * LATT_MARKED - This mailbox is marked. This means that it may
 * contain new messages since the last time it was checked. Not provided by all IMAP
 * servers.
 *
 *
 *
 *
 * LATT_UNMARKED - This mailbox is not marked, does not contain new
 * messages.  If either MARKED or UNMARKED is
 * provided, you can assume the IMAP server  supports this feature for this mailbox.
 *
 *
 *
 *
 * LATT_REFERRAL - This container has a referral to a remote mailbox.
 *
 *
 *
 *
 * LATT_HASCHILDREN - This mailbox has selectable inferiors.
 *
 *
 *
 *
 * LATT_HASNOCHILDREN - This mailbox has no selectable inferiors.
 *
 *
 *
 * The function returns FALSE on failure.
 * @throws ImapException
 *
 */
function imap_getmailboxes($imap, string $reference, string $pattern): array
{
    error_clear_last();
    $result = \imap_getmailboxes($imap, $reference, $pattern);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Gets information about the subscribed mailboxes.
 *
 * Identical to imap_getmailboxes, except that it only
 * returns mailboxes that the user is subscribed to.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $reference reference should normally be just the server
 * specification as described in imap_open
 * @param string $pattern Specifies where in the mailbox hierarchy
 * to start searching.
 *
 * There are two special characters you can
 * pass as part of the pattern:
 * '*' and '%'.
 * '*' means to return all mailboxes. If you pass
 * pattern as '*', you will
 * get a list of the entire mailbox hierarchy.
 * '%'
 * means to return the current level only.
 * '%' as the pattern
 * parameter will return only the top level
 * mailboxes; '~/mail/%' on UW_IMAPD will return every mailbox in the ~/mail directory, but none in subfolders of that directory.
 * @return array Returns an array of objects containing mailbox information. Each
 * object has the attributes name, specifying
 * the full name of the mailbox; delimiter,
 * which is the hierarchy delimiter for the part of the hierarchy
 * this mailbox is in; and
 * attributes. Attributes
 * is a bitmask that can be tested against:
 *
 *
 *
 * LATT_NOINFERIORS - This mailbox has no
 * "children" (there are no mailboxes below this one).
 *
 *
 *
 *
 * LATT_NOSELECT - This is only a container,
 * not a mailbox - you cannot open it.
 *
 *
 *
 *
 * LATT_MARKED - This mailbox is marked.
 * Only used by UW-IMAPD.
 *
 *
 *
 *
 * LATT_UNMARKED - This mailbox is not marked.
 * Only used by UW-IMAPD.
 *
 *
 *
 *
 * LATT_REFERRAL - This container has a referral to a remote mailbox.
 *
 *
 *
 *
 * LATT_HASCHILDREN - This mailbox has selectable inferiors.
 *
 *
 *
 *
 * LATT_HASNOCHILDREN - This mailbox has no selectable inferiors.
 *
 *
 *
 * The function returns FALSE on failure.
 * @throws ImapException
 *
 */
function imap_getsubscribed($imap, string $reference, string $pattern): array
{
    error_clear_last();
    $result = \imap_getsubscribed($imap, $reference, $pattern);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Gets information about the given message number by reading its headers.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $message_num The message number
 * @param int $from_length Number of characters for the fetchfrom property.
 * Must be greater than or equal to zero.
 * @param int $subject_length Number of characters for the fetchsubject property
 * Must be greater than or equal to zero.
 * @return \stdClass Returns FALSE on error or, if successful, the information in an object with following properties:
 *
 *
 *
 * toaddress - full to: line, up to 1024 characters
 *
 *
 *
 *
 * to - an array of objects from the To: line, with the following
 * properties: personal, adl,
 * mailbox, and host
 *
 *
 *
 *
 * fromaddress - full from: line, up to 1024 characters
 *
 *
 *
 *
 * from - an array of objects from the From: line, with the following
 * properties: personal, adl,
 * mailbox, and host
 *
 *
 *
 *
 * ccaddress - full cc: line, up to 1024 characters
 *
 *
 *
 *
 * cc - an array of objects from the Cc: line, with the following
 * properties: personal, adl,
 * mailbox, and host
 *
 *
 *
 *
 * bccaddress - full bcc: line, up to 1024 characters
 *
 *
 *
 *
 * bcc - an array of objects from the Bcc: line, with the following
 * properties: personal, adl,
 * mailbox, and host
 *
 *
 *
 *
 * reply_toaddress - full Reply-To: line, up to 1024 characters
 *
 *
 *
 *
 * reply_to - an array of objects from the Reply-To: line, with the following
 * properties: personal, adl,
 * mailbox, and host
 *
 *
 *
 *
 * senderaddress - full sender: line, up to 1024 characters
 *
 *
 *
 *
 * sender - an array of objects from the Sender: line, with the following
 * properties: personal, adl,
 * mailbox, and host
 *
 *
 *
 *
 * return_pathaddress - full Return-Path: line, up to 1024 characters
 *
 *
 *
 *
 * return_path - an array of objects from the Return-Path: line, with the
 * following properties: personal,
 * adl, mailbox, and
 * host
 *
 *
 *
 *
 * remail -
 *
 *
 *
 *
 * date - The message date as found in its headers
 *
 *
 *
 *
 * Date - Same as date
 *
 *
 *
 *
 * subject - The message subject
 *
 *
 *
 *
 * Subject - Same as subject
 *
 *
 *
 *
 * in_reply_to -
 *
 *
 *
 *
 * message_id -
 *
 *
 *
 *
 * newsgroups -
 *
 *
 *
 *
 * followup_to -
 *
 *
 *
 *
 * references -
 *
 *
 *
 *
 * Recent - R if recent and seen, N
 * if recent and not seen, ' ' if not recent.
 *
 *
 *
 *
 * Unseen - U if not seen AND not recent, ' ' if seen
 * OR not seen and recent
 *
 *
 *
 *
 * Flagged - F if flagged, ' ' if not flagged
 *
 *
 *
 *
 * Answered - A if answered, ' ' if unanswered
 *
 *
 *
 *
 * Deleted - D if deleted, ' ' if not deleted
 *
 *
 *
 *
 * Draft - X if draft, ' ' if not draft
 *
 *
 *
 *
 * Msgno - The message number
 *
 *
 *
 *
 * MailDate -
 *
 *
 *
 *
 * Size - The message size
 *
 *
 *
 *
 * udate - mail message date in Unix time
 *
 *
 *
 *
 * fetchfrom - from line formatted to fit from_length
 * characters
 *
 *
 *
 *
 * fetchsubject - subject line formatted to fit
 * subject_length characters
 *
 *
 *
 * @throws ImapException
 *
 */
function imap_headerinfo($imap, int $message_num, int $from_length = 0, int $subject_length = 0): \stdClass
{
    error_clear_last();
    $result = \imap_headerinfo($imap, $message_num, $from_length, $subject_length);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Returns headers for all messages in a mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @return array Returns an array of string formatted with header info. One
 * element per mail message.
 * Returns FALSE on failure.
 * @throws ImapException
 *
 */
function imap_headers($imap): array
{
    error_clear_last();
    $result = \imap_headers($imap);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Returns an array containing the names of the mailboxes that have
 * content in the text of the mailbox.
 *
 * This function is similar to imap_listmailbox,
 * but it will additionally check for the presence of the string
 * content inside the mailbox data.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $reference reference should normally be just the server
 * specification as described in imap_open
 * @param string $pattern Specifies where in the mailbox hierarchy
 * to start searching.
 *
 * There are two special characters you can
 * pass as part of the pattern:
 * '*' and '%'.
 * '*' means to return all mailboxes. If you pass
 * pattern as '*', you will
 * get a list of the entire mailbox hierarchy.
 * '%'
 * means to return the current level only.
 * '%' as the pattern
 * parameter will return only the top level
 * mailboxes; '~/mail/%' on UW_IMAPD will return every mailbox in the ~/mail directory, but none in subfolders of that directory.
 * @param string $content The searched string
 * @return array Returns an array containing the names of the mailboxes that have
 * content in the text of the mailbox.
 * @throws ImapException
 *
 */
function imap_listscan($imap, string $reference, string $pattern, string $content): array
{
    error_clear_last();
    $result = \imap_listscan($imap, $reference, $pattern, $content);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Gets an array of all the mailboxes that you have subscribed.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $reference reference should normally be just the server
 * specification as described in imap_open
 * @param string $pattern Specifies where in the mailbox hierarchy
 * to start searching.
 *
 * There are two special characters you can
 * pass as part of the pattern:
 * '*' and '%'.
 * '*' means to return all mailboxes. If you pass
 * pattern as '*', you will
 * get a list of the entire mailbox hierarchy.
 * '%'
 * means to return the current level only.
 * '%' as the pattern
 * parameter will return only the top level
 * mailboxes; '~/mail/%' on UW_IMAPD will return every mailbox in the ~/mail directory, but none in subfolders of that directory.
 * @return array Returns an array of all the subscribed mailboxes.
 * @throws ImapException
 *
 */
function imap_lsub($imap, string $reference, string $pattern): array
{
    error_clear_last();
    $result = \imap_lsub($imap, $reference, $pattern);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Create a MIME message based on the given envelope
 * and bodies sections.
 *
 * @param array $envelope An associative array of header fields. Valid keys are: "remail",
 * "return_path", "date", "from", "reply_to", "in_reply_to", "subject",
 * "to", "cc", "bcc" and "message_id", which set the respective message headers to the given string.
 * To set additional headers, the key "custom_headers" is supported, which expects
 * an array of those headers, e.g. ["User-Agent: My Mail Client"].
 * @param array $bodies An indexed array of bodies. The first body is the main body of the message;
 * only if it has a type of TYPEMULTIPART, further bodies
 * are processed; these bodies constitute the bodies of the parts.
 *
 *
 * Body Array Structure
 *
 *
 *
 * Key
 * Type
 * Description
 *
 *
 *
 *
 * type
 * int
 *
 * The MIME type.
 * One of TYPETEXT (default), TYPEMULTIPART,
 * TYPEMESSAGE, TYPEAPPLICATION,
 * TYPEAUDIO, TYPEIMAGE,
 * TYPEMODEL or TYPEOTHER.
 *
 *
 *
 * encoding
 * int
 *
 * The Content-Transfer-Encoding.
 * One of ENC7BIT (default), ENC8BIT,
 * ENCBINARY, ENCBASE64,
 * ENCQUOTEDPRINTABLE or ENCOTHER.
 *
 *
 *
 * charset
 * string
 * The charset parameter of the MIME type.
 *
 *
 * type.parameters
 * array
 * An associative array of Content-Type parameter names and their values.
 *
 *
 * subtype
 * string
 * The MIME subtype, e.g. 'jpeg' for TYPEIMAGE.
 *
 *
 * id
 * string
 * The Content-ID.
 *
 *
 * description
 * string
 * The Content-Description.
 *
 *
 * disposition.type
 * string
 * The Content-Disposition, e.g. 'attachment'.
 *
 *
 * disposition
 * array
 * An associative array of Content-Disposition parameter names and values.
 *
 *
 * contents.data
 * string
 * The payload.
 *
 *
 * lines
 * int
 * The size of the payload in lines.
 *
 *
 * bytes
 * int
 * The size of the payload in bytes.
 *
 *
 * md5
 * string
 * The MD5 checksum of the payload.
 *
 *
 *
 *
 * @return string Returns the MIME message as string.
 * @throws ImapException
 *
 */
function imap_mail_compose(array $envelope, array $bodies): string
{
    error_clear_last();
    $result = \imap_mail_compose($envelope, $bodies);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Copies mail messages specified by message_nums
 * to specified mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $message_nums message_nums is a range not just message
 * numbers (as described in RFC2060).
 * @param string $mailbox The mailbox name, see imap_open for more
 * information
 * @param int $flags flags is a bitmask of one or more of
 *
 *
 *
 * CP_UID - the sequence numbers contain UIDS
 *
 *
 *
 *
 * CP_MOVE - Delete the messages from
 * the current mailbox after copying. If this flag is set, the function
 * behaves identically to imap_mail_move.
 *
 *
 *
 * @throws ImapException
 *
 */
function imap_mail_copy($imap, string $message_nums, string $mailbox, int $flags = 0): void
{
    error_clear_last();
    $result = \imap_mail_copy($imap, $message_nums, $mailbox, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Moves mail messages specified by message_nums to the
 * specified mailbox.
 * Note that the mail messages are actually copied to the
 * mailbox, and the original messages are flagged for deletion.
 * That implies that the messages in mailbox are assigned new UIDs.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $message_nums message_nums is a range not just message numbers
 * (as described in RFC2060).
 * @param string $mailbox The mailbox name, see imap_open for more
 * information
 * @param int $flags flags is a bitmask and may contain the single option:
 *
 *
 *
 * CP_UID - the sequence numbers contain UIDS
 *
 *
 *
 * @throws ImapException
 *
 */
function imap_mail_move($imap, string $message_nums, string $mailbox, int $flags = 0): void
{
    error_clear_last();
    $result = \imap_mail_move($imap, $message_nums, $mailbox, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * This function allows sending of emails with correct handling of
 * Cc and Bcc receivers.
 *
 * The parameters to, cc
 * and bcc are all strings and are all parsed
 * as RFC822 address lists.
 *
 * @param string $to The receiver
 * @param string $subject The mail subject
 * @param string $message The mail body, see imap_mail_compose
 * @param string $additional_headers As string with additional headers to be set on the mail
 * @param string $cc
 * @param string $bcc The receivers specified in bcc will get the
 * mail, but are excluded from the headers.
 * @param string $return_path Use this parameter to specify return path upon mail delivery failure.
 * This is useful when using PHP as a mail client for multiple users.
 * @throws ImapException
 *
 */
function imap_mail(string $to, string $subject, string $message, string $additional_headers = null, string $cc = null, string $bcc = null, string $return_path = null): void
{
    error_clear_last();
    if ($return_path !== null) {
        $result = \imap_mail($to, $subject, $message, $additional_headers, $cc, $bcc, $return_path);
    } elseif ($bcc !== null) {
        $result = \imap_mail($to, $subject, $message, $additional_headers, $cc, $bcc);
    } elseif ($cc !== null) {
        $result = \imap_mail($to, $subject, $message, $additional_headers, $cc);
    } elseif ($additional_headers !== null) {
        $result = \imap_mail($to, $subject, $message, $additional_headers);
    } else {
        $result = \imap_mail($to, $subject, $message);
    }
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Checks the current mailbox status on the server. It is similar to
 * imap_status, but will additionally sum up the size of
 * all messages in the mailbox, which will take some additional time to
 * execute.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @return \stdClass Returns the information in an object with following properties:
 *
 * Mailbox properties
 *
 *
 *
 * Date
 * date of last change (current datetime)
 *
 *
 * Driver
 * driver
 *
 *
 * Mailbox
 * name of the mailbox
 *
 *
 * Nmsgs
 * number of messages
 *
 *
 * Recent
 * number of recent messages
 *
 *
 * Unread
 * number of unread messages
 *
 *
 * Deleted
 * number of deleted messages
 *
 *
 * Size
 * mailbox size
 *
 *
 *
 *
 *
 * Returns FALSE on failure.
 * @throws ImapException
 *
 */
function imap_mailboxmsginfo($imap): \stdClass
{
    error_clear_last();
    $result = \imap_mailboxmsginfo($imap);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Decodes MIME message header extensions that are non ASCII text (see RFC2047).
 *
 * @param string $string The MIME text
 * @return array The decoded elements are returned in an array of objects, where each
 * object has two properties, charset and
 * text.
 *
 * If the element hasn't been encoded, and in other words is in
 * plain US-ASCII, the charset property of that element is
 * set to default.
 *
 * The function returns FALSE on failure.
 * @throws ImapException
 *
 */
function imap_mime_header_decode(string $string): array
{
    error_clear_last();
    $result = \imap_mime_header_decode($string);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Decode a modified UTF-7 (as specified in RFC 2060, section 5.1.3) string to UTF-8.
 *
 * @param string $string A string encoded in modified UTF-7.
 * @return string Returns string converted to UTF-8.
 * @throws ImapException
 *
 */
function imap_mutf7_to_utf8(string $string): string
{
    error_clear_last();
    $result = \imap_mutf7_to_utf8($string);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Gets the number of messages in the current mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @return int Return the number of messages in the current mailbox, as an integer.
 * @throws ImapException
 *
 */
function imap_num_msg($imap): int
{
    error_clear_last();
    $result = \imap_num_msg($imap);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Opens an IMAP stream to a mailbox.
 *
 * This function can also be used to open streams to POP3 and
 * NNTP servers, but some functions and features are only
 * available on IMAP servers.
 *
 * @param string $mailbox A mailbox name consists of a server and a mailbox path on this server.
 * The special name INBOX stands for the current users
 * personal mailbox. Mailbox names that contain international characters
 * besides those in the printable ASCII space have to be encoded with
 * imap_utf7_encode.
 *
 * The server part, which is enclosed in '{' and '}', consists of the servers
 * name or ip address, an optional port (prefixed by ':'), and an optional
 * protocol specification (prefixed by '/').
 *
 * The server part is mandatory in all mailbox
 * parameters.
 *
 * All names which start with { are remote names, and are
 * in the form "{" remote_system_name [":" port] [flags] "}"
 * [mailbox_name] where:
 *
 *
 *
 * remote_system_name - Internet domain name or
 * bracketed IP address of server.
 *
 *
 *
 *
 * port - optional TCP port number, default is the
 * default port for that service
 *
 *
 *
 *
 * flags - optional flags, see following table.
 *
 *
 *
 *
 * mailbox_name - remote mailbox name, default is INBOX
 *
 *
 *
 *
 *
 * Optional flags for names
 *
 *
 *
 * Flag
 * Description
 *
 *
 *
 *
 * /service=service
 * mailbox access service, default is "imap"
 *
 *
 * /user=user
 * remote user name for login on the server
 *
 *
 * /authuser=user
 * remote authentication user; if specified this is the user name
 * whose password is used (e.g. administrator)
 *
 *
 * /anonymous
 * remote access as anonymous user
 *
 *
 * /debug
 * record protocol telemetry in application's debug log
 *
 *
 * /secure
 * do not transmit a plaintext password over the network
 *
 *
 * /imap, /imap2,
 * /imap2bis, /imap4,
 * /imap4rev1
 * equivalent to /service=imap
 *
 *
 * /pop3
 * equivalent to /service=pop3
 *
 *
 * /nntp
 * equivalent to /service=nntp
 *
 *
 * /norsh
 * do not use rsh or ssh to establish a preauthenticated IMAP
 * session
 *
 *
 * /ssl
 * use the Secure Socket Layer to encrypt the session
 *
 *
 * /validate-cert
 * validate certificates from TLS/SSL server (this is the default
 * behavior)
 *
 *
 * /novalidate-cert
 * do not validate certificates from TLS/SSL server, needed if
 * server uses self-signed certificates
 *
 *
 * /tls
 * force use of start-TLS to encrypt the session, and reject
 * connection to servers that do not support it
 *
 *
 * /notls
 * do not do start-TLS to encrypt the session, even with servers
 * that support it
 *
 *
 * /readonly
 * request read-only mailbox open (IMAP only; ignored on NNTP, and
 * an error with SMTP and POP3)
 *
 *
 *
 *
 * @param string $user The user name
 * @param string $password The password associated with the user
 * @param int $flags The flags are a bit mask with one or more of
 * the following:
 *
 *
 *
 * OP_READONLY - Open mailbox read-only
 *
 *
 *
 *
 * OP_ANONYMOUS - Don't use or update a
 * .newsrc for news (NNTP only)
 *
 *
 *
 *
 * OP_HALFOPEN - For IMAP
 * and NNTP names, open a connection but
 * don't open a mailbox.
 *
 *
 *
 *
 * CL_EXPUNGE - Expunge mailbox automatically upon mailbox close
 * (see also imap_delete and
 * imap_expunge)
 *
 *
 *
 *
 * OP_DEBUG - Debug protocol negotiations
 *
 *
 *
 *
 * OP_SHORTCACHE - Short (elt-only) caching
 *
 *
 *
 *
 * OP_SILENT - Don't pass up events (internal use)
 *
 *
 *
 *
 * OP_PROTOTYPE - Return driver prototype
 *
 *
 *
 *
 * OP_SECURE - Don't do non-secure authentication
 *
 *
 *
 * @param int $retries Number of maximum connect attempts
 * @param array $options Connection parameters, the following (string) keys maybe used
 * to set one or more connection parameters:
 *
 *
 *
 * DISABLE_AUTHENTICATOR - Disable authentication properties
 *
 *
 *
 * @return resource Returns an IMAP\Connection instance on success.
 * @throws ImapException
 *
 */
function imap_open(string $mailbox, string $user, string $password, int $flags = 0, int $retries = 0, array $options = [])
{
    error_clear_last();
    $result = \imap_open($mailbox, $user, $password, $flags, $retries, $options);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Convert a quoted-printable string to an 8 bit string according to RFC2045, section 6.7.
 *
 * @param string $string A quoted-printable string
 * @return string Returns an 8 bits string.
 * @throws ImapException
 *
 */
function imap_qprint(string $string): string
{
    error_clear_last();
    $result = \imap_qprint($string);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * This function renames on old mailbox to new mailbox (see
 * imap_open for the format of
 * mbox names).
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $from The old mailbox name, see imap_open for more
 * information
 * @param string $to The new mailbox name, see imap_open for more
 * information
 * @throws ImapException
 *
 */
function imap_renamemailbox($imap, string $from, string $to): void
{
    error_clear_last();
    $result = \imap_renamemailbox($imap, $from, $to);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Returns a properly formatted email address as defined in RFC2822 given the needed information.
 *
 * @param string|null $mailbox The mailbox name, see imap_open for more
 * information
 * @param string|null $hostname The email host part
 * @param string|null $personal The name of the account owner
 * @return string Returns a string properly formatted email address as defined in RFC2822.
 * @throws ImapException
 *
 */
function imap_rfc822_write_address(?string $mailbox, ?string $hostname, ?string $personal): string
{
    error_clear_last();
    $result = \imap_rfc822_write_address($mailbox, $hostname, $personal);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Saves a part or the whole body of the specified message.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string|resource $file The path to the saved file as a string, or a valid file descriptor
 * returned by fopen.
 * @param int $message_num The message number
 * @param string $section The part number. It is a string of integers delimited by period which
 * index into a body part list as per the IMAP4 specification
 * @param int $flags A bitmask with one or more of the following:
 *
 *
 *
 * FT_UID - The message_num is a UID
 *
 *
 *
 *
 * FT_PEEK - Do not set the \Seen flag if
 * not already set
 *
 *
 *
 *
 * FT_INTERNAL - The return string is in
 * internal format, will not canonicalize to CRLF.
 *
 *
 *
 * @throws ImapException
 *
 */
function imap_savebody($imap, $file, int $message_num, string $section = "", int $flags = 0): void
{
    error_clear_last();
    $result = \imap_savebody($imap, $file, $message_num, $section, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Sets an upper limit quota on a per mailbox basis.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $quota_root The mailbox to have a quota set. This should follow the IMAP standard
 * format for a mailbox: user.name.
 * @param int $mailbox_size The maximum size (in KB) for the quota_root
 * @throws ImapException
 *
 */
function imap_set_quota($imap, string $quota_root, int $mailbox_size): void
{
    error_clear_last();
    $result = \imap_set_quota($imap, $quota_root, $mailbox_size);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Sets the ACL for a giving mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $mailbox The mailbox name, see imap_open for more
 * information
 * @param string $user_id The user to give the rights to.
 * @param string $rights The rights to give to the user. Passing an empty string will delete
 * acl.
 * @throws ImapException
 *
 */
function imap_setacl($imap, string $mailbox, string $user_id, string $rights): void
{
    error_clear_last();
    $result = \imap_setacl($imap, $mailbox, $user_id, $rights);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Causes a store to add the specified flag to the
 * flags set for the messages in the specified
 * sequence.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $sequence A sequence of message numbers. You can enumerate desired messages
 * with the X,Y syntax, or retrieve all messages
 * within an interval with the X:Y syntax
 * @param string $flag The flags which you can set are \Seen,
 * \Answered, \Flagged,
 * \Deleted, and \Draft as
 * defined by RFC2060.
 * @param int $options A bit mask that may contain the single option:
 *
 *
 *
 * ST_UID - The sequence argument contains UIDs
 * instead of sequence numbers
 *
 *
 *
 * @throws ImapException
 *
 */
function imap_setflag_full($imap, string $sequence, string $flag, int $options = 0): void
{
    error_clear_last();
    $result = \imap_setflag_full($imap, $sequence, $flag, $options);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Gets and sorts message numbers by the given parameters.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $criteria Criteria can be one (and only one) of the following:
 *
 *
 *
 * SORTDATE - message Date
 *
 *
 *
 *
 * SORTARRIVAL - arrival date
 *
 *
 *
 *
 * SORTFROM - mailbox in first From address
 *
 *
 *
 *
 * SORTSUBJECT - message subject
 *
 *
 *
 *
 * SORTTO - mailbox in first To address
 *
 *
 *
 *
 * SORTCC - mailbox in first cc address
 *
 *
 *
 *
 * SORTSIZE - size of message in octets
 *
 *
 *
 * @param int $reverse Whether to sort in reverse order.
 * @param int $flags The flags are a bitmask of one or more of the
 * following:
 *
 *
 *
 * SE_UID - Return UIDs instead of sequence numbers
 *
 *
 *
 *
 * SE_NOPREFETCH - Don't prefetch searched messages
 *
 *
 *
 * @param string $search_criteria IMAP2-format search criteria string. For details see
 * imap_search.
 * @param string $charset MIME character set to use when sorting strings.
 * @return array Returns an array of message numbers sorted by the given
 * parameters.
 * @throws ImapException
 *
 */
function imap_sort($imap, int $criteria, int $reverse, int $flags = 0, string $search_criteria = null, string $charset = null): array
{
    error_clear_last();
    if ($charset !== null) {
        $result = \imap_sort($imap, $criteria, $reverse, $flags, $search_criteria, $charset);
    } elseif ($search_criteria !== null) {
        $result = \imap_sort($imap, $criteria, $reverse, $flags, $search_criteria);
    } else {
        $result = \imap_sort($imap, $criteria, $reverse, $flags);
    }
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Gets status information about the given mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $mailbox The mailbox name, see imap_open for more
 * information
 * @param int $flags Valid flags are:
 *
 *
 *
 * SA_MESSAGES - set $status-&gt;messages to the
 * number of messages in the mailbox
 *
 *
 *
 *
 * SA_RECENT - set $status-&gt;recent to the number
 * of recent messages in the mailbox
 *
 *
 *
 *
 * SA_UNSEEN - set $status-&gt;unseen to the number
 * of unseen (new) messages in the mailbox
 *
 *
 *
 *
 * SA_UIDNEXT - set $status-&gt;uidnext to the next
 * uid to be used in the mailbox
 *
 *
 *
 *
 * SA_UIDVALIDITY - set $status-&gt;uidvalidity to a
 * constant that changes when uids for the mailbox may no longer be
 * valid
 *
 *
 *
 *
 * SA_ALL - set all of the above
 *
 *
 *
 * @return \stdClass This function returns an object containing status information.
 * The object has the following properties: messages,
 * recent, unseen,
 * uidnext, and uidvalidity.
 *
 * flags is also set, which contains a bitmask which can
 * be checked against any of the above constants.
 * @throws ImapException
 *
 */
function imap_status($imap, string $mailbox, int $flags): \stdClass
{
    error_clear_last();
    $result = \imap_status($imap, $mailbox, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Subscribe to a new mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $mailbox The mailbox name, see imap_open for more
 * information
 * @throws ImapException
 *
 */
function imap_subscribe($imap, string $mailbox): void
{
    error_clear_last();
    $result = \imap_subscribe($imap, $mailbox);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Gets a tree of a threaded message.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param int $flags
 * @return array imap_thread returns an associative array containing
 * a tree of messages threaded by REFERENCES.
 *
 * Every message in the current mailbox will be represented by three entries
 * in the resulting array:
 *
 *
 * $thread["XX.num"] - current message number
 *
 *
 * $thread["XX.next"]
 *
 *
 * $thread["XX.branch"]
 *
 *
 * @throws ImapException
 *
 */
function imap_thread($imap, int $flags = SE_FREE): array
{
    error_clear_last();
    $result = \imap_thread($imap, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Sets or fetches the imap timeout.
 *
 * @param int $timeout_type One of the following:
 * IMAP_OPENTIMEOUT,
 * IMAP_READTIMEOUT,
 * IMAP_WRITETIMEOUT, or
 * IMAP_CLOSETIMEOUT.
 * @param int $timeout The timeout, in seconds.
 * @return mixed If the timeout parameter is set, this function
 * returns TRUE on success.
 *
 * If timeout  is not provided or evaluates to -1,
 * the current timeout value of timeout_type is
 * returned as an integer.
 * @throws ImapException
 *
 */
function imap_timeout(int $timeout_type, int $timeout = -1)
{
    error_clear_last();
    $result = \imap_timeout($timeout_type, $timeout);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}


/**
 * Removes the deletion flag for a specified message, which is set by
 * imap_delete or imap_mail_move.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $message_nums A string representing one or more messages in IMAP4-style sequence format
 * ("n", "n:m", or combination of these
 * delimited by commas).
 * @param int $flags
 * @throws ImapException
 *
 */
function imap_undelete($imap, string $message_nums, int $flags = 0): void
{
    error_clear_last();
    $result = \imap_undelete($imap, $message_nums, $flags);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Unsubscribe from the specified mailbox.
 *
 * @param resource $imap An IMAP\Connection instance.
 * @param string $mailbox The mailbox name, see imap_open for more
 * information
 * @throws ImapException
 *
 */
function imap_unsubscribe($imap, string $mailbox): void
{
    error_clear_last();
    $result = \imap_unsubscribe($imap, $mailbox);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
}


/**
 * Encode a UTF-8 string to modified UTF-7 (as specified in RFC 2060, section 5.1.3).
 *
 * @param string $string A UTF-8 encoded string.
 * @return string Returns string converted to modified UTF-7.
 * @throws ImapException
 *
 */
function imap_utf8_to_mutf7(string $string): string
{
    error_clear_last();
    $result = \imap_utf8_to_mutf7($string);
    if ($result === false) {
        throw ImapException::createFromPhpError();
    }
    return $result;
}