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
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
|
<?php
/*
* email_message.php
*
* @(#) $Id: email_message.php,v 1.99 2013/09/08 22:44:46 mlemos Exp $
*
*
*/
/*
{metadocument}<?xml version="1.0" encoding="ISO-8859-1"?>
<class>
<package>net.manuellemos.mimemessage</package>
<version>@(#) $Id: email_message.php,v 1.99 2013/09/08 22:44:46 mlemos Exp $</version>
<copyright>Copyright © (C) Manuel Lemos 1999-2004</copyright>
<title>MIME E-mail message composing and sending</title>
<author>Manuel Lemos</author>
<authoraddress>mlemos-at-acm.org</authoraddress>
<documentation>
<idiom>en</idiom>
<purpose>Compose and send e-mail messages according to the MIME
standards.</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>mimemessage-dev</tt> mailing list. Just go to the mailing list
page to browse the list archives, learn how to to join and post
support request messages:<paragraphbreak />
<link>
<data>http://groups-beta.google.com/group/mimemessage-dev</data>
<url>http://groups-beta.google.com/group/mimemessage-dev</url>
</link></support>
<usage>To used this class just create a new object as follows, set any
variables to configure its behavior and call the functions you need
to compose and send your messages.<paragraphbreak />
<tt>require('email_message.php');<br />
<br />
$message_object = new email_message_class;<br /></tt><paragraphbreak />
<b>- Set the sender and recipients</b><paragraphbreak />
You can set the message sender and one or more recipient addresses
using the <functionlink>SetHeader</functionlink> or the
<functionlink>SetEncodedEmailHeader</functionlink> functions
specifying the addresses for the <tt>From</tt>, <tt>To</tt>,
<tt>Cc</tt> and <tt>Bcc</tt> headers.<paragraphbreak />
<b>- Formatting text messages</b><paragraphbreak />
You can use the <functionlink>WrapText</functionlink> to assure that
a text message does not have more than 75 columns by breaking the
longer lines between words.<paragraphbreak />
<paragraphbreak />
If you are composing a reply to another text message, you can use the
<functionlink>QuoteText</functionlink> function to conveniently mark
the text quoted from the original message.<paragraphbreak />
<b>- Add a plain text message body</b><paragraphbreak />
If the text of the message that you want to send only contains ASCII
characters (7 bits), use the
<functionlink>AddPlainTextPart</functionlink> function to add the
text to the message.<paragraphbreak />
<b>- Add a text message with non-ASCII characters</b><paragraphbreak />
If your message text may contains non-ASCII characters (8 bits or
more), use the
<functionlink>AddQuotedPrintableTextPart</functionlink> function
to add the text to the message.<paragraphbreak />
<paragraphbreak />
If the text uses a character set other than
<i>ISO-8859-1</i> (ISO Latin 1), set the
<variablelink>default_charset</variablelink> variable to change the
default character set.<paragraphbreak />
<b>- Setting the error message bounce address</b><paragraphbreak />
This class provides a means to specify the address where error
messages should be bounced in case it is not possible to deliver a
message. That can be done by setting the header <tt>Return-Path</tt>
with the <functionlink>SetHeader</functionlink>
function.<paragraphbreak />
<b>- Request message receipt notification</b><paragraphbreak />
If you would like to be receive an notification when a message that
is sent is received, just use the
<functionlink>SetHeader</functionlink> function with the
<tt>Disposition-Notification-To</tt> header to specify the address to
where you want to receive the notification message.<paragraphbreak />
Keep in mind that this header just expresses that you want to get a
receipt notification, but it may be denied or ignored by the
recipient, which does not mean the message was not
received.<paragraphbreak />
<b>- Avoding temporary delivery failure warning messages</b><paragraphbreak />
Sometimes it is not possible to deliver a message immediately due
to a networking failure or some other problem. In that case, the mail
transfer system usually leaves the message in a queue and keeps
retrying to deliver the message until it succeeds or it has reached
the limit number of days before it gives up. When it gives up the
the message is bounced to the return-path address.<paragraphbreak />
However some systems send a warning message to the original sender
when it is not delivered after the first few hours. This may be an
useful notification when the message is sent by a human but it maybe
inconvinient when you are sending messages to many users like for
instance newsletters or messages to subscribers of mailing lists.<paragraphbreak />
If you want to hint the mail transfer system to not send temporary
delivery failure warning messages, just use the
<functionlink>SetHeader</functionlink> function to set the
<tt>Precedence</tt> header to <tt>bulk</tt>.<paragraphbreak />
Setting this header this way is a convention used by mailing list
manager programs precisely for this purpose. It may also hint some
mail receiving systems to not send auto-response messages, for
instance when the recipient user is away on vaction. However, not all
systems are aware of this convention and still send auto-response
messages when you set this header.<paragraphbreak />
<b>- Send the message</b><paragraphbreak />
Once you have set the message sender, the recipients and added the
message text, use the <functionlink>Send</functionlink> function
to send the message. This class uses the PHP function <tt>mail()</tt>
to send messages.<paragraphbreak />
<paragraphbreak />
If for some reason you need to use a different message delivery
method, you may use one of the existing sub-classes that are
specialized in delivering messages by connecting to an SMTP server or
using directly the programs sendmail and qmail.<paragraphbreak />
<b>- Add an HTML message body</b><paragraphbreak />
If you want to send an HTML message you can use the
<functionlink>AddHTMLPart</functionlink> function if it contains
only ASCII characters. If it contains non-ASCII characters, you
should the <functionlink>AddQuotedPrintableHTMLPart</functionlink>
function instead.<paragraphbreak />
<b>- Add alternative text body for HTML messages</b><paragraphbreak />
Not every e-mail program can display HTML messages. Therefore, when
you send an HTML message, you should also include an alternative text
part to be displayed by programs that do not support HTML
messages.<paragraphbreak />
<paragraphbreak />
This is achieved by composing <tt>multipart/alternative</tt>
messages. This type of message is composed by creating the HTML
message part with the <functionlink>CreateHTMLPart</functionlink> or
the <functionlink>CreateQuotedPrintableHTMLPart</functionlink>
functions, then create the alternative text part with the
<functionlink>CreatePlainTextPart</functionlink> or the
<functionlink>CreateQuotedPrintableTextPart</functionlink>
functions, and finally use the
<functionlink>AddAlternativeMultipart</functionlink> function to add
an assembly of both message parts.<paragraphbreak />
Note that the text part should be the first to be specified in the
array of parts passed to the
<functionlink>AddAlternativeMultipart</functionlink> function, or
else it will not appear correctly.<paragraphbreak />
Despite this procedure adds a little complexity to the process of
sending HTML messages, it is the same procedure that is followed by
e-mail programs that are used by most people to send HTML
messages.<paragraphbreak />
Therefore, you are strongly recommended to follow the same procedure
because some of the modern spam filter programs discard HTML messages
without an alternative plain text part, as it constitutes a pattern
that identifies messages composed by some of the spam sending
programs.<paragraphbreak />
<b><link>
<data>- Embed images in HTML messages</data>
<anchor>embed-image</anchor>
</link></b><paragraphbreak />
One way to show an image in an HTML message is to use
<tt><img></tt> tag with <tt>src</tt> attribute set to the
remote site URL of the image that is meant to be displayed.
However, since the message recipient user may not be online when
they will check their e-mail, an image referenced this way may not
appear.<paragraphbreak />
Alternatively, an image file can be embedded in an HTML message using
<tt>multipart/related</tt> message parts. This type of message part
is composed by creating the image file part with the
<functionlink>CreateFilePart</functionlink> function.<paragraphbreak />
Then use the <functionlink>GetPartContentID</functionlink> function
the image part identifier text. Prepend the string
<stringvalue>cid:</stringvalue> to this identifier to form a special
URL that should be used in the HTML part to reference the image part
like this:<paragraphbreak />
<tt>$image_tag = <stringvalue><img src="cid:</stringvalue> .
$message_object->GetPartContentID($image_part) .
<stringvalue>"></stringvalue> ;</tt><paragraphbreak />
When you have composed the whole HTML document, create the HTML
message part with the <functionlink>CreateHTMLPart</functionlink> or
the <functionlink>CreateQuotedPrintableHTMLPart</functionlink>
functions, and finally use the
<functionlink>CreateRelatedMultipart</functionlink> function to
create a message part that can be added to the message with the
function <functionlink>AddAlternativeMultipart</functionlink> like
HTML messages with alternative text parts described
before.<paragraphbreak />
Note that the HTML part must be the first listed in the parts array
argument that is passed to the function
<functionlink>CreateRelatedMultipart</functionlink>, or else the
message may not appear correctly.<paragraphbreak />
Note also that when you are composing an HTML message with embedded
images and an alternative text part, first you need to create the
<tt>multipart/alternative</tt> part with the HTML and the text parts
using the <functionlink>CreateAlternativeMultipart</functionlink>
function, and then you add the <tt>multipart/related</tt> part to
the message with the
<functionlink>AddRelatedMultipart</functionlink> function,
passing an array of parts that lists first the
<tt>multipart/alternative</tt> part and then the image part created
before.<paragraphbreak />
<b>- Attach files to messages</b><paragraphbreak />
To send a message with attached files, it is necessary to compose a
<tt>multipart/mixed</tt> message. This is a type of message made by a
text or HTML part followed by one or more file
parts.<paragraphbreak />
If you add multiple parts to a message, this class implicitly turns
it into a <tt>multipart/mixed</tt> message. Therefore you only need
to use the function <functionlink>AddFilePart</functionlink> for each
file that you want to attach and the class will automatically
generate the message treating any parts added after the first as
attachments.<paragraphbreak />
<b>- Forward received messages</b><paragraphbreak />
To forward an e-mail message received from somewhere, just use the
function <functionlink>AddMessagePart</functionlink> passing the
message complete with the original headers and body data. The message
is forwarded as an attachment that most mail programs can
display.<paragraphbreak />
<b>- Sending messages to many recipients (mass or bulk mailing)</b><paragraphbreak />
Sending messages to many recipients is an activity also known as
mass or bulk mailing. There are several alternatives for mass
mailing. One way consists on specifying all recipient addresses
with the <tt>Bcc</tt> header, separating the addresses with commas
(,), or using the
<functionlink>SetMultipleEncodedEmailHeader</functionlink> function.
This way you only need to send one message that is distributed to all
recipients by your mail transfer system.<paragraphbreak />
Unfortunately, many mail account providers like Hotmail, tend to
consider messages sent this way as spam because the real recipients
addresses are not visible in <tt>To</tt> of <tt>Cc</tt> headers.
So, this method is no longer a good solution these
days.<paragraphbreak />
The alternative is to send a separate message to each recipient by
iteratively setting the <tt>To</tt> header with each recipient
address and calling the <functionlink>Send</functionlink> function.
This way tends to take too much time and CPU as the number of
recipients grow.<paragraphbreak />
When sending messages to many recipients, call the
<functionlink>SetBulkMail</functionlink> function to hint the class
to optimize the way it works to make the delivery of the messages
more efficient and eventually faster.<paragraphbreak />
The actual optimizations that are performed depend on the delivery
method that is used by this class or any of its subclasses
specialized on the different delivery methods that are supported.
Check the documentation of the subclass that you use to learn about
the optimizations that are performed, if any.<paragraphbreak />
If you intend to send messages with the same body to all recipients,
the class can optimize the generation of the messages and reduce
significantly the composition time if you set the
<variablelink>cache_body</variablelink> variable to
<tt><booleanvalue>1</booleanvalue></tt>.<paragraphbreak />
If you really need to personalize the content of a message part with
different text, HTML or file to each recipient, you should use the
<functionlink>ReplacePart</functionlink> function to avoid as much
as possible the overhead of composing a new message to each of the
recipients of the mailing.<paragraphbreak />
If you are sending personalized messages to multiple recipients but
the messages include attached or embedded files that are the same
for all recipients, you should also set the
<stringvalue>Cached</stringvalue> option of the <argumentlink>
<argument>file</argument>
<function>CreateFilePart</function>
</argumentlink> parameter of the
<functionlink>CreateFilePart</functionlink> function.<paragraphbreak />
Other than that, take a look at the documentation of the this class
sub-classes that may be used in your PHP environment, as these may
provide more efficient delivery solutions for mass mailing.<paragraphbreak />
<b>- Error handling</b><paragraphbreak />
Most of the functions of this class that may fail, return an error
message string that describes the error that has occurred. If there
was no error, the functions return an empty string.<paragraphbreak />
Verifying the return value of all the functions to determine
whether there was an error is a tedious task to implement for most
developers. To avoid this problem, this class supports <i>cumulative
error checking</i>.<paragraphbreak />
Cumulative error checking means that when an error occurs, the class
stores the error message in the <variablelink>error</variablelink>
variable. Then, when another function that may fail is called, it
does nothing and immediately returns the same error
message.<paragraphbreak />
This way, the developers only need to check the return value of the
last function that is called, which is usually the
<functionlink>Send</functionlink> function.
</usage>
</documentation>
{/metadocument}
*/
class email_message_class
{
/* Private variables */
var $headers=array("To"=>"","Subject"=>"");
var $body=-1;
var $body_parts=0;
var $parts=array();
var $total_parts=0;
var $free_parts=array();
var $total_free_parts=0;
var $delivery=array("State"=>"");
var $next_token="";
var $php_version=0;
var $mailings=array();
var $last_mailing=0;
var $header_length_limit=512;
var $auto_message_id=1;
var $mailing_path="";
var $body_cache=array();
var $line_break="\n";
var $line_length=76;
var $ruler="_";
var $email_address_pattern="([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,6}";
var $bulk_mail=0;
/* Public variables */
/*
{metadocument}
<variable>
<name>email_regular_expression</name>
<type>STRING</type>
<value>^([-!#$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+\.)+[a-zA-Z]{2,6}$</value>
<documentation>
<purpose>Specify the regular expression that is used by the
<functionlink>ValidateEmailAddress</functionlink> function to
verify whether a given e-mail address may be valid.</purpose>
<usage>Do not change this variable unless you have reason to believe
that it is rejecting existing e-mail addresses that are known to be
valid.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $email_regular_expression="^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,6}\$";
/*
{metadocument}
<variable>
<name>mailer</name>
<type>STRING</type>
<value>http://www.phpclasses.org/mimemessage $Revision: 1.99 $</value>
<documentation>
<purpose>Specify the base text that is used identify the name and the
version of the class that is used to send the message by setting an
implicit the <tt>X-Mailer</tt> message header. This is meant
mostly to assist on the debugging of delivery problems.</purpose>
<usage>Change this to set another mailer identification string or
leave it to an empty string to prevent that the <tt>X-Mailer</tt>
header be added to the message.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $mailer='';
/*
{metadocument}
<variable>
<name>mailer_delivery</name>
<type>STRING</type>
<value>mail</value>
<documentation>
<purpose>Specify the text that is used to identify the mail
delivery class or sub-class. This text is appended to the
<tt>X-Mailer</tt> header text defined by the
<variablelink>mailer</variablelink> variable.</purpose>
<usage>This variable should only be redefined by the different mail
delivery sub-classes.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $mailer_delivery='mail';
/*
{metadocument}
<variable>
<name>default_charset</name>
<type>STRING</type>
<value>ISO-8859-1</value>
<documentation>
<purpose>Specify the default character set to be assumed for the
message headers and body text.</purpose>
<usage>Change this variable to the correct character set name if it
is different than the default.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $default_charset="ISO-8859-1";
/*
{metadocument}
<variable>
<name>line_quote_prefix</name>
<type>STRING</type>
<value>> </value>
<documentation>
<purpose>Specify the default line quote prefix text used by the
<functionlink>QuoteText</functionlink> function.</purpose>
<usage>Change it only if you prefer to quote lines marking them with
a different line prefix.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $line_quote_prefix="> ";
/*
{metadocument}
<variable>
<name>break_long_lines</name>
<type>BOOLEAN</type>
<value>1</value>
<documentation>
<purpose>Determine whether lines exceeding the length limit will be
broken by the line break character when using the
<functionlink>WrapText</functionlink> function.</purpose>
<usage>Change it only if you want to avoid breaking long lines
without any space characters, like for instance of messages with
long URLs.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $break_long_lines=1;
/*
{metadocument}
<variable>
<name>file_buffer_length</name>
<type>INTEGER</type>
<value>8000</value>
<documentation>
<purpose>Specify the length of the buffer that is used to read
files in chunks of limited size.</purpose>
<usage>The default value may be increased if you have plenty of
memory and want to benefit from additional speed when processing
the files that are used to compose messages.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $file_buffer_length=8000;
/*
{metadocument}
<variable>
<name>debug</name>
<type>STRING</type>
<value></value>
<documentation>
<purpose>Specify the name of a function that is called whenever an
error occurs.</purpose>
<usage>If you need to track the errors that may happen during the use
of the class, set this variable to the name of a callback function.
It should take only one argument that is the error message. When
this variable is set to an empty string, no debug callback function
is called.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $debug="";
/*
{metadocument}
<variable>
<name>cache_body</name>
<type>BOOLEAN</type>
<value>0</value>
<documentation>
<purpose>Specify whether the message bodies that are generated by the
class before sending, should be cached in memory to be reused on
the next message delivery.</purpose>
<usage>Set this variable to <tt><booleanvalue>1</booleanvalue></tt>
if you intend to send the a message with the same body to many
recipients, so the class avoids the overhead of regenerating
messages with the same content.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $cache_body=0;
/*
{metadocument}
<variable>
<name>error</name>
<type>STRING</type>
<value></value>
<documentation>
<purpose>Store the last error return by any function that may fail
due to some error.</purpose>
<usage>Do not change this variable value unless you intend to clear
the error status by setting it to an empty string.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $error="";
/*
{metadocument}
<variable>
<name>localhost</name>
<type>STRING</type>
<value></value>
<documentation>
<purpose>Specify the domain name of the computer sending the
message.</purpose>
<usage>This value is used as default domain of the sender e-mail
address when generating automatic <tt>Message-Id</tt>
headers.</usage>
</documentation>
</variable>
{/metadocument}
*/
var $localhost="";
/* Private methods */
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 GetFilenameExtension($filename)
{
return(GetType($dot=strrpos($filename,"."))=="integer" ? substr($filename,$dot) : "");
}
Function OutputError($error)
{
if(strcmp($function=$this->debug,"")
&& strcmp($error,""))
$function($error);
return($this->error=$error);
}
Function OutputPHPError($error, &$php_error_message)
{
if(IsSet($php_error_message)
&& strlen($php_error_message))
$error.=": ".$php_error_message;
return($this->OutputError($error));
}
Function GetPHPVersion()
{
if($this->php_version==0)
{
$version=explode(".",function_exists("phpversion") ? phpversion() : "3.0.7");
$this->php_version=$version[0]*1000000+$version[1]*1000+$version[2];
}
return($this->php_version);
}
Function EscapePattern($pattern)
{
return('/'.str_replace('/', '\\/', $pattern).'/');
}
Function GetRFC822Addresses($address,&$addresses)
{
if(function_exists("imap_rfc822_parse_adrlist"))
{
if(GetType($parsed_addresses=@imap_rfc822_parse_adrlist($address,$this->localhost))!="array")
return("it was not specified a valid address list");
for($entry=0;$entry<count($parsed_addresses);++$entry)
{
if(!IsSet($parsed_addresses[$entry]->host)
|| $parsed_addresses[$entry]->host==".SYNTAX-ERROR.")
return($parsed_addresses[$entry]->mailbox." .SYNTAX-ERROR.");
$parsed_address=$parsed_addresses[$entry]->mailbox."@".$parsed_addresses[$entry]->host;
if(IsSet($addresses[$parsed_address]))
++$addresses[$parsed_address];
else
$addresses[$parsed_address]=1;
}
}
else
{
$length=strlen($address);
for($position=0;$position<$length;)
{
$match=preg_split($this->EscapePattern($this->email_address_pattern),strtolower(substr($address,$position)),2);
if(count($match)<2)
break;
$position+=strlen($match[0]);
$next_position=$length-strlen($match[1]);
$found=substr($address,$position,$next_position-$position);
if(!strcmp($found,""))
break;
if(IsSet($addresses[$found]))
++$addresses[$found];
else
$addresses[$found]=1;
$position=$next_position;
}
}
return("");
}
Function FormatHeader($header_name,$header_value)
{
$length=strlen($header_value);
for($header_data="",$header_line=$header_name.": ",$line_length=strlen($header_line),$position=0;$position<$length;)
{
for($space=$position,$line_length=strlen($header_line);$space<$length;)
{
if(GetType($next=strpos($header_value," ",$space+1))!="integer")
$next=$length;
if($next-$position+$line_length>$this->header_length_limit)
{
if($space==$position)
$space=$next;
break;
}
$space=$next;
}
$header_data.=$header_line.substr($header_value,$position,$space-$position);
if($space<$length)
$header_line="";
$position=$space;
if($position<$length)
$header_data.=$this->line_break;
}
return($header_data);
}
Function GenerateMessageID($sender)
{
$micros=$this->Tokenize(microtime()," ");
$seconds=$this->Tokenize("");
$local=$this->Tokenize($sender,"@");
$host=$this->Tokenize(" @");
if(strlen($host)
&& $host[strlen($host)-1]=="-")
$host=substr($host,0,strlen($host)-1);
return($this->FormatHeader("Message-ID", "<".strftime("%Y%m%d%H%M%S", $seconds).substr($micros,1,5).".".preg_replace('/[^A-Za-z]/', '-', $local)."@".preg_replace('/[^.A-Za-z_-]/', '', $host).">"));
}
Function SendMail($to, $subject, $body, $headers, $return_path)
{
$ini_safemode_chk = false;
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$ini_safemode_chk = ini_get('safe_mode');
}
if(!function_exists("mail"))
return($this->OutputError("the mail() function is not available in this PHP installation"));
if(strlen($return_path))
{
if(!defined("PHP_OS"))
return($this->OutputError("it is not possible to set the Return-Path header with your PHP version"));
if(!strcmp(substr(PHP_OS,0,3),"WIN"))
return($this->OutputError("it is not possible to set the Return-Path header directly from a PHP script on Windows"));
if($this->GetPHPVersion()<4000005)
return($this->OutputError("it is not possible to set the Return-Path header in PHP version older than 4.0.5"));
if(function_exists("ini_get")
&& $ini_safemode_chk)
return($this->OutputError("it is not possible to set the Return-Path header due to PHP safe mode restrictions"));
$success=@mail($to,$subject,$body,$headers,"-f".$return_path);
}
else
$success=@mail($to,$subject,$body,$headers);
return($success ? "" : $this->OutputPHPError("it was not possible to send e-mail message", error_get_last()['message']));
}
Function StartSendingMessage()
{
if(strcmp($this->delivery["State"],""))
return($this->OutputError("the message was already started to be sent"));
$this->delivery=array("State"=>"SendingHeaders");
return("");
}
Function SendMessageHeaders($headers)
{
if(strcmp($this->delivery["State"],"SendingHeaders"))
{
if(!strcmp($this->delivery["State"],""))
return($this->OutputError("the message was not yet started to be sent"));
else
return($this->OutputError("the message headers were already sent"));
}
$this->delivery["Headers"]=$headers;
$this->delivery["State"]="SendingBody";
return("");
}
Function SendMessageBody($data)
{
if(strcmp($this->delivery["State"],"SendingBody"))
return($this->OutputError("the message headers were not yet sent"));
if(IsSet($this->delivery["Body"]))
$this->delivery["Body"].=$data;
else
$this->delivery["Body"]=$data;
return("");
}
Function EndSendingMessage()
{
if(strcmp($this->delivery["State"],"SendingBody"))
return($this->OutputError("the message body data was not yet sent"));
if(!IsSet($this->delivery["Headers"])
|| count($this->delivery["Headers"])==0)
return($this->OutputError("message has no headers"));
$line_break=((defined("PHP_OS") && !strcmp(substr(PHP_OS,0,3),"WIN")) ? "\r\n" : $this->line_break);
$headers=$this->delivery["Headers"];
for($has=array(),$headers_text="",$header=0,Reset($headers);$header<count($headers);Next($headers),++$header)
{
$header_name=Key($headers);
switch(strtolower($header_name))
{
case "to":
case "subject":
$has[strtolower($header_name)]=$headers[$header_name];
break;
case "cc":
case "bcc":
case "from":
case "return-path":
case "message-id":
$has[strtolower($header_name)]=$headers[$header_name];
default:
$header_line=$header_name.": ".$headers[$header_name];
if(strlen($headers_text))
$headers_text.=$this->line_break.$header_line;
else
$headers_text=$header_line;
}
}
if(strlen($has["to"])==0
&& !IsSet($has["cc"])
&& !IsSet($has["bcc"]))
return($this->OutputError("it were not specified a valid To:, Cc: or Bcc: headers"));
if(!IsSet($has["subject"]))
return($this->OutputError("it was not specified a valid Subject: header"));
if(!IsSet($has["message-id"])
&& $this->auto_message_id)
{
$sender = $senders = array();
if(IsSet($has["return-path"]))
$sender[] = $has["return-path"];
if(IsSet($has["from"]))
$sender[] = $has["from"];
$sender[] = $has["to"];
$ts = count($sender);
for($s = 0; $s < $ts; ++$s)
{
$error = $this->GetRFC822Addresses($sender[$s], $senders);
if(strlen($error) == 0
&& count($senders))
break;
}
if(count($senders) == 0)
return('it was not specified a valid sender address'.(strlen($error) ? ': '.$error : ''));
Reset($senders);
$sender=Key($senders);
$header_line=$this->GenerateMessageID($sender);
if(strlen($headers_text))
$headers_text.=$this->line_break.$header_line;
else
$headers_text=$header_line;
}
if(strcmp($error=$this->SendMail(strlen($has["to"]) ? $has["to"] : (IsSet($has["cc"]) ? "" : "undisclosed-recipients: ;"), $has["subject"], $this->delivery["Body"], $headers_text, IsSet($has["return-path"]) ? $has["return-path"] : ""),""))
return($error);
$this->delivery=array("State"=>"");
return("");
}
Function StopSendingMessage()
{
$this->delivery=array("State"=>"");
return("");
}
Function GetPartBoundary($part)
{
if(!IsSet($this->parts[$part]["BOUNDARY"]))
$this->parts[$part]["BOUNDARY"]=md5(uniqid($part.time()));
}
Function GetPartHeaders(&$headers,$part)
{
if(IsSet($this->parts[$part]['CachedHeaders']))
{
$headers = $this->parts[$part]['CachedHeaders'];
return('');
}
if(!IsSet($this->parts[$part]["Content-Type"]))
return($this->OutputError("it was added a part without Content-Type: defined"));
$type=$this->Tokenize($full_type=strtolower($this->parts[$part]["Content-Type"]),"/");
$sub_type=$this->Tokenize("");
switch($type)
{
case "text":
case "image":
case "audio":
case "video":
case "application":
case "message":
if(IsSet($this->parts[$part]["NAME"]))
$filename = $this->QuotedPrintableEncode($this->parts[$part]["NAME"], $this->default_charset, 1, 1);
$headers["Content-Type"]=$full_type.(IsSet($this->parts[$part]["CHARSET"]) ? "; charset=".$this->parts[$part]["CHARSET"] : "").(IsSet($this->parts[$part]["NAME"]) ? "; name=\"".$filename."\"" : "");
if(IsSet($this->parts[$part]["Content-Transfer-Encoding"]))
$headers["Content-Transfer-Encoding"]=$this->parts[$part]["Content-Transfer-Encoding"];
if(IsSet($this->parts[$part]["DISPOSITION"])
&& strlen($this->parts[$part]["DISPOSITION"]))
$headers["Content-Disposition"]=$this->parts[$part]["DISPOSITION"].(IsSet($this->parts[$part]["NAME"]) ? "; filename=\"".$filename."\"" : "");
break;
case "multipart":
switch($sub_type)
{
case "alternative":
case "related":
case "mixed":
case "parallel":
$this->GetPartBoundary($part);
$headers["Content-Type"]=$full_type."; boundary=\"".$this->parts[$part]["BOUNDARY"]."\"";
break;
default:
return($this->OutputError("multipart Content-Type sub_type $sub_type not yet supported"));
}
break;
default:
return($this->OutputError("Content-Type: $full_type not yet supported"));
}
if(IsSet($this->parts[$part]["Content-ID"]))
$headers["Content-ID"]="<".$this->parts[$part]["Content-ID"].">";
if(IsSet($this->parts[$part]['Cache'])
&& $this->parts[$part]['Cache'])
$this->parts[$part]['CachedHeaders'] = $headers;
return("");
}
Function GetPartBody(&$body,$part)
{
if(IsSet($this->parts[$part]['CachedBody']))
{
$body = $this->parts[$part]['CachedBody'];
return('');
}
if(!IsSet($this->parts[$part]["Content-Type"]))
return($this->OutputError("it was added a part without Content-Type: defined"));
$type=$this->Tokenize($full_type=strtolower($this->parts[$part]["Content-Type"]),"/");
$sub_type=$this->Tokenize("");
$body="";
switch($type)
{
case "text":
case "image":
case "audio":
case "video":
case "application":
case "message":
if(IsSet($this->parts[$part]["FILENAME"]))
{
$size=@filesize($this->parts[$part]["FILENAME"]);
if(!($file=@fopen($this->parts[$part]["FILENAME"],"rb")))
return($this->OutputPHPError("could not open part file ".$this->parts[$part]["FILENAME"], error_get_last()['message']));
while(!feof($file))
{
if(GetType($block=@fread($file,$this->file_buffer_length))!="string")
{
fclose($file);
return($this->OutputPHPError("could not read part file", error_get_last()['message']));
}
$body.=$block;
}
fclose($file);
if((GetType($size)=="integer"
&& strlen($body)>$size)
|| (function_exists("get_magic_quotes_runtime")
&& get_magic_quotes_runtime()))
$body=StripSlashes($body);
if(GetType($size)=="integer"
&& strlen($body)!=$size)
return($this->OutputError("the length of the file that was read does not match the size of the part file ".$this->parts[$part]["FILENAME"]." due to possible data corruption"));
}
else
{
if(!IsSet($this->parts[$part]["DATA"]))
return($this->OutputError("it was added a part without a body PART"));
$body=$this->parts[$part]["DATA"];
}
$encoding=(IsSet($this->parts[$part]["Content-Transfer-Encoding"]) ? strtolower($this->parts[$part]["Content-Transfer-Encoding"]) : "");
switch($encoding)
{
case "base64":
$body=chunk_split(base64_encode($body), $this->line_length, $this->line_break);
break;
case "":
case "quoted-printable":
case "7bit":
break;
default:
return($this->OutputError($encoding." is not yet a supported encoding type"));
}
break;
case "multipart":
switch($sub_type)
{
case "alternative":
case "related":
case "mixed":
case "parallel":
$this->GetPartBoundary($part);
$boundary="--".$this->parts[$part]["BOUNDARY"];
$parts=count($this->parts[$part]["PARTS"]);
$b = $this->line_break;
$lb = strlen($b);
for($multipart=0;$multipart<$parts;$multipart++)
{
if(strlen($body) >= $lb
&& strcmp(substr($body, -$lb), $b))
$body.=$b;
$body.=$boundary.$this->line_break;
$part_headers=array();
$sub_part=$this->parts[$part]["PARTS"][$multipart];
if(strlen($error=$this->GetPartHeaders($part_headers,$sub_part)))
return($error);
for($part_header=0,Reset($part_headers);$part_header<count($part_headers);$part_header++,Next($part_headers))
{
$header=Key($part_headers);
$body.=$header.": ".$part_headers[$header].$b;
}
$body.=$b;
if(strlen($error=$this->GetPartBody($part_body,$sub_part)))
return($error);
$body.=$part_body;
}
if(strlen($body) >= $lb
&& strcmp(substr($body, -$lb), $b))
$body.=$b;
$body.=$boundary."--".$b;
break;
default:
return($this->OutputError("multipart Content-Type sub_type $sub_type not yet supported"));
}
break;
default:
return($this->OutputError("Content-Type: $full_type not yet supported"));
}
if(IsSet($this->parts[$part]['Cache'])
&& $this->parts[$part]['Cache'])
$this->parts[$part]['CachedBody'] = $body;
return("");
}
/* Public functions */
/*
{metadocument}
<function>
<name>ValidateEmailAddress</name>
<type>BOOLEAN</type>
<documentation>
<purpose>Determine whether a given e-mail address may be
valid.</purpose>
<usage>Just pass the e-mail <argumentlink>
<function>ValidateEmailAddress</function>
<argument>address</argument>
</argumentlink> to be checked as function argument. This function
uses the regular expression defined by the
<variablelink>email_regular_expression</variablelink> variable to
check the address.</usage>
<returnvalue>The function returns
<tt><booleanvalue>1</booleanvalue></tt> if the specified address
may be valid.</returnvalue>
</documentation>
<argument>
<name>address</name>
<type>STRING</type>
<documentation>
<purpose>Specify the e-mail address to be validated.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function ValidateEmailAddress($address)
{
return(preg_match('/'.str_replace('/', '\\/', $this->email_regular_expression).'/i',$address));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function EncodeCharacter($matches)
{
return sprintf('=%02X', Ord($matches[1]));
}
Function QuotedPrintableEncode($text, $header_charset='', $break_lines=1, $email_header = 0)
{
$ln=strlen($text);
$h=(strlen($header_charset)>0);
if($h)
{
$encode = array(
'='=>1,
'?'=>1,
'_'=>1,
'('=>1,
')'=>1,
'<'=>1,
'>'=>1,
'@'=>1,
','=>1,
';'=>1,
'"'=>1,
'\\'=>1,
'['=>1,
']'=>1,
':'=>1,
/*
'/'=>1,
'.'=>1,
*/
);
$s=($email_header ? $encode : array());
$b=$space=$break_lines=0;
for($i=0; $i<$ln; ++$i)
{
$c = $text[$i];
if(IsSet($s[$c]))
{
$b=1;
break;
}
switch($o=Ord($c))
{
case 9:
case 32:
$space=$i+1;
$b=1;
break 2;
case 10:
case 13:
break 2;
default:
if($o<32
|| $o>127)
{
$b=1;
$s = $encode;
break 2;
}
}
}
if($i==$ln)
return($text);
if($space>0)
return(substr($text,0,$space).($space<$ln ? $this->QuotedPrintableEncode(substr($text,$space), $header_charset, $break_lines, $email_header) : ""));
}
elseif(function_exists('quoted_printable_encode'))
{
$different = strcmp($this->line_break, "\r\n");
if($different)
$text = str_replace($this->line_break, "\r\n", str_replace("\r\n", $this->line_break, $text));
$encoded = preg_replace_callback('/^(f|F|\\.)/m', array($this, 'EncodeCharacter'), quoted_printable_encode($text));
if($different)
$encoded = str_replace("\r\n", $this->line_break, $encoded);
return $encoded;
}
for($w=$e='',$n=0, $l=0,$i=0;$i<$ln; ++$i)
{
$c = $text[$i];
$o=Ord($c);
$en=0;
switch($o)
{
case 9:
case 32:
if(!$h)
{
$w=$c;
$c='';
}
else
{
if($b)
{
if($o==32)
$c='_';
else
$en=1;
}
}
break;
case 10:
case 13:
if(strlen($w))
{
if($break_lines
&& $l+3>75)
{
$e.='='.$this->line_break;
$l=0;
}
$e.=sprintf('=%02X',Ord($w));
$l+=3;
$w='';
}
$e.=$c;
if($h)
$e.="\t";
$l=0;
continue 2;
case 46:
case 70:
case 102:
$en=(!$h && ($l==0 || $l+1>75));
break;
default:
if($o>127
|| $o<32
|| !strcmp($c,'='))
$en=1;
elseif($h
&& IsSet($s[$c]))
$en=1;
break;
}
if(strlen($w))
{
if($break_lines
&& $l+1>75)
{
$e.='='.$this->line_break;
$l=0;
}
$e.=$w;
++$l;
$w='';
}
if(strlen($c))
{
if($en)
{
$c=sprintf('=%02X',$o);
$el=3;
$n=1;
$b=1;
}
else
$el=1;
if($break_lines
&& $l+$el>75)
{
$e.='='.$this->line_break;
$l=0;
}
$e.=$c;
$l+=$el;
}
}
if(strlen($w))
{
if($break_lines
&& $l+3>75)
$e.='='.$this->line_break;
$e.=sprintf('=%02X',Ord($w));
}
if($h
&& $n)
return('=?'.$header_charset.'?q?'.$e.'?=');
else
return($e);
}
/*
{metadocument}
<function>
<name>WrapText</name>
<type>STRING</type>
<documentation>
<purpose>Split a text in lines that do not exceed the length limit
avoiding to break it in the middle of any words.</purpose>
<usage>Just pass the <argumentlink>
<function>WrapText</function>
<argument>text</argument>
</argumentlink> to be wrapped.</usage>
<returnvalue>The wrapped text eventually broken in multiple lines
that do not exceed the line length limit.</returnvalue>
</documentation>
<argument>
<name>text</name>
<type>STRING</type>
<documentation>
<purpose>Text to be wrapped.</purpose>
</documentation>
</argument>
<argument>
<name>line_length</name>
<type>INTEGER</type>
<defaultvalue>0</defaultvalue>
<documentation>
<purpose>Line length limit. Pass a value different than
<tt><integervalue>0</integervalue></tt> to use a line length
limit other than the default of 75 characters.</purpose>
</documentation>
</argument>
<argument>
<name>line_break</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character sequence that is used to break the lines longer
than the length limit. Pass a non-empty to use a line breaking
sequence other than the default
<tt><stringvalue> </stringvalue></tt>.</purpose>
</documentation>
</argument>
<argument>
<name>line_prefix</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character sequence that is used to insert in the beginning
of all lines.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function WrapText($text,$line_length=0,$line_break="",$line_prefix="")
{
if(strlen($line_break)==0)
$line_break=$this->line_break;
if($line_length==0)
$line_length=$this->line_length;
$lines=explode("\n",str_replace("\r","\n",str_replace("\r\n","\n",$text)));
for($wrapped="",$line=0;$line<count($lines);++$line)
{
if(strlen($text_line=$lines[$line]))
{
for(;strlen($text_line=$line_prefix.$text_line)>$line_length;)
{
if(GetType($cut=strrpos(substr($text_line,0,$line_length)," "))!="integer"
|| $cut<strlen($line_prefix))
{
if($this->break_long_lines)
{
$wrapped.=substr($text_line,0,$line_length).$line_break;
$cut=$line_length;
}
elseif(GetType($cut=strpos($text_line," ",$line_length))=="integer")
{
$wrapped.=substr($text_line, 0, $cut).$line_break;
++$cut;
}
else
{
$wrapped.=$text_line.$line_break;
$cut=strlen($text_line);
}
}
else
{
$wrapped.=substr($text_line,0,$cut).$line_break;
++$cut;
}
$text_line=substr($text_line,$cut);
}
}
$wrapped.=$text_line.$line_break;
}
return($wrapped);
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>CenterText</name>
<type>STRING</type>
<documentation>
<purpose>Center a text in the middle of line.</purpose>
<usage>Just pass the <argumentlink>
<function>CenterText</function>
<argument>text</argument>
</argumentlink> to be centered.</usage>
<returnvalue>The centered text.</returnvalue>
</documentation>
<argument>
<name>text</name>
<type>STRING</type>
<documentation>
<purpose>Text to be centered.</purpose>
</documentation>
</argument>
<argument>
<name>line_length</name>
<type>INTEGER</type>
<defaultvalue>0</defaultvalue>
<documentation>
<purpose>Line length limit. Pass a value different than
<tt><integervalue>0</integervalue></tt> to use a line length
limit other than the default of 75 characters.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function CenterText($text, $line_length=0)
{
if($line_length==0)
$line_length=$this->line_length;
$length = strlen($text);
if($length<$line_length)
$text = str_repeat(' ', ($line_length-$length)/2).$text;
return($text);
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>Ruler</name>
<type>STRING</type>
<documentation>
<purpose>Generate a line with characters that can be displayed as a
separator ruler in a text message.</purpose>
<returnvalue>The ruler line string.</returnvalue>
</documentation>
<argument>
<name>line_length</name>
<type>INTEGER</type>
<defaultvalue>0</defaultvalue>
<documentation>
<purpose>Line length limit. Pass a value different than
<tt><integervalue>0</integervalue></tt> to use a line length
limit other than the default of 75 characters.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function Ruler($line_length=0)
{
if($line_length==0)
$line_length=$this->line_length;
return(str_repeat($this->ruler, $line_length));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>QuoteText</name>
<type>STRING</type>
<documentation>
<purpose>Mark a text block to appear like in reply messages composed
with common e-mail programs that include text from the original
message being replied.</purpose>
<usage>Just pass the <argumentlink>
<function>QuoteText</function>
<argument>text</argument>
</argumentlink> to be marked as a quote.</usage>
<returnvalue>The quoted text with all lines prefixed with a quote
prefix mark.</returnvalue>
</documentation>
<argument>
<name>text</name>
<type>STRING</type>
<documentation>
<purpose>Text to be quoted.</purpose>
</documentation>
</argument>
<argument>
<name>quote_prefix</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character sequence that is inserted in the beginning of
all lines as a quote mark. Set to an empty string to tell the
function to use the default specified by the
<variablelink>line_quote_prefix</variablelink> variable.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function QuoteText($text,$quote_prefix="")
{
if(strlen($quote_prefix)==0)
$quote_prefix=$this->line_quote_prefix;
return($this->WrapText($text,$line_length=0,$line_break="",$quote_prefix));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>SetHeader</name>
<type>STRING</type>
<documentation>
<purpose>Set the value of a message header.</purpose>
<usage>Use this function to set the values of the headers of the
message that may be needed. There are some message headers that are
automatically set by the class when the message is sent. Others
must be defined before sending. Here follows the list of the names
of the headers that must be set before sending:<paragraphbreak />
<paragraphbreak />
<b>Message subject</b> - <tt>Subject</tt><paragraphbreak />
<b>Sender address</b> - <tt>From</tt><paragraphbreak />
<b>Recipient addresses</b> - <tt>To</tt>, <tt>Cc</tt> and
<tt>Bcc</tt><paragraphbreak />
Each of the recipient address headers may contain one or more
addresses. Multiple addresses must be separated by a comma and a
space.<paragraphbreak />
<b>Return path address</b> - <tt>Return-Path</tt><paragraphbreak />
Optional header to specify the address where the message should be
bounced in case it is not possible to deliver it.<paragraphbreak />
In reality this is a virtual header. This means that adding this
header to a message will not do anything by itself. However, this
class looks for this header to adjust the message delivery
procedure in such way that the Message Transfer Agent (MTA) system
is hinted to direct any bounced messages to the address specified
by this header.<paragraphbreak />
Note that under some systems there is no way to set the return path
address programmatically. This is the case when using the PHP
<tt>mail()</tt> function under Windows where the return path
address should be set in the <tt>php.ini</tt> configuration
file.<paragraphbreak />
Keep in mind that even when it is possible to set the return path
address, the systems of some e-mail account providers may ignore
this address and send bounced messages to the sender address. This
is a bug of those systems. There is nothing that can be done other
than complain.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>header</name>
<type>STRING</type>
<documentation>
<purpose>Name of the header.</purpose>
</documentation>
</argument>
<argument>
<name>value</name>
<type>STRING</type>
<documentation>
<purpose>Text value for the header.</purpose>
</documentation>
</argument>
<argument>
<name>encoding_charset</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character set used in the header value. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function SetHeader($header, $value, $encoding_charset="")
{
if(strlen($this->error))
return($this->error);
$this->headers[strval($header)]=(!strcmp($encoding_charset,"") ? strval($value) : $this->QuotedPrintableEncode($value, $encoding_charset, 1, 0));
return("");
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>SetEncodedHeader</name>
<type>STRING</type>
<documentation>
<purpose>The same as the <functionlink>SetHeader</functionlink>
function assuming the default character set specified by the
<variablelink>default_charset</variablelink> variable.</purpose>
<usage>See the <functionlink>SetHeader</functionlink> function.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>header</name>
<type>STRING</type>
<documentation>
<purpose>Name of the header.</purpose>
</documentation>
</argument>
<argument>
<name>value</name>
<type>STRING</type>
<documentation>
<purpose>Text value for the header.</purpose>
</documentation>
</argument>
<argument>
<name>encoding_charset</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character set used in the header value. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function SetEncodedHeader($header,$value, $encoding_charset = '')
{
return($this->SetHeader($header,$value,strlen($encoding_charset) ? $encoding_charset : $this->default_charset));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>SetEncodedEmailHeader</name>
<type>STRING</type>
<documentation>
<purpose>Set the value of an header that is meant to represent the
e-mail address of a person or entity with a known name. This is
meant mostly to set the <tt>From</tt>, <tt>To</tt>, <tt>Cc</tt> and
<tt>Bcc</tt> headers.</purpose>
<usage>Use this function like the
<functionlink>SetHeader</functionlink> specifying the e-mail
<argumentlink>
<function>SetEncodedEmailHeader</function>
<argument>address</argument>
</argumentlink> as header value and also specifying the
<argumentlink>
<function>SetEncodedEmailHeader</function>
<argument>name</argument>
</argumentlink> of the known person or entity.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>header</name>
<type>STRING</type>
<documentation>
<purpose>Name of the header.</purpose>
</documentation>
</argument>
<argument>
<name>address</name>
<type>STRING</type>
<documentation>
<purpose>E-mail address value.</purpose>
</documentation>
</argument>
<argument>
<name>name</name>
<type>STRING</type>
<documentation>
<purpose>Person or entity name associated with the specified e-mail
address.</purpose>
</documentation>
</argument>
<argument>
<name>encoding_charset</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character set used in the header value. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function SetEncodedEmailHeader($header, $address, $name, $encoding_charset = '')
{
return($this->SetHeader($header,$this->QuotedPrintableEncode($name, strlen($encoding_charset) ? $encoding_charset : $this->default_charset, 1, 1).' <'.$address.'>'));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>SetMultipleEncodedEmailHeader</name>
<type>STRING</type>
<documentation>
<purpose>Set the value of an header that is meant to represent a list
of e-mail addresses of names of people or entities. This is meant
mostly to set the <tt>To</tt>, <tt>Cc</tt> and <tt>Bcc</tt>
headers.</purpose>
<usage>Use this function specifying the <argumentlink>
<function>SetMultipleEncodedEmailHeader</function>
<argument>header</argument>
</argumentlink> and all the <argumentlink>
<function>SetMultipleEncodedEmailHeader</function>
<argument>addresses</argument>
</argumentlink> in an associative array that should have
the email addresses as entry indexes and the name of the respective
people or entities as entry values.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
<example><pre>$message_object->SetMultipleEncodedEmailHeader('Bcc', array(
'peter@gabriel.org' => 'Peter Gabriel',
'paul@simon.net' => 'Paul Simon',
'mary@chain.com' => 'Mary Chain'
));</pre></example>
</documentation>
<argument>
<name>header</name>
<type>STRING</type>
<documentation>
<purpose>Name of the header.</purpose>
</documentation>
</argument>
<argument>
<name>addresses</name>
<type>HASH</type>
<documentation>
<purpose>List of all email addresses and associated person or
entity names.</purpose>
</documentation>
</argument>
<argument>
<name>encoding_charset</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character set used in the header value. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function SetMultipleEncodedEmailHeader($header,$addresses, $encoding_charset = '')
{
Reset($addresses);
$end=(GetType($address=Key($addresses))!="string");
for($value="";!$end;)
{
if(strlen($value))
$value.=", ";
$value.=$this->QuotedPrintableEncode($addresses[$address], strlen($encoding_charset) ? $encoding_charset : $this->default_charset, 1, 1).' <'.$address.'>';
Next($addresses);
$end=(GetType($address=Key($addresses))!="string");
}
return($this->SetHeader($header,$value));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>ResetMessage</name>
<type>VOID</type>
<documentation>
<purpose>Restore the content of the message to the initial state when
the class object is created, i.e. without any headers or body
parts.</purpose>
<usage>Use this function if you want to start composing a completely
new message.</usage>
</documentation>
<do>
{/metadocument}
*/
Function ResetMessage()
{
$this->headers=array();
$this->body=-1;
$this->body_parts=0;
$this->parts=array();
$this->total_parts=0;
$this->free_parts=array();
$this->total_free_parts=0;
$this->delivery=array("State"=>"");
$this->error="";
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function CreatePart(&$definition,&$part)
{
$part=-1;
if(strlen($this->error))
return($this->error);
if($this->total_free_parts)
{
$this->total_free_parts--;
$part=$this->free_parts[$this->total_free_parts];
Unset($this->free_parts[$this->total_free_parts]);
}
else
{
$part=$this->total_parts;
++$this->total_parts;
}
$this->parts[$part]=$definition;
return("");
}
/*
{metadocument}
<function>
<name>AddPart</name>
<type>STRING</type>
<documentation>
<purpose>Add a previously created part to the message.</purpose>
<usage>Use any of the functions to create standalone message parts
and then use this function to add them to the message.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>part</name>
<type>INTEGER</type>
<documentation>
<purpose>Number of the part as returned by the function that
originally created it.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function AddPart($part)
{
if(strlen($this->error))
return($this->error);
switch($this->body_parts)
{
case 0;
$this->body=$part;
break;
case 1:
$parts=array(
$this->body,
$part
);
if(strlen($error=$this->CreateMixedMultipart($parts,$body)))
return($error);
$this->body=$body;
break;
default:
$this->parts[$this->body]["PARTS"][]=$part;
break;
}
++$this->body_parts;
return("");
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>ReplacePart</name>
<type>STRING</type>
<documentation>
<purpose>Replace a message part already added to the message with a
newly created part. The replaced part gets the definition of the
replacing part. The replacing part is discarded and its part number
becomes free for creation of a new part.</purpose>
<usage>Use one of the functions to create message parts and then pass
the returned part numbers to this function.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>old_part</name>
<type>INTEGER</type>
<documentation>
<purpose>Number of the previously added part.</purpose>
</documentation>
</argument>
<argument>
<name>new_part</name>
<type>INTEGER</type>
<documentation>
<purpose>Number of the replacing part.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function ReplacePart($old_part,$new_part)
{
if(strlen($this->error))
return($this->error);
if(!IsSet($this->parts[$old_part]))
return($this->error="it was attempted to replace an invalid message part");
if(IsSet($this->parts[$old_part]["FREE"]))
return($this->error="it was attempted to replace a message part that is no longer valid");
if(!IsSet($this->parts[$new_part]))
return($this->error="it was attempted to use an invalid message replacecement part");
if(IsSet($this->parts[$new_part]["FREE"]))
return($this->error="it was attempted to use a message replacecement part that is no longer valid");
$this->parts[$old_part]=$this->parts[$new_part];
$this->parts[$new_part]=array("FREE"=>1);
$this->free_parts[$this->total_free_parts]=$new_part;
++$this->total_free_parts;
return("");
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function CreateAndAddPart(&$definition)
{
if(strlen($error=$this->CreatePart($definition,$part))
|| strlen($error=$this->AddPart($part)))
return($error);
return("");
}
/*
{metadocument}
<function>
<name>CreatePlainTextPart</name>
<type>STRING</type>
<documentation>
<purpose>Create a plain text message part.</purpose>
<usage>Pass an ASCII (7 bits) <argumentlink>
<function>CreatePlainTextPart</function>
<argument>text</argument>
</argumentlink> string and get the created part number in the
<argumentlink>
<function>CreatePlainTextPart</function>
<argument>part</argument>
</argumentlink> that is returned by reference.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>text</name>
<type>STRING</type>
<documentation>
<purpose>Text of the message part to create.</purpose>
</documentation>
</argument>
<argument>
<name>charset</name>
<type>STRING</type>
<documentation>
<purpose>Character set used in the part text. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<argument>
<name>part</name>
<type>INTEGER</type>
<out />
<documentation>
<purpose>Number of the created part that is returned by reference.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function CreatePlainTextPart($text,$charset,&$part)
{
if(!strcmp($charset,""))
$charset=$this->default_charset;
$definition=array(
"Content-Type"=>"text/plain",
"DATA"=>$text
);
if(strcmp(strtoupper($charset),"ASCII"))
$definition["CHARSET"]=$charset;
return($this->CreatePart($definition,$part));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>AddPlainTextPart</name>
<type>STRING</type>
<documentation>
<purpose>Add a plain text part to the message.</purpose>
<usage>Pass an ASCII (7 bits) <argumentlink>
<function>AddPlainTextPart</function>
<argument>text</argument>
</argumentlink> string.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>text</name>
<type>STRING</type>
<documentation>
<purpose>Text of the message part to add.</purpose>
</documentation>
</argument>
<argument>
<name>charset</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character set used in the part text. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function AddPlainTextPart($text,$charset="")
{
if(strlen($error=$this->CreatePlainTextPart($text,$charset,$part))
|| strlen($error=$this->AddPart($part)))
return($error);
return("");
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function CreateEncodedQuotedPrintableTextPart($text,$charset,&$part)
{
if(!strcmp($charset,""))
$charset=$this->default_charset;
$definition=array(
"Content-Type"=>"text/plain",
"Content-Transfer-Encoding"=>"quoted-printable",
"CHARSET"=>$charset,
"DATA"=>$text
);
return($this->CreatePart($definition,$part));
}
Function AddEncodedQuotedPrintableTextPart($text,$charset="")
{
if(strlen($error=$this->CreateEncodedQuotedPrintableTextPart($text,$charset,$part))
|| strlen($error=$this->AddPart($part)))
return($error);
return("");
}
/*
{metadocument}
<function>
<name>CreateQuotedPrintableTextPart</name>
<type>STRING</type>
<documentation>
<purpose>Create a text message part that may contain non-ASCII
characters (8 bits or more).</purpose>
<usage>Pass a <argumentlink>
<function>CreateQuotedPrintableTextPart</function>
<argument>text</argument>
</argumentlink> string and get the created part number in the
<argumentlink>
<function>CreateQuotedPrintableTextPart</function>
<argument>part</argument>
</argumentlink> that is returned by reference.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>text</name>
<type>STRING</type>
<documentation>
<purpose>Text of the message part to create.</purpose>
</documentation>
</argument>
<argument>
<name>charset</name>
<type>STRING</type>
<documentation>
<purpose>Character set used in the part text. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<argument>
<name>part</name>
<type>INTEGER</type>
<out />
<documentation>
<purpose>Number of the created part that is returned by reference.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function CreateQuotedPrintableTextPart($text,$charset,&$part)
{
return($this->CreateEncodedQuotedPrintableTextPart($this->QuotedPrintableEncode($text),$charset,$part));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>AddQuotedPrintableTextPart</name>
<type>STRING</type>
<documentation>
<purpose>Add a text part to the message that may contain non-ASCII
characters (8 bits or more).</purpose>
<usage>Pass a <argumentlink>
<function>AddQuotedPrintableTextPart</function>
<argument>text</argument>
</argumentlink> string.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>text</name>
<type>STRING</type>
<documentation>
<purpose>Text of the message part to create.</purpose>
</documentation>
</argument>
<argument>
<name>charset</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character set used in the part text. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function AddQuotedPrintableTextPart($text,$charset="")
{
return($this->AddEncodedQuotedPrintableTextPart($this->QuotedPrintableEncode($text),$charset));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>CreateHTMLPart</name>
<type>STRING</type>
<documentation>
<purpose>Create an HTML message part only with ASCII characters (7 bit).</purpose>
<usage>Pass an ASCII (7 bits) <argumentlink>
<function>CreateHTMLPart</function>
<argument>html</argument>
</argumentlink> text string and get the created part number in the
<argumentlink>
<function>CreateHTMLPart</function>
<argument>part</argument>
</argumentlink> that is returned by reference.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>html</name>
<type>STRING</type>
<documentation>
<purpose>HTML of the message part to create.</purpose>
</documentation>
</argument>
<argument>
<name>charset</name>
<type>STRING</type>
<documentation>
<purpose>Character set used in the part text. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<argument>
<name>part</name>
<type>INTEGER</type>
<out />
<documentation>
<purpose>Number of the created part that is returned by reference.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function CreateHTMLPart($html,$charset,&$part)
{
if(!strcmp($charset,""))
$charset=$this->default_charset;
$definition=array(
"Content-Type"=>"text/html",
"CHARSET"=>$charset,
"DATA"=>$html
);
return($this->CreatePart($definition,$part));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>AddHTMLPart</name>
<type>STRING</type>
<documentation>
<purpose>Add an HTML part to the message only with ASCII characters.</purpose>
<usage>Pass an <argumentlink>
<function>AddHTMLPart</function>
<argument>html</argument>
</argumentlink> text string.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>html</name>
<type>STRING</type>
<documentation>
<purpose>HTML of the message part to create.</purpose>
</documentation>
</argument>
<argument>
<name>charset</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character set used in the part text. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function AddHTMLPart($html,$charset="")
{
if(strlen($error=$this->CreateHTMLPart($html,$charset,$part))
|| strlen($error=$this->AddPart($part)))
return($error);
return("");
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function CreateEncodedQuotedPrintableHTMLPart($html,$charset,&$part)
{
if(!strcmp($charset,""))
$charset=$this->default_charset;
$definition=array(
"Content-Type"=>"text/html",
"Content-Transfer-Encoding"=>"quoted-printable",
"CHARSET"=>$charset,
"DATA"=>$html
);
return($this->CreatePart($definition,$part));
}
Function AddEncodedQuotedPrintableHTMLPart($html,$charset="")
{
if(strlen($error=$this->CreateEncodedQuotedPrintableHTMLPart($html,$charset,$part))
|| strlen($error=$this->AddPart($part)))
return($error);
return("");
}
/*
{metadocument}
<function>
<name>CreateQuotedPrintableHTMLPart</name>
<type>STRING</type>
<documentation>
<purpose>Create an HTML message part that may contain non-ASCII
characters (8 bits or more).</purpose>
<usage>Pass a <argumentlink>
<function>CreateQuotedPrintableHTMLPart</function>
<argument>html</argument>
</argumentlink> text string and get the created part number in the
<argumentlink>
<function>CreateQuotedPrintableHTMLPart</function>
<argument>part</argument>
</argumentlink> that is returned by reference.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>html</name>
<type>STRING</type>
<documentation>
<purpose>HTML of the message part to create.</purpose>
</documentation>
</argument>
<argument>
<name>charset</name>
<type>STRING</type>
<documentation>
<purpose>Character set used in the part text. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<argument>
<name>part</name>
<type>INTEGER</type>
<out />
<documentation>
<purpose>Number of the created part that is returned by reference.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function CreateQuotedPrintableHTMLPart($html,$charset,&$part)
{
return($this->CreateEncodedQuotedPrintableHTMLPart($this->QuotedPrintableEncode($html),$charset,$part));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>AddQuotedPrintableHTMLPart</name>
<type>STRING</type>
<documentation>
<purpose>Add an HTML part to the message that may contain non-ASCII
characters (8 bits or more).</purpose>
<usage>Pass a <argumentlink>
<function>AddQuotedPrintableHTMLPart</function>
<argument>html</argument>
</argumentlink> text string.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>html</name>
<type>STRING</type>
<documentation>
<purpose>HTML of the message part to create.</purpose>
</documentation>
</argument>
<argument>
<name>charset</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Character set used in the part text. If it is set to an
empty string, it is assumed the character set defined by the
<variablelink>default_charset</variablelink> variable.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function AddQuotedPrintableHTMLPart($html,$charset="")
{
return($this->AddEncodedQuotedPrintableHTMLPart($this->QuotedPrintableEncode($html),$charset));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function GetFileDefinition($file, &$definition, $require_name=1)
{
if(strlen($this->error))
return($this->error);
$name="";
if(IsSet($file["FileName"]))
$name=basename($file["FileName"]);
else
{
if(!IsSet($file["Data"]))
return($this->OutputError("it was not specified the file part file name"));
}
if(IsSet($file["Name"]))
$name=$file["Name"];
if($require_name
&& strlen($name)==0)
return($this->OutputError("it was not specified the file part name"));
$encoding="base64";
if(IsSet($file["Content-Type"]))
{
$content_type=$file["Content-Type"];
$type=$this->Tokenize(strtolower($content_type),"/");
$sub_type=$this->Tokenize("");
switch($type)
{
case "text":
case "image":
case "audio":
case "video":
case "application":
break;
case "message":
$encoding="7bit";
break;
case "automatic":
switch($sub_type)
{
case "name":
if(strlen($name)==0)
return($this->OutputError("it is not possible to determine content type from the name"));
switch(strtolower($this->GetFilenameExtension($name)))
{
case ".xls":
$content_type="application/excel";
break;
case ".hqx":
$content_type="application/macbinhex40";
break;
case ".doc":
case ".dot":
case ".wrd":
$content_type="application/msword";
break;
case ".pdf":
$content_type="application/pdf";
break;
case ".pgp":
$content_type="application/pgp";
break;
case ".ps":
case ".eps":
case ".ai":
$content_type="application/postscript";
break;
case ".ppt":
$content_type="application/powerpoint";
break;
case ".rtf":
$content_type="application/rtf";
break;
case ".tgz":
case ".gtar":
$content_type="application/x-gtar";
break;
case ".gz":
$content_type="application/x-gzip";
break;
case ".php":
case ".php3":
$content_type="application/x-httpd-php";
break;
case ".js":
$content_type="application/x-javascript";
break;
case ".ppd":
case ".psd":
$content_type="application/x-photoshop";
break;
case ".swf":
case ".swc":
case ".rf":
$content_type="application/x-shockwave-flash";
break;
case ".tar":
$content_type="application/x-tar";
break;
case ".zip":
$content_type="application/zip";
break;
case ".mid":
case ".midi":
case ".kar":
$content_type="audio/midi";
break;
case ".mp2":
case ".mp3":
case ".mpga":
$content_type="audio/mpeg";
break;
case ".ra":
$content_type="audio/x-realaudio";
break;
case ".wav":
$content_type="audio/wav";
break;
case ".bmp":
$content_type="image/bitmap";
break;
case ".gif":
$content_type="image/gif";
break;
case ".iff":
$content_type="image/iff";
break;
case ".jb2":
$content_type="image/jb2";
break;
case ".jpg":
case ".jpe":
case ".jpeg":
$content_type="image/jpeg";
break;
case ".jpx":
$content_type="image/jpx";
break;
case ".png":
$content_type="image/png";
break;
case ".tif":
case ".tiff":
$content_type="image/tiff";
break;
case ".wbmp":
$content_type="image/vnd.wap.wbmp";
break;
case ".xbm":
$content_type="image/xbm";
break;
case ".css":
$content_type="text/css";
break;
case ".txt":
$content_type="text/plain";
break;
case ".htm":
case ".html":
$content_type="text/html";
break;
case ".xml":
$content_type="text/xml";
break;
case ".mpg":
case ".mpe":
case ".mpeg":
$content_type="video/mpeg";
break;
case ".qt":
case ".mov":
$content_type="video/quicktime";
break;
case ".avi":
$content_type="video/x-ms-video";
break;
case ".eml":
$content_type="message/rfc822";
$encoding="7bit";
break;
default:
$content_type="application/octet-stream";
break;
}
break;
default:
return($this->OutputError($content_type." is not a supported automatic content type detection method"));
}
break;
default:
return($this->OutputError($content_type." is not a supported file content type"));
}
}
else
$content_type="application/octet-stream";
$definition=array(
"Content-Type"=>$content_type,
"Content-Transfer-Encoding"=>$encoding,
"NAME"=>$name
);
if(IsSet($file["Disposition"]))
{
switch(strtolower($file["Disposition"]))
{
case "inline":
case "attachment":
break;
default:
return($this->OutputError($file["Disposition"]." is not a supported message part content disposition"));
}
$definition["DISPOSITION"]=$file["Disposition"];
}
if(IsSet($file["FileName"]))
$definition["FILENAME"]=$file["FileName"];
else
{
if(IsSet($file["Data"]))
$definition["DATA"]=$file["Data"];
}
if(IsSet($file['Cache'])
&& $file['Cache'])
$definition['Cache'] = 1;
return("");
}
/*
{metadocument}
<function>
<name>CreateFilePart</name>
<type>STRING</type>
<documentation>
<purpose>Create a message part to be handled as a file.</purpose>
<usage>Pass a <argumentlink>
<function>CreateFilePart</function>
<argument>file</argument>
</argumentlink> definition associative array and get the created
part number in the <argumentlink>
<function>CreateFilePart</function>
<argument>part</argument>
</argumentlink> that is returned by reference.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>file</name>
<type>HASH</type>
<documentation>
<purpose>Associative array to specify parameters that describe the
file part. Here follows the list of supported parameters that
should be used as indexes of the array:<paragraphbreak />
<tt>FileName</tt><paragraphbreak />
Name of the file from which the part data will be read when the
message is generated. It may be a remote URL as long as your PHP
installation is configured to allow accessing remote files with
the <tt>fopen()</tt> function.<paragraphbreak />
<tt>Data</tt><paragraphbreak />
String that specifies the data of the file. This should be used
as alternative data source to <tt>FileName</tt> for passing data
available in memory, like for instance files stored in a database
that was queried dynamically and the file contents was fetched
into a string variable.<paragraphbreak />
<tt>Name</tt><paragraphbreak />
Name of the file that will appear in the message. If this
parameter is missing the base name of the <tt>FileName</tt>
parameter is used, if present.<paragraphbreak />
<tt>Content-Type</tt><paragraphbreak />
Content type of the part: <tt>text/plain</tt> for text,
<tt>text/html</tt> for HTML, <tt>image/gif</tt> for GIF images,
etc..<paragraphbreak />
There is one special type named <tt>automatic/name</tt> that may
be used to tell the class to try to guess the content type from
the file name. Many file types are recognized from the file name
extension. If the file name extension is not recognized, the
default for binary data <tt>application/octet-stream</tt> is
assumed.<paragraphbreak />
<tt>Disposition</tt><paragraphbreak />
Information to whether this file part is meant to be used as a
file <tt>attachment</tt> or as a part meant to be displayed
<tt>inline</tt>, eventually integrated with another related
part.<paragraphbreak />
<tt>Cache</tt><paragraphbreak />
Boolean flag that indicates that this message part should be
cached when generating the message body. Use only when sending
many messages to multiple recipients, but this part does not
change between each of the messages that are sent.<paragraphbreak />
Note that it is also not worth using this option when setting the
<variablelink>cache_body</variablelink>, as that variable makes
the class cache the whole message body and the internal message
parts will not be rebuilt.</purpose>
</documentation>
</argument>
<argument>
<name>part</name>
<type>INTEGER</type>
<out />
<documentation>
<purpose>Number of the created part that is returned by reference.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function CreateFilePart(&$file,&$part)
{
if(strlen($this->GetFileDefinition($file,$definition)))
return($this->error);
return($this->CreatePart($definition,$part));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>AddFilePart</name>
<type>STRING</type>
<documentation>
<purpose>Add a message part to be handled as a file.</purpose>
<usage>Pass a <argumentlink>
<function>AddFilePart</function>
<argument>file</argument>
</argumentlink> definition associative array.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>file</name>
<type>HASH</type>
<documentation>
<purpose>Associative array to specify parameters that describe the
file part. See the <argumentlink>
<function>CreateFilePart</function>
<argument>file</argument>
</argumentlink> argument description of the
<functionlink>CreateFilePart</functionlink> function for an
explanation about the supported file parameters.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function AddFilePart(&$file)
{
if(strlen($error=$this->CreateFilePart($file,$part))
|| strlen($error=$this->AddPart($part)))
return($error);
return("");
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>CreateMessagePart</name>
<type>STRING</type>
<documentation>
<purpose>Create a message part to encapsulate another message. This
is usually meant to create an attachment that contains a message
that was received and is being forwarded intact with the original
the headers and body data.</purpose>
<usage>This function should be used like the
<functionlink>CreateFilePart</functionlink> function, passing the
same parameters to the <argumentlink>
<function>CreateMessagePart</function>
<argument>message</argument>
</argumentlink> argument.<paragraphbreak />
The message to be encapsulated can be specified either as an
existing file with the <tt>FileName</tt> parameter, or as string
of data in memory with the <tt>Data</tt>
parameter.<paragraphbreak />
The <tt>Content-Type</tt> and <tt>Disposition</tt> file parameters
do not need to be specified because they are overridden by this
function.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>message</name>
<type>HASH</type>
<documentation>
<purpose>Associative array that specifies definition parameters of
the message file part.</purpose>
</documentation>
</argument>
<argument>
<name>part</name>
<type>INTEGER</type>
<out />
<documentation>
<purpose>Number of the created part that is returned by reference.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function CreateMessagePart(&$message,&$part)
{
$message["Content-Type"]="message/rfc822";
$message["Disposition"]="inline";
if(strlen($this->GetFileDefinition($message,$definition)))
return($this->error);
return($this->CreatePart($definition,$part));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>AddMessagePart</name>
<type>STRING</type>
<documentation>
<purpose>Add a message part that encapsulates another message. This
is usually meant to add an attachment that contains a message that
was received and is being forwarded intact with the original the
headers and body data.</purpose>
<usage>This function should be used like the
<functionlink>AddFilePart</functionlink> function, passing the
same parameters to the <argumentlink>
<function>AddMessagePart</function>
<argument>message</argument>
</argumentlink> argument. See the
<functionlink>CreateFilePart</functionlink> function for more
details.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>message</name>
<type>HASH</type>
<documentation>
<purpose>Associative array that specifies definition parameters of
the message file part.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function AddMessagePart(&$message)
{
if(strlen($error=$this->CreateMessagePart($message,$part))
|| strlen($error=$this->AddPart($part)))
return($error);
return("");
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function CreateMultipart(&$parts,&$part,$type)
{
$definition=array(
"Content-Type"=>"multipart/".$type,
"PARTS"=>$parts
);
return($this->CreatePart($definition,$part));
}
Function AddMultipart(&$parts,$type)
{
if(strlen($error=$this->CreateMultipart($parts,$part,$type))
|| strlen($error=$this->AddPart($part)))
return($error);
return("");
}
/*
{metadocument}
<function>
<name>CreateAlternativeMultipart</name>
<type>STRING</type>
<documentation>
<purpose>Create a message part composed of multiple parts that can be
displayed by the recipient e-mail program in alternative
formats.<paragraphbreak />
This is usually meant to create HTML messages with an alternative
text part to be displayed by programs that cannot display HTML
messages.</purpose>
<usage>Create all the alternative message parts that are going to be
sent and pass their numbers to the <argumentlink>
<function>CreateAlternativeMultipart</function>
<argument>parts</argument>
</argumentlink> array argument.<paragraphbreak />
The least sophisticated part, usually the text part, should appear
first in the parts array because the e-mail programs that support
displaying more sophisticated message parts will pick the last part
in the message that is supported.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>parts</name>
<type>ARRAY</type>
<documentation>
<purpose>Array with the numbers with all the alternative parts.</purpose>
</documentation>
</argument>
<argument>
<name>part</name>
<type>INTEGER</type>
<out />
<documentation>
<purpose>Number of the created part that is returned by reference.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function CreateAlternativeMultipart(&$parts,&$part)
{
return($this->CreateMultiPart($parts,$part,"alternative"));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>AddAlternativeMultipart</name>
<type>STRING</type>
<documentation>
<purpose>Add a message part composed of multiple parts that can be
displayed by the recipient e-mail program in alternative
formats.<paragraphbreak />
This is usually meant to create HTML messages with an alternative
text part to be displayed by programs that cannot display HTML
messages.</purpose>
<usage>Create all the alternative message parts that are going to be
sent and pass their numbers to the <argumentlink>
<function>AddAlternativeMultipart</function>
<argument>parts</argument>
</argumentlink> array argument.<paragraphbreak />
The least sophisticated part, usually the text part, should appear
first in the parts array because the e-mail programs that support
displaying more sophisticated message parts will pick the last part
in the message that is supported.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>parts</name>
<type>ARRAY</type>
<documentation>
<purpose>Array with the numbers with all the alternative parts.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function AddAlternativeMultipart(&$parts)
{
return($this->AddMultipart($parts,"alternative"));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>CreateRelatedMultipart</name>
<type>STRING</type>
<documentation>
<purpose>Create a message part that groups several related
parts.<paragraphbreak />
This is usually meant to group an HTML message part with images or
other types of files that should be embedded in the same message
and be displayed as a single part by the recipient e-mail
program.</purpose>
<usage>Create all the related message parts that are going to be
sent and pass their numbers to the <argumentlink>
<function>CreateRelatedMultipart</function>
<argument>parts</argument>
</argumentlink> array argument.<paragraphbreak />
When using this function to group an HTML message with embedded
images or other related files, make sure that the HTML part number
is the first listed in the <argumentlink>
<function>CreateRelatedMultipart</function>
<argument>parts</argument>
</argumentlink> array argument, or else the message may not appear
correctly.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>parts</name>
<type>ARRAY</type>
<documentation>
<purpose>Array with the numbers with all the related parts.</purpose>
</documentation>
</argument>
<argument>
<name>part</name>
<type>INTEGER</type>
<out />
<documentation>
<purpose>Number of the created part that is returned by reference.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function CreateRelatedMultipart(&$parts,&$part)
{
return($this->CreateMultipart($parts,$part,"related"));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>AddRelatedMultipart</name>
<type>STRING</type>
<documentation>
<purpose>Add a message part that groups several related
parts.<paragraphbreak />
This is usually meant to group an HTML message part with images or
other types of files that should be embedded in the same message
and be displayed as a single part by the recipient e-mail
program.</purpose>
<usage>Create all the related message parts that are going to be
sent and pass their numbers to the <argumentlink>
<function>AddRelatedMultipart</function>
<argument>parts</argument>
</argumentlink> array argument.<paragraphbreak />
When using this function to group an HTML message with embedded
images or other related files, make sure that the HTML part number
is the first listed in the <argumentlink>
<function>AddRelatedMultipart</function>
<argument>parts</argument>
</argumentlink> array argument, or else the message may not appear
correctly.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>parts</name>
<type>ARRAY</type>
<documentation>
<purpose>Array with the numbers with all the related parts.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function AddRelatedMultipart(&$parts)
{
return($this->AddMultipart($parts,"related"));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>CreateMixedMultipart</name>
<type>STRING</type>
<documentation>
<purpose>Create a message part that groups several independent
parts.<paragraphbreak />
Usually this is meant compose messages with one or more file
attachments. However, it is not necessary to use this function as
the class implicitly creates a <tt>multipart/mixed</tt> message
when more than one part is added to the message.</purpose>
<usage>Create all the independent message parts that are going to be
sent and pass their numbers to the <argumentlink>
<function>CreateMixedMultipart</function>
<argument>parts</argument>
</argumentlink> array argument.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>parts</name>
<type>ARRAY</type>
<documentation>
<purpose>Array with the numbers with all the related parts.</purpose>
</documentation>
</argument>
<argument>
<name>part</name>
<type>INTEGER</type>
<out />
<documentation>
<purpose>Number of the created part that is returned by reference.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function CreateMixedMultipart(&$parts,&$part)
{
return($this->CreateMultipart($parts,$part,"mixed"));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>AddMixedMultipart</name>
<type>STRING</type>
<documentation>
<purpose>Add a message part that groups several independent
parts.<paragraphbreak />
Usually this is meant compose messages with one or more file
attachments. However, it is not necessary to use this function as
the class implicitly creates a <tt>multipart/mixed</tt> message
when more than one part is added to the message.</purpose>
<usage>Create all the independent message parts that are going to be
sent and pass their numbers to the <argumentlink>
<function>AddMixedMultipart</function>
<argument>parts</argument>
</argumentlink> array argument.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>parts</name>
<type>ARRAY</type>
<documentation>
<purpose>Array with the numbers with all the related parts.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function AddMixedMultipart(&$parts)
{
return($this->AddMultipart($parts,"mixed"));
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function CreateParallelMultipart(&$parts,&$part)
{
return($this->CreateMultipart($parts,$part,"paralell"));
}
Function AddParalellMultipart(&$parts)
{
return($this->AddMultipart($parts,"paralell"));
}
/*
{metadocument}
<function>
<name>GetPartContentID</name>
<type>STRING</type>
<documentation>
<purpose>Retrieve the content identifier associated to a given
message part.</purpose>
<usage>Create a message part and pass its number to the <argumentlink>
<function>GetPartContentID</function>
<argument>part</argument>
</argumentlink> argument.<paragraphbreak />
This function is usually meant to create an URL that can be used
in an HTML message part to reference related parts like images, CSS
(Cascaded Style Sheets), or any other type of files related to the
HTML part that are embedded in the same message as part of a
<tt>multipart/related</tt> composite part.<paragraphbreak />
To use the part content identifier returned by this function you
need to prepend the string <tt><stringvalue>cid:</stringvalue></tt>
to form a special URL that can be used in the HTML document this
part file.<paragraphbreak />
You may read more about using this function in the class usage
section about <link>
<data>embedding images in HTML messages</data>
<name>embed-image</name>
</link>.</usage>
<returnvalue>The content identifier text string.<paragraphbreak />
If it is specified an invalid message part, this function returns
an empty string.</returnvalue>
</documentation>
<argument>
<name>part</name>
<type>INTEGER</type>
<documentation>
<purpose>Number of the part as returned by the function that
originally created it.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function GetPartContentID($part)
{
if(!IsSet($this->parts[$part]))
return("");
if(!IsSet($this->parts[$part]["Content-ID"]))
{
$extension=(IsSet($this->parts[$part]["NAME"]) ? $this->GetFilenameExtension($this->parts[$part]["NAME"]) : "");
$this->parts[$part]["Content-ID"]=md5(uniqid($part.time())).$extension;
}
return($this->parts[$part]["Content-ID"]);
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>GetDataURL</name>
<type>STRING</type>
<documentation>
<purpose>Generate a <tt>data:</tt> URL according to the <link>
<data>RFC 2397</data>
<url>http://www.ietf.org/rfc/rfc2397.txt</url>
</link> suitable for using in HTML messages to represent an image
or other type of file on which the data is directly embedded in the
HTML code instead of being fetched from a separate file or remote
URL.<paragraphbreak />
Note that not all e-mail programs are capable of displaying images
or other types of files embedded in HTML messages this way.</purpose>
<usage>Pass a <argumentlink>
<function>GetDataURL</function>
<argument>file</argument>
</argumentlink> part definition array like for the
<functionlink>CreateFilePart</functionlink> function.</usage>
<returnvalue>The <tt>data:</tt> representing the described file or an
empty string in case there was an error.</returnvalue>
</documentation>
<argument>
<name>file</name>
<type>HASH</type>
<documentation>
<purpose>File definition.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function GetDataURL($file)
{
$ini_magic_quotes_runtime_chk = false;
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$ini_magic_quotes_runtime_chk = ini_get('magic_quotes_runtime');
}
if(strlen($this->GetFileDefinition($file,$definition,0)))
return($this->error);
if(IsSet($definition["FILENAME"]))
{
$size=@filesize($definition["FILENAME"]);
if(!($file=@fopen($definition["FILENAME"],"rb")))
return($this->OutputPHPError("could not open data file ".$definition["FILENAME"], error_get_last()['message']));
for($body="";!feof($file);)
{
if(GetType($block=@fread($file,$this->file_buffer_length))!="string")
{
$this->OutputPHPError("could not read data file", error_get_last()['message']);
fclose($file);
return("");
}
$body.=$block;
}
fclose($file);
if(GetType($size)=="integer"
&& strlen($body)!=$size)
{
$this->OutputError("the length of the file that was read does not match the size of the part file ".$definition["FILENAME"]." due to possible data corruption");
return("");
}
if(function_exists("ini_get")
&& $ini_magic_quotes_runtime_chk)
$body=StripSlashes($body);
$body=chunk_split(base64_encode($body), $this->line_length, $this->line_break);
}
else
{
if(!IsSet($definition["DATA"]))
{
$this->OutputError("it was not specified a file or data block");
return("");
}
$body=chunk_split(base64_encode($definition["DATA"]), $this->line_length, $this->line_break);
}
return("data:".$definition["Content-Type"].";base64,".$body);
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function GetHeadersAndBody(&$headers, &$body)
{
$headers=$this->headers;
if(strcmp($this->mailer,""))
{
$headers["X-Mailer"]=$this->mailer;
if(strlen($this->mailer_delivery))
$headers["X-Mailer"].=' ('.$this->mailer_delivery.')';
}
$headers["MIME-Version"]="1.0";
if($this->body_parts==0)
return($this->OutputError("message has no body parts"));
if(strlen($error=$this->GetPartHeaders($headers,$this->body)))
return($error);
if($this->cache_body
&& IsSet($this->body_cache[$this->body]))
$body=$this->body_cache[$this->body];
else
{
if(strlen($error=$this->GetPartBody($body,$this->body)))
return($error);
if($this->cache_body)
$this->body_cache[$this->body]=$body;
}
return("");
}
/*
{metadocument}
<function>
<name>Send</name>
<type>STRING</type>
<documentation>
<purpose>Send a composed message.</purpose>
<usage>Use this function after you have set the necessary message
headers and added the message body parts.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<do>
{/metadocument}
*/
Function Send()
{
if(strlen($this->error))
return($this->error);
if(strlen($error=$this->GetHeadersAndBody($headers, $body)))
return($error);
if(strcmp($error=$this->StartSendingMessage(),""))
return($error);
if(strlen($error=$this->SendMessageHeaders($headers))==0
&& strlen($error=$this->SendMessageBody($body))==0)
$error=$this->EndSendingMessage();
$this->StopSendingMessage();
return($error);
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>GetMessage</name>
<type>STRING</type>
<documentation>
<purpose>Get the whole message headers and body.</purpose>
<usage>Use this function to retrieve the message headers and body
without sending it.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>message</name>
<type>STRING</type>
<out />
<documentation>
<purpose>Reference to a string variable to store the text of the
message headers and body.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function GetMessage(&$message)
{
if(strlen($this->error))
return($this->error);
if(strlen($error=$this->GetHeadersAndBody($headers, $body)))
return($error);
for($message="", $h=0, Reset($headers); $h<count($headers); ++$h, Next($headers))
{
$name=Key($headers);
$message.=$name.": ".$headers[$name].$this->line_break;
}
$message.=$this->line_break;
$message.=$body;
return("");
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>GetMessageSize</name>
<type>STRING</type>
<documentation>
<purpose>Get the size of the whole message headers and body.</purpose>
<usage>Use this function to retrieve the size in bytes of the
message headers and body without sending it.</usage>
<returnvalue>An error message in case there was an error or an empty
string otherwise. This return value may be safely ignored if the
function parameters are set correctly.</returnvalue>
</documentation>
<argument>
<name>message</name>
<type>STRING</type>
<out />
<documentation>
<purpose>Reference to an integer variable to store the size of the
message headers and body.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function GetMessageSize(&$size)
{
if(strlen($error=$this->GetMessage($message)))
return($error);
$size=strlen($message);
return("");
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
/*
{metadocument}
<function>
<name>Mail</name>
<type>BOOLEAN</type>
<documentation>
<purpose>Emulate the PHP <tt>mail()</tt> function by composing and
sending a message given the same arguments.<paragraphbreak />
This is mostly meant to provide a solution for sending messages
with alternative delivery methods provided by this class
sub-classes. It uses the same arguments as the PHP <tt>mail()</tt>
function. Developers willing to use this alternative do not need to
change much their scripts that already use the <tt>mail()</tt>
function.</purpose>
<usage>Use this function passing the same arguments as to PHP
<tt><link>
<data>mail()</data>
<url>http://www.php.net/manual/en/function.mail.php</url>
</link></tt> function.</usage>
<returnvalue>If this function succeeds, it returns
<tt><booleanvalue>1</booleanvalue></tt>.</returnvalue>
</documentation>
<argument>
<name>to</name>
<type>STRING</type>
<documentation>
<purpose>Recipient e-mail address.</purpose>
</documentation>
</argument>
<argument>
<name>subject</name>
<type>STRING</type>
<documentation>
<purpose>Message subject.</purpose>
</documentation>
</argument>
<argument>
<name>message</name>
<type>STRING</type>
<documentation>
<purpose>Message body.</purpose>
</documentation>
</argument>
<argument>
<name>additional_headers</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Text string headers and the respective values. There
should be one header and value per line with line breaks
separating each line.</purpose>
</documentation>
</argument>
<argument>
<name>additional_parameters</name>
<type>STRING</type>
<defaultvalue></defaultvalue>
<documentation>
<purpose>Text string with additional parameters. In the original
PHP <tt>mail()</tt> function these were actual switches to be
passed in the sendmail program invocation command line. This
function only supports the <tt>-f</tt> switch followed by an
e-mail address meant to specify the message bounce return path
address.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function Mail($to, $subject, $message, $additional_headers="", $additional_parameters="")
{
$this->ResetMessage();
$this->headers=array("To"=>$to,"Subject"=>$subject);
$content_type="";
while(strlen($additional_headers))
{
preg_match("/([^\r\n]+)(\r?\n)?(.*)\$/",$additional_headers,$matches);
$header=$matches[1];
$additional_headers=$matches[3];
if(!preg_match("/^([^:]+):[ \t]+(.+)\$/",$header,$matches))
{
$this->error="invalid header \"$header\"";
return(0);
}
if(strtolower($matches[1])=="content-type")
{
if(strlen($content_type))
{
$this->error="the content-type header was specified more than once.";
return(0);
}
$content_type=$matches[2];
}
else
$this->SetHeader($matches[1],$matches[2]);
}
if(strlen($additional_parameters))
{
if(preg_match("/^[ \t]*-f[ \t]*([^@]+@[^ \t]+)[ \t]*(.*)\$/", $additional_parameters, $matches))
{
if(!preg_match('/'.str_replace('/', '\\/', $this->email_regular_expression).'/i', $matches[1]))
{
$this->error="it was specified an invalid e-mail address for the additional parameter -f";
return(0);
}
if(strlen($matches[2]))
{
$this->error="it were specified some additional parameters after -f e-mail address parameter that are not supported";
return(0);
}
$this->SetHeader("Return-Path",$matches[1]);
}
else
{
$this->error="the additional parameters that were specified are not supported";
return(0);
}
}
if(strlen($content_type)==0)
$content_type="text/plain";
$definition=array(
"Content-Type"=>$content_type,
"DATA"=>$message
);
$this->CreateAndAddPart($definition);
$this->Send();
return(strlen($this->error)==0);
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function ChangeBulkMail($on)
{
return(1);
}
/*
{metadocument}
<function>
<name>SetBulkMail</name>
<type>BOOLEAN</type>
<documentation>
<purpose>Hint the class to adjust itself in order to send individual
messages to many recipients more efficiently.</purpose>
<usage>Call this function before starting sending messages to many
recipients passing <booleanvalue>1</booleanvalue> to the
<argumentlink>
<function>SetBulkMail</function>
<argument>on</argument>
</argumentlink> argument. Then call this function again after the
bulk mailing delivery has ended passing passing
<booleanvalue>1</booleanvalue> to the <argumentlink>
<function>SetBulkMail</function>
<argument>on</argument>
</argumentlink> argument.</usage>
<returnvalue>If this function succeeds, it returns
<tt><booleanvalue>1</booleanvalue></tt>.</returnvalue>
</documentation>
<argument>
<name>on</name>
<type>BOOLEAN</type>
<documentation>
<purpose>Boolean flag that indicates whether a bulk delivery is
going to start if set to <booleanvalue>1</booleanvalue> or that
the bulk delivery has ended if set to
<booleanvalue>0</booleanvalue>.</purpose>
</documentation>
</argument>
<do>
{/metadocument}
*/
Function SetBulkMail($on)
{
if(strlen($this->error))
return(0);
if(!$this->bulk_mail==!$on)
return(1);
if(!$this->ChangeBulkMail($on))
return(0);
$this->bulk_mail=!!$on;
return(1);
}
/*
{metadocument}
</do>
</function>
{/metadocument}
*/
Function OpenMailing(&$mailing,&$mailing_properties)
{
if(strlen($this->error))
return($this->error);
if(!IsSet($mailing_properties["Name"])
|| strlen($mailing_properties["Name"])==0)
return($this->OutputError("it was not specified a valid mailing Name"));
if(!IsSet($mailing_properties["Return-Path"])
|| strlen($mailing_properties["Return-Path"])==0)
return($this->OutputError("it was not specified a valid mailing Return-Path"));
$separator="";
$directory_separator=(defined("DIRECTORY_SEPARATOR") ? DIRECTORY_SEPARATOR : ((defined("PHP_OS") && !strcmp(substr(PHP_OS,0,3),"WIN")) ? "\\" : "/"));
$length=strlen($this->mailing_path);
if($length)
{
if($this->mailing_path[$length-1]!=$directory_separator)
$separator=$directory_separator;
}
$base_path=$this->mailing_path.$separator.$mailing_properties["Name"];
if($this->body_parts==0)
return($this->OutputError("message has no body parts"));
$line_break="\n";
$headers=$this->headers;
if(strlen($this->mailer))
$headers["X-Mailer"]=$this->mailer;
$headers["MIME-Version"]="1.0";
if(strlen($error=$this->GetPartHeaders($headers,$this->body)))
return($error);
if(!($header_file=@fopen($base_path.".h","wb")))
return($this->OutputPHPError("could not open mailing headers file ".$base_path.".h", error_get_last()['message']));
for($header=0,Reset($headers);$header<count($headers);Next($headers),++$header)
{
$header_name=Key($headers);
if(!@fwrite($header_file,$header_name.": ".$headers[$header_name].$line_break))
{
fclose($header_file);
return($this->OutputPHPError("could not write to the mailing headers file ".$base_path.".h", error_get_last()['message']));
}
}
if(!@fflush($header_file))
{
fclose($header_file);
@unlink($base_path.".h");
return($this->OutputPHPError("could not write to the mailing headers file ".$base_path.".h", error_get_last()['message']));
}
fclose($header_file);
if(strlen($error=$this->GetPartBody($body,$this->body)))
{
@unlink($base_path.".h");
return($error);
}
if(!($body_file=@fopen($base_path.".b","wb")))
{
@unlink($base_path.".h");
return($this->OutputPHPError("could not open mailing body file ".$base_path.".b", error_get_last()['message']));
}
if(!@fwrite($body_file,$body)
|| !@fflush($body_file))
{
fclose($body_file);
@unlink($base_path.".b");
@unlink($base_path.".h");
return($this->OutputPHPError("could not write to the mailing body file ".$base_path.".b", error_get_last()['message']));
}
fclose($body_file);
if(!($envelope=@fopen($base_path.".e","wb")))
{
@unlink($base_path.".b");
@unlink($base_path.".h");
return($this->OutputPHPError("could not open mailing envelope file ".$base_path.".e", error_get_last()['message']));
}
if(!@fwrite($envelope,"F".$mailing_properties["Return-Path"].chr(0))
|| !@fflush($envelope))
{
@fclose($envelope);
@unlink($base_path.".e");
@unlink($base_path.".b");
@unlink($base_path.".h");
return($this->OutputPHPError("could not write to the return path to the mailing envelope file ".$base_path.".e", error_get_last()['message']));
}
$mailing=++$this->last_mailing;
$this->mailings[$mailing]=array(
"Envelope"=>$envelope,
"BasePath"=>$base_path
);
return("");
}
Function AddMailingRecipient($mailing,&$recipient_properties)
{
if(strlen($this->error))
return($this->error);
if(!IsSet($this->mailings[$mailing]))
return($this->OutputError("it was not specified a valid mailing"));
if(!IsSet($recipient_properties["Address"])
|| strlen($recipient_properties["Address"])==0)
return($this->OutputError("it was not specified a valid mailing recipient Address"));
if(!@fwrite($this->mailings[$mailing]["Envelope"],"T".$recipient_properties["Address"].chr(0)))
return($this->OutputPHPError("could not write recipient address to the mailing envelope file", error_get_last()['message']));
return("");
}
Function EndMailing($mailing)
{
if(strlen($this->error))
return($this->error);
if(!IsSet($this->mailings[$mailing]))
return($this->OutputError("it was not specified a valid mailing"));
if(!IsSet($this->mailings[$mailing]["Envelope"]))
return($this->OutputError("the mailing was already ended"));
if(!@fwrite($this->mailings[$mailing]["Envelope"],chr(0))
|| !@fflush($this->mailings[$mailing]["Envelope"]))
return($this->OutputPHPError("could not end writing to the mailing envelope file", error_get_last()['message']));
fclose($this->mailings[$mailing]["Envelope"]);
Unset($this->mailings[$mailing]["Envelope"]);
return("");
}
Function SendMailing($mailing)
{
if(strlen($this->error))
return($this->error);
if(!IsSet($this->mailings[$mailing]))
return($this->OutputError("it was not specified a valid mailing"));
if(IsSet($this->mailings[$mailing]["Envelope"]))
return($this->OutputError("the mailing was not yet ended"));
$this->ResetMessage();
$base_path=$this->mailings[$mailing]["BasePath"];
if(GetType($header_lines=@File($base_path.".h"))!="array")
return($this->OutputPHPError("could not read the mailing headers file ".$base_path.".h", error_get_last()['message']));
for($line=0;$line<count($header_lines);++$line)
{
$header_name=$this->Tokenize($header_lines[$line],": ");
$this->headers[$header_name]=trim($this->Tokenize("\n"));
}
if(!($envelope_file=@fopen($base_path.".e","rb")))
return($this->OutputPHPError("could not open the mailing envelope file ".$base_path.".e", error_get_last()['message']));
for($bcc=$data="",$position=0;!feof($envelope_file) || strlen($data);)
{
if(GetType($break=strpos($data,chr(0),$position))!="integer")
{
if(GetType($chunk=@fread($envelope_file,$this->file_buffer_length))!="string")
{
fclose($envelope_file);
return($this->OutputPHPError("could not read the mailing envelop file ".$base_path.".e", error_get_last()['message']));
}
$data=substr($data,$position).$chunk;
$position=0;
continue;
}
if($break==$position)
break;
switch($data[$position])
{
case "F":
$this->headers["Return-Path"]=substr($data,$position+1,$break-$position-1);
break;
case "T":
$bcc.=(strlen($bcc)==0 ? "" : ", ").substr($data,$position+1,$break-$position-1);
break;
default:
return($this->OutputError("invalid mailing envelope file ".$base_path.".e"));
}
$position=$break+1;
}
fclose($envelope_file);
if(strlen($bcc)==0)
return($this->OutputError("the mailing envelop file ".$base_path.".e does not contain any recipients"));
$this->headers["Bcc"]=$bcc;
if(!($body_file=@fopen($base_path.".b","rb")))
return($this->OutputPHPError("could not open the mailing body file ".$base_path.".b", error_get_last()['message']));
for($data="";!feof($body_file);)
{
if(GetType($chunk=@fread($body_file,$this->file_buffer_length))!="string")
{
fclose($body_file);
return($this->OutputPHPError("could not read the mailing body file ".$base_path.".b", error_get_last()['message']));
}
$data.=$chunk;
}
fclose($body_file);
if(strlen($error=$this->StartSendingMessage()))
return($error);
if(strlen($error=$this->SendMessageHeaders($this->headers))==0
&& strlen($error=$this->SendMessageBody($data))==0)
$error=$this->EndSendingMessage();
$this->StopSendingMessage();
return($error);
}
};
/*
{metadocument}
</class>
{/metadocument}
*/
?>
|