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 863 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 1863 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 2863 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 3863 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
|
package MIME::Lite;
use 5.006; ### for /c modifier in m/\G.../gc modifier
use strict;
use warnings;
=head1 NAME
MIME::Lite - low-calorie MIME generator
=head1 WAIT!
MIME::Lite is not recommended by its current maintainer. There are a number of
alternatives, like Email::MIME or MIME::Entity and Email::Sender, which you
should probably use instead. MIME::Lite continues to accrue weird bug reports,
and it is not receiving a large amount of refactoring due to the availability
of better alternatives. Please consider using something else.
=head1 SYNOPSIS
Create and send using the default send method for your OS a single-part message:
use MIME::Lite;
### Create a new single-part message, to send a GIF file:
$msg = MIME::Lite->new(
From => '[email protected]',
To => '[email protected]',
Cc => '[email protected], [email protected]',
Subject => 'Helloooooo, nurse!',
Type => 'image/gif',
Encoding => 'base64',
Path => 'hellonurse.gif'
);
$msg->send; # send via default
Create a multipart message (i.e., one with attachments) and send it via SMTP
### Create a new multipart message:
$msg = MIME::Lite->new(
From => '[email protected]',
To => '[email protected]',
Cc => '[email protected], [email protected]',
Subject => 'A message with 2 parts...',
Type => 'multipart/mixed'
);
### Add parts (each "attach" has same arguments as "new"):
$msg->attach(
Type => 'TEXT',
Data => "Here's the GIF file you wanted"
);
$msg->attach(
Type => 'image/gif',
Path => 'aaa000123.gif',
Filename => 'logo.gif',
Disposition => 'attachment'
);
### use Net::SMTP to do the sending
$msg->send('smtp','some.host', Debug=>1 );
Output a message:
### Format as a string:
$str = $msg->as_string;
### Print to a filehandle (say, a "sendmail" stream):
$msg->print(\*SENDMAIL);
Send a message:
### Send in the "best" way (the default is to use "sendmail"):
$msg->send;
### Send a specific way:
$msg->send('type',@args);
Specify default send method:
MIME::Lite->send('smtp','some.host',Debug=>0);
with authentication
MIME::Lite->send('smtp','some.host', AuthUser=>$user, AuthPass=>$pass);
using SSL
MIME::Lite->send('smtp','some.host', SSL => 1, Port => 465 );
=head1 DESCRIPTION
In the never-ending quest for great taste with fewer calories,
we proudly present: I<MIME::Lite>.
MIME::Lite is intended as a simple, standalone module for generating
(not parsing!) MIME messages... specifically, it allows you to
output a simple, decent single- or multi-part message with text or binary
attachments. It does not require that you have the Mail:: or MIME::
modules installed, but will work with them if they are.
You can specify each message part as either the literal data itself (in
a scalar or array), or as a string which can be given to open() to get
a readable filehandle (e.g., "<filename" or "somecommand|").
You don't need to worry about encoding your message data:
this module will do that for you. It handles the 5 standard MIME encodings.
=head1 EXAMPLES
=head2 Create a simple message containing just text
$msg = MIME::Lite->new(
From =>'[email protected]',
To =>'[email protected]',
Cc =>'[email protected], [email protected]',
Subject =>'Helloooooo, nurse!',
Data =>"How's it goin', eh?"
);
=head2 Create a simple message containing just an image
$msg = MIME::Lite->new(
From =>'[email protected]',
To =>'[email protected]',
Cc =>'[email protected], [email protected]',
Subject =>'Helloooooo, nurse!',
Type =>'image/gif',
Encoding =>'base64',
Path =>'hellonurse.gif'
);
=head2 Create a multipart message
### Create the multipart "container":
$msg = MIME::Lite->new(
From =>'[email protected]',
To =>'[email protected]',
Cc =>'[email protected], [email protected]',
Subject =>'A message with 2 parts...',
Type =>'multipart/mixed'
);
### Add the text message part:
### (Note that "attach" has same arguments as "new"):
$msg->attach(
Type =>'TEXT',
Data =>"Here's the GIF file you wanted"
);
### Add the image part:
$msg->attach(
Type =>'image/gif',
Path =>'aaa000123.gif',
Filename =>'logo.gif',
Disposition => 'attachment'
);
=head2 Attach a GIF to a text message
This will create a multipart message exactly as above, but using the
"attach to singlepart" hack:
### Start with a simple text message:
$msg = MIME::Lite->new(
From =>'[email protected]',
To =>'[email protected]',
Cc =>'[email protected], [email protected]',
Subject =>'A message with 2 parts...',
Type =>'TEXT',
Data =>"Here's the GIF file you wanted"
);
### Attach a part... the make the message a multipart automatically:
$msg->attach(
Type =>'image/gif',
Path =>'aaa000123.gif',
Filename =>'logo.gif'
);
=head2 Attach a pre-prepared part to a message
### Create a standalone part:
$part = MIME::Lite->new(
Top => 0,
Type =>'text/html',
Data =>'<H1>Hello</H1>',
);
$part->attr('content-type.charset' => 'UTF-8');
$part->add('X-Comment' => 'A message for you');
### Attach it to any message:
$msg->attach($part);
=head2 Print a message to a filehandle
### Write it to a filehandle:
$msg->print(\*STDOUT);
### Write just the header:
$msg->print_header(\*STDOUT);
### Write just the encoded body:
$msg->print_body(\*STDOUT);
=head2 Print a message into a string
### Get entire message as a string:
$str = $msg->as_string;
### Get just the header:
$str = $msg->header_as_string;
### Get just the encoded body:
$str = $msg->body_as_string;
=head2 Send a message
### Send in the "best" way (the default is to use "sendmail"):
$msg->send;
=head2 Send an HTML document... with images included!
$msg = MIME::Lite->new(
To =>'[email protected]',
Subject =>'HTML with in-line images!',
Type =>'multipart/related'
);
$msg->attach(
Type => 'text/html',
Data => qq{
<body>
Here's <i>my</i> image:
<img src="cid:myimage.gif">
</body>
},
);
$msg->attach(
Type => 'image/gif',
Id => 'myimage.gif',
Path => '/path/to/somefile.gif',
);
$msg->send();
=head2 Change how messages are sent
### Do something like this in your 'main':
if ($I_DONT_HAVE_SENDMAIL) {
MIME::Lite->send('smtp', $host, Timeout=>60,
AuthUser=>$user, AuthPass=>$pass);
}
### Now this will do the right thing:
$msg->send; ### will now use Net::SMTP as shown above
=head1 PUBLIC INTERFACE
=head2 Global configuration
To alter the way the entire module behaves, you have the following
methods/options:
=over 4
=item MIME::Lite->field_order()
When used as a L<classmethod|/field_order>, this changes the default
order in which headers are output for I<all> messages.
However, please consider using the instance method variant instead,
so you won't stomp on other message senders in the same application.
=item MIME::Lite->quiet()
This L<classmethod|/quiet> can be used to suppress/unsuppress
all warnings coming from this module.
=item MIME::Lite->send()
When used as a L<classmethod|/send>, this can be used to specify
a different default mechanism for sending message.
The initial default is:
MIME::Lite->send("sendmail", "/usr/lib/sendmail -t -oi -oem");
However, you should consider the similar but smarter and taint-safe variant:
MIME::Lite->send("sendmail");
Or, for non-Unix users:
MIME::Lite->send("smtp");
=item $MIME::Lite::AUTO_CC
If true, automatically send to the Cc/Bcc addresses for send_by_smtp().
Default is B<true>.
=item $MIME::Lite::AUTO_CONTENT_TYPE
If true, try to automatically choose the content type from the file name
in C<new()>/C<build()>. In other words, setting this true changes the
default C<Type> from C<"TEXT"> to C<"AUTO">.
Default is B<false>, since we must maintain backwards-compatibility
with prior behavior. B<Please> consider keeping it false,
and just using Type 'AUTO' when you build() or attach().
=item $MIME::Lite::AUTO_ENCODE
If true, automatically choose the encoding from the content type.
Default is B<true>.
=item $MIME::Lite::AUTO_VERIFY
If true, check paths to attachments right before printing, raising an exception
if any path is unreadable.
Default is B<true>.
=item $MIME::Lite::PARANOID
If true, we won't attempt to use MIME::Base64, MIME::QuotedPrint,
or MIME::Types, even if they're available.
Default is B<false>. Please consider keeping it false,
and trusting these other packages to do the right thing.
=back
=cut
use Carp ();
use FileHandle;
use vars qw(
$AUTO_CC
$AUTO_CONTENT_TYPE
$AUTO_ENCODE
$AUTO_VERIFY
$PARANOID
$QUIET
$VANILLA
$VERSION
$DEBUG
);
# GLOBALS, EXTERNAL/CONFIGURATION...
$VERSION = '3.033';
### Automatically interpret CC/BCC for SMTP:
$AUTO_CC = 1;
### Automatically choose content type from file name:
$AUTO_CONTENT_TYPE = 0;
### Automatically choose encoding from content type:
$AUTO_ENCODE = 1;
### Check paths right before printing:
$AUTO_VERIFY = 1;
### Set this true if you don't want to use MIME::Base64/QuotedPrint/Types:
$PARANOID = 0;
### Don't warn me about dangerous activities:
$QUIET = undef;
### Unsupported (for tester use): don't qualify boundary with time/pid:
$VANILLA = 0;
$MIME::Lite::DEBUG = 0;
#==============================
#==============================
#
# GLOBALS, INTERNAL...
my $Sender = "";
my $SENDMAIL = "";
if ( $^O =~ /win32|cygwin/i ) {
$Sender = "smtp";
} else {
### Find sendmail:
$Sender = "sendmail";
$SENDMAIL = "/usr/lib/sendmail";
( -x $SENDMAIL ) or ( $SENDMAIL = "/usr/sbin/sendmail" );
( -x $SENDMAIL ) or ( $SENDMAIL = "sendmail" );
unless (-x $SENDMAIL) {
require File::Spec;
for my $dir (File::Spec->path) {
if ( -x "$dir/sendmail" ) {
$SENDMAIL = "$dir/sendmail";
last;
}
}
}
unless (-x $SENDMAIL) {
undef $SENDMAIL;
}
}
### Our sending facilities:
my %SenderArgs = (
sendmail => [],
smtp => [],
sub => [],
);
### Boundary counter:
my $BCount = 0;
### Known Mail/MIME fields... these, plus some general forms like
### "x-*", are recognized by build():
my %KnownField = map { $_ => 1 }
qw(
bcc cc comments date encrypted
from keywords message-id mime-version organization
received references reply-to return-path sender
subject to
approved
);
### What external packages do we use for encoding?
my @Uses;
### Header order:
my @FieldOrder;
### See if we have File::Basename
my $HaveFileBasename = 0;
if ( eval "require File::Basename" ) { # not affected by $PARANOID, core Perl
$HaveFileBasename = 1;
push @Uses, "F$File::Basename::VERSION";
}
### See if we have/want MIME::Types
my $HaveMimeTypes = 0;
if ( !$PARANOID and eval "require MIME::Types; MIME::Types->VERSION(1.28);" ) {
$HaveMimeTypes = 1;
push @Uses, "T$MIME::Types::VERSION";
}
#==============================
#==============================
#
# PRIVATE UTILITY FUNCTIONS...
#------------------------------
#
# fold STRING
#
# Make STRING safe as a field value. Remove leading/trailing whitespace,
# and make sure newlines are represented as newline space
sub fold {
my $str = shift;
$str =~ s/^\s*|\s*$//g; ### trim
$str =~ s/\n/\n /g;
$str;
}
#------------------------------
#
# gen_boundary
#
# Generate a new boundary to use.
# The unsupported $VANILLA is for test purposes only.
sub gen_boundary {
return ( "_----------=_" . ( $VANILLA ? '' : int(time) . $$ ) . $BCount );
}
#------------------------------
#
# is_mime_field FIELDNAME
#
# Is this a field I manage?
sub is_mime_field {
$_[0] =~ /^(mime\-|content\-)/i;
}
#------------------------------
#
# extract_full_addrs STRING
# extract_only_addrs STRING
#
# Split STRING into an array of email addresses: somewhat of a KLUDGE.
#
# Unless paranoid, we try to load the real code before supplying our own.
BEGIN {
my $ATOM = '[^ \000-\037()<>@,;:\134"\056\133\135] ';
my $QSTR = '".*?"';
my $WORD = '(?:' . $QSTR . '|' . $ATOM . ')';
my $DOMAIN = '(?:' . $ATOM . '(?:' . '\\.' . $ATOM . ')*' . ')';
my $LOCALPART = '(?:' . $WORD . '(?:' . '\\.' . $WORD . ')*' . ')';
my $ADDR = '(?:' . $LOCALPART . '@' . $DOMAIN . ')';
my $PHRASE = '(?:' . $WORD . ') ';
my $SEP = "(?:^\\s*|\\s*,\\s*)"; ### before elems in a list
sub my_extract_full_addrs {
my $str = shift;
return unless $str;
my @addrs;
$str =~ s/\s/ /g; ### collapse whitespace
pos($str) = 0;
while ( $str !~ m{\G\s*\Z}gco ) {
### print STDERR "TACKLING: ".substr($str, pos($str))."\n";
if ( $str =~ m{\G$SEP($PHRASE)\s*<\s*($ADDR)\s*>}gco ) {
push @addrs, "$1 <$2>";
} elsif ( $str =~ m{\G$SEP($ADDR)}gco or $str =~ m{\G$SEP($ATOM)}gco ) {
push @addrs, $1;
} else {
my $problem = substr( $str, pos($str) );
die "can't extract address at <$problem> in <$str>\n";
}
}
return wantarray ? @addrs : $addrs[0];
}
sub my_extract_only_addrs {
my @ret = map { /<([^>] )>/ ? $1 : $_ } my_extract_full_addrs(@_);
return wantarray ? @ret : $ret[0];
}
}
#------------------------------
if ( !$PARANOID and eval "require Mail::Address" ) {
push @Uses, "A$Mail::Address::VERSION";
eval q{
sub extract_full_addrs {
my @ret=map { $_->format } Mail::Address->parse($_[0]);
return wantarray ? @ret : $ret[0]
}
sub extract_only_addrs {
my @ret=map { $_->address } Mail::Address->parse($_[0]);
return wantarray ? @ret : $ret[0]
}
}; ### q
} else {
eval q{
*extract_full_addrs=*my_extract_full_addrs;
*extract_only_addrs=*my_extract_only_addrs;
}; ### q
} ### if
#==============================
#==============================
#
# PRIVATE ENCODING FUNCTIONS...
#------------------------------
#
# encode_base64 STRING
#
# Encode the given string using BASE64.
# Unless paranoid, we try to load the real code before supplying our own.
if ( !$PARANOID and eval "require MIME::Base64" ) {
import MIME::Base64 qw(encode_base64);
push @Uses, "B$MIME::Base64::VERSION";
} else {
eval q{
sub encode_base64 {
my $res = "";
my $eol = "\n";
pos($_[0]) = 0; ### thanks, Andreas!
while ($_[0] =~ /(.{1,45})/gs) {
$res .= substr(pack('u', $1), 1);
chop($res);
}
$res =~ tr|` -_|AA-Za-z0-9 /|;
### Fix padding at the end:
my $padding = (3 - length($_[0]) % 3) % 3;
$res =~ s/.{$padding}$/'=' x $padding/e if $padding;
### Break encoded string into lines of no more than 76 characters each:
$res =~ s/(.{1,76})/$1$eol/g if (length $eol);
return $res;
} ### sub
} ### q
} ### if
#------------------------------
#
# encode_qp STRING
#
# Encode the given string, LINE BY LINE, using QUOTED-PRINTABLE.
# Stolen from MIME::QuotedPrint by Gisle Aas, with a slight bug fix: we
# break lines earlier. Notice that this seems not to work unless
# encoding line by line.
#
# Unless paranoid, we try to load the real code before supplying our own.
if ( !$PARANOID and eval "require MIME::QuotedPrint" ) {
import MIME::QuotedPrint qw(encode_qp);
push @Uses, "Q$MIME::QuotedPrint::VERSION";
} else {
eval q{
sub encode_qp {
my $res = shift;
local($_);
$res =~ s/([^ \t\n!-<>-~])/sprintf("=X", ord($1))/eg; ### rule #2,#3
$res =~ s/([ \t] )$/
join('', map { sprintf("=X", ord($_)) }
split('', $1)
)/egm; ### rule #3 (encode whitespace at eol)
### rule #5 (lines shorter than 76 chars, but can't break =XX escapes:
my $brokenlines = "";
$brokenlines .= "$1=\n" while $res =~ s/^(.{70}([^=]{2})?)//; ### 70 was 74
$brokenlines =~ s/=\n$// unless length $res;
"$brokenlines$res";
} ### sub
} ### q
} ### if
#------------------------------
#
# encode_8bit STRING
#
# Encode the given string using 8BIT.
# This breaks long lines into shorter ones.
sub encode_8bit {
my $str = shift;
$str =~ s/^(.{990})/$1\n/mg;
$str;
}
#------------------------------
#
# encode_7bit STRING
#
# Encode the given string using 7BIT.
# This NO LONGER protects people through encoding.
sub encode_7bit {
my $str = shift;
$str =~ s/[\x80-\xFF]//g;
$str =~ s/^(.{990})/$1\n/mg;
$str;
}
#==============================
#==============================
=head2 Construction
=over 4
=cut
#------------------------------
=item new [PARAMHASH]
I<Class method, constructor.>
Create a new message object.
If any arguments are given, they are passed into C<build()>; otherwise,
just the empty object is created.
=cut
sub new {
my $class = shift;
### Create basic object:
my $self = { Attrs => {}, ### MIME attributes
SubAttrs => {}, ### MIME sub-attributes
Header => [], ### explicit message headers
Parts => [], ### array of parts
};
bless $self, $class;
### Build, if needed:
return ( @_ ? $self->build(@_) : $self );
}
#------------------------------
=item attach PART
=item attach PARAMHASH...
I<Instance method.>
Add a new part to this message, and return the new part.
If you supply a single PART argument, it will be regarded
as a MIME::Lite object to be attached. Otherwise, this
method assumes that you are giving in the pairs of a PARAMHASH
which will be sent into C<new()> to create the new part.
One of the possibly-quite-useful hacks thrown into this is the
"attach-to-singlepart" hack: if you attempt to attach a part (let's
call it "part 1") to a message that doesn't have a content-type
of "multipart" or "message", the following happens:
=over 4
=item *
A new part (call it "part 0") is made.
=item *
The MIME attributes and data (but I<not> the other headers)
are cut from the "self" message, and pasted into "part 0".
=item *
The "self" is turned into a "multipart/mixed" message.
=item *
The new "part 0" is added to the "self", and I<then> "part 1" is added.
=back
One of the nice side-effects is that you can create a text message
and then add zero or more attachments to it, much in the same way
that a user agent like Netscape allows you to do.
=cut
sub attach {
my $self = shift;
my $attrs = $self->{Attrs};
my $sub_attrs = $self->{SubAttrs};
### Create new part, if necessary:
my $part1 = ( ( @_ == 1 ) ? shift: ref($self)->new( Top => 0, @_ ) );
### Do the "attach-to-singlepart" hack:
if ( $attrs->{'content-type'} !~ m{^(multipart|message)/}i ) {
### Create part zero:
my $part0 = ref($self)->new;
### Cut MIME stuff from self, and paste into part zero:
foreach (qw(SubAttrs Attrs Data Path FH)) {
$part0->{$_} = $self->{$_};
delete( $self->{$_} );
}
$part0->top_level(0); ### clear top-level attributes
### Make self a top-level multipart:
$attrs = $self->{Attrs} ||= {}; ### reset (sam: bug? this doesn't reset anything since Attrs is already a hash-ref)
$sub_attrs = $self->{SubAttrs} ||= {}; ### reset
$attrs->{'content-type'} = 'multipart/mixed';
$sub_attrs->{'content-type'}{'boundary'} = gen_boundary();
$attrs->{'content-transfer-encoding'} = '7bit';
$self->top_level(1); ### activate top-level attributes
### Add part 0:
push @{ $self->{Parts} }, $part0;
}
### Add the new part:
push @{ $self->{Parts} }, $part1;
$part1;
}
#------------------------------
=item build [PARAMHASH]
I<Class/instance method, initializer.>
Create (or initialize) a MIME message object.
Normally, you'll use the following keys in PARAMHASH:
* Data, FH, or Path (either one of these, or none if multipart)
* Type (e.g., "image/jpeg")
* From, To, and Subject (if this is the "top level" of a message)
The PARAMHASH can contain the following keys:
=over 4
=item (fieldname)
Any field you want placed in the message header, taken from the
standard list of header fields (you don't need to worry about case):
Approved Encrypted Received Sender
Bcc From References Subject
Cc Keywords Reply-To To
Comments Message-ID Resent-* X-*
Content-* MIME-Version Return-Path
Date Organization
To give experienced users some veto power, these fields will be set
I<after> the ones I set... so be careful: I<don't set any MIME fields>
(like C<Content-type>) unless you know what you're doing!
To specify a fieldname that's I<not> in the above list, even one that's
identical to an option below, just give it with a trailing C<":">,
like C<"My-field:">. When in doubt, that I<always> signals a mail
field (and it sort of looks like one too).
=item Data
I<Alternative to "Path" or "FH".>
The actual message data. This may be a scalar or a ref to an array of
strings; if the latter, the message consists of a simple concatenation
of all the strings in the array.
=item Datestamp
I<Optional.>
If given true (or omitted), we force the creation of a C<Date:> field
stamped with the current date/time if this is a top-level message.
You may want this if using L<send_by_smtp()|/send_by_smtp>.
If you don't want this to be done, either provide your own Date
or explicitly set this to false.
=item Disposition
I<Optional.>
The content disposition, C<"inline"> or C<"attachment">.
The default is C<"inline">.
=item Encoding
I<Optional.>
The content transfer encoding that should be used to encode your data:
Use encoding: | If your message contains:
------------------------------------------------------------
7bit | Only 7-bit text, all lines <1000 characters
8bit | 8-bit text, all lines <1000 characters
quoted-printable | 8-bit text or long lines (more reliable than "8bit")
base64 | Largely non-textual data: a GIF, a tar file, etc.
The default is taken from the Type; generally it is "binary" (no
encoding) for text/*, message/*, and multipart/*, and "base64" for
everything else. A value of C<"binary"> is generally I<not> suitable
for sending anything but ASCII text files with lines under 1000
characters, so consider using one of the other values instead.
In the case of "7bit"/"8bit", long lines are automatically chopped to
legal length; in the case of "7bit", all 8-bit characters are
automatically I<removed>. This may not be what you want, so pick your
encoding well! For more info, see L<"A MIME PRIMER">.
=item FH
I<Alternative to "Data" or "Path".>
Filehandle containing the data, opened for reading.
See "ReadNow" also.
=item Filename
I<Optional.>
The name of the attachment. You can use this to supply a
recommended filename for the end-user who is saving the attachment
to disk. You only need this if the filename at the end of the
"Path" is inadequate, or if you're using "Data" instead of "Path".
You should I<not> put path information in here (e.g., no "/"
or "\" or ":" characters should be used).
=item Id
I<Optional.>
Same as setting "content-id".
=item Length
I<Optional.>
Set the content length explicitly. Normally, this header is automatically
computed, but only under certain circumstances (see L<"Benign limitations">).
=item Path
I<Alternative to "Data" or "FH".>
Path to a file containing the data... actually, it can be any open()able
expression. If it looks like a path, the last element will automatically
be treated as the filename.
See "ReadNow" also.
=item ReadNow
I<Optional, for use with "Path".>
If true, will open the path and slurp the contents into core now.
This is useful if the Path points to a command and you don't want
to run the command over and over if outputting the message several
times. B<Fatal exception> raised if the open fails.
=item Top
I<Optional.>
If defined, indicates whether or not this is a "top-level" MIME message.
The parts of a multipart message are I<not> top-level.
Default is true.
=item Type
I<Optional.>
The MIME content type, or one of these special values (case-sensitive):
"TEXT" means "text/plain"
"BINARY" means "application/octet-stream"
"AUTO" means attempt to guess from the filename, falling back
to 'application/octet-stream'. This is good if you have
MIME::Types on your system and you have no idea what
file might be used for the attachment.
The default is C<"TEXT">, but it will be C<"AUTO"> if you set
$AUTO_CONTENT_TYPE to true (sorry, but you have to enable
it explicitly, since we don't want to break code which depends
on the old behavior).
=back
A picture being worth 1000 words (which
is of course 2000 bytes, so it's probably more of an "icon" than a "picture",
but I digress...), here are some examples:
$msg = MIME::Lite->build(
From => '[email protected]',
To => '[email protected]',
Subject => "Hi there!",
Type => 'TEXT',
Encoding => '7bit',
Data => "Just a quick note to say hi!"
);
$msg = MIME::Lite->build(
From => '[email protected]',
To => '[email protected]',
Subject => "A gif for U"
Type => 'image/gif',
Path => "/home/httpd/logo.gif"
);
$msg = MIME::Lite->build(
From => '[email protected]',
To => '[email protected]',
Subject => "A gzipp'ed tar file",
Type => 'x-gzip',
Path => "gzip < /usr/inc/somefile.tar |",
ReadNow => 1,
Filename => "somefile.tgz"
);
To show you what's really going on, that last example could also
have been written:
$msg = new MIME::Lite;
$msg->build(
Type => 'x-gzip',
Path => "gzip < /usr/inc/somefile.tar |",
ReadNow => 1,
Filename => "somefile.tgz"
);
$msg->add(From => "[email protected]");
$msg->add(To => "[email protected]");
$msg->add(Subject => "A gzipp'ed tar file");
=cut
sub build {
my $self = shift;
my %params = @_;
my @params = @_;
my $key;
### Miko's note: reorganized to check for exactly one of Data, Path, or FH
( defined( $params{Data} ) defined( $params{Path} ) defined( $params{FH} ) <= 1 )
or Carp::croak "supply exactly zero or one of (Data|Path|FH).\n";
### Create new instance, if necessary:
ref($self) or $self = $self->new;
### CONTENT-TYPE....
###
### Get content-type or content-type-macro:
my $type = ( $params{Type} || ( $AUTO_CONTENT_TYPE ? 'AUTO' : 'TEXT' ) );
### Interpret content-type-macros:
if ( $type eq 'TEXT' ) { $type = 'text/plain'; }
elsif ( $type eq 'HTML' ) { $type = 'text/html'; }
elsif ( $type eq 'BINARY' ) { $type = 'application/octet-stream' }
elsif ( $type eq 'AUTO' ) { $type = $self->suggest_type( $params{Path} ); }
### We now have a content-type; set it:
$type = lc($type);
my $attrs = $self->{Attrs};
my $sub_attrs = $self->{SubAttrs};
$attrs->{'content-type'} = $type;
### Get some basic attributes from the content type:
my $is_multipart = ( $type =~ m{^(multipart)/}i );
### Add in the multipart boundary:
if ($is_multipart) {
my $boundary = gen_boundary();
$sub_attrs->{'content-type'}{'boundary'} = $boundary;
}
### CONTENT-ID...
###
if ( defined $params{Id} ) {
my $id = $params{Id};
$id = "<$id>" unless $id =~ /\A\s*<.*>\s*\z/;
$attrs->{'content-id'} = $id;
}
### DATA OR PATH...
### Note that we must do this *after* we get the content type,
### in case read_now() is invoked, since it needs the binmode().
### Get data, as...
### ...either literal data:
if ( defined( $params{Data} ) ) {
$self->data( $params{Data} );
}
### ...or a path to data:
elsif ( defined( $params{Path} ) ) {
$self->path( $params{Path} ); ### also sets filename
$self->read_now if $params{ReadNow};
}
### ...or a filehandle to data:
### Miko's note: this part works much like the path routine just above,
elsif ( defined( $params{FH} ) ) {
$self->fh( $params{FH} );
$self->read_now if $params{ReadNow}; ### implement later
}
### FILENAME... (added by Ian Smith <[email protected]> on 8/4/97)
### Need this to make sure the filename is added. The Filename
### attribute is ignored, otherwise.
if ( defined( $params{Filename} ) ) {
$self->filename( $params{Filename} );
}
### CONTENT-TRANSFER-ENCODING...
###
### Get it:
my $enc =
( $params{Encoding} || ( $AUTO_ENCODE and $self->suggest_encoding($type) ) || 'binary' );
$attrs->{'content-transfer-encoding'} = lc($enc);
### Sanity check:
if ( $type =~ m{^(multipart|message)/} ) {
( $enc =~ m{^(7bit|8bit|binary)\Z} )
or Carp::croak( "illegal MIME: " . "can't have encoding $enc with type $type\n" );
}
### CONTENT-DISPOSITION...
### Default is inline for single, none for multis:
###
my $disp = ( $params{Disposition} or ( $is_multipart ? undef: 'inline' ) );
$attrs->{'content-disposition'} = $disp;
### CONTENT-LENGTH...
###
my $length;
if ( exists( $params{Length} ) ) { ### given by caller:
$attrs->{'content-length'} = $params{Length};
} else { ### compute it ourselves
$self->get_length;
}
### Init the top-level fields:
my $is_top = defined( $params{Top} ) ? $params{Top} : 1;
$self->top_level($is_top);
### Datestamp if desired:
my $ds_wanted = $params{Datestamp};
my $ds_defaulted = ( $is_top and !exists( $params{Datestamp} ) );
if ( ( $ds_wanted or $ds_defaulted ) and !exists( $params{Date} ) ) {
require Email::Date::Format;
$self->add( "date", Email::Date::Format::email_date() );
}
### Set message headers:
my @paramz = @params;
my $field;
while (@paramz) {
my ( $tag, $value ) = ( shift(@paramz), shift(@paramz) );
my $lc_tag = lc($tag);
### Get tag, if a tag:
if ( $lc_tag =~ /^-(.*)/ ) { ### old style, backwards-compatibility
$field = $1;
} elsif ( $lc_tag =~ /^(.*):$/ ) { ### new style
$field = $1;
} elsif ( $KnownField{$lc_tag} or
$lc_tag =~ m{^(content|resent|x)-.} ){
$field = $lc_tag;
} else { ### not a field:
next;
}
### Add it:
$self->add( $field, $value );
}
### Done!
$self;
}
=back
=cut
#==============================
#==============================
=head2 Setting/getting headers and attributes
=over 4
=cut
#------------------------------
#
# top_level ONOFF
#
# Set/unset the top-level attributes and headers.
# This affects "MIME-Version" and "X-Mailer".
sub top_level {
my ( $self, $onoff ) = @_;
my $attrs = $self->{Attrs};
if ($onoff) {
$attrs->{'mime-version'} = '1.0';
my $uses = ( @Uses ? ( "(" . join( "; ", @Uses ) . ")" ) : '' );
$self->replace( 'X-Mailer' => "MIME::Lite $VERSION $uses" )
unless $VANILLA;
} else {
delete $attrs->{'mime-version'};
$self->delete('X-Mailer');
}
}
#------------------------------
=item add TAG,VALUE
I<Instance method.>
Add field TAG with the given VALUE to the end of the header.
The TAG will be converted to all-lowercase, and the VALUE
will be made "safe" (returns will be given a trailing space).
B<Beware:> any MIME fields you "add" will override any MIME
attributes I have when it comes time to output those fields.
Normally, you will use this method to add I<non-MIME> fields:
$msg->add("Subject" => "Hi there!");
Giving VALUE as an arrayref will cause all those values to be added.
This is only useful for special multiple-valued fields like "Received":
$msg->add("Received" => ["here", "there", "everywhere"]
Giving VALUE as the empty string adds an invisible placeholder
to the header, which can be used to suppress the output of
the "Content-*" fields or the special "MIME-Version" field.
When suppressing fields, you should use replace() instead of add():
$msg->replace("Content-disposition" => "");
I<Note:> add() is probably going to be more efficient than C<replace()>,
so you're better off using it for most applications if you are
certain that you don't need to delete() the field first.
I<Note:> the name comes from Mail::Header.
=cut
sub add {
my $self = shift;
my $tag = lc(shift);
my $value = shift;
### If a dangerous option, warn them:
Carp::carp "Explicitly setting a MIME header field ($tag) is dangerous:\n"
. "use the attr() method instead.\n"
if ( is_mime_field($tag) && !$QUIET );
### Get array of clean values:
my @vals = ( ( ref($value) and ( ref($value) eq 'ARRAY' ) )
? @{$value}
: ( $value . '' )
);
map { s/\n/\n /g } @vals;
### Add them:
foreach (@vals) {
push @{ $self->{Header} }, [ $tag, $_ ];
}
}
#------------------------------
=item attr ATTR,[VALUE]
I<Instance method.>
Set MIME attribute ATTR to the string VALUE.
ATTR is converted to all-lowercase.
This method is normally used to set/get MIME attributes:
$msg->attr("content-type" => "text/html");
$msg->attr("content-type.charset" => "US-ASCII");
$msg->attr("content-type.name" => "homepage.html");
This would cause the final output to look something like this:
Content-type: text/html; charset=US-ASCII; name="homepage.html"
Note that the special empty sub-field tag indicates the anonymous
first sub-field.
Giving VALUE as undefined will cause the contents of the named
subfield to be deleted.
Supplying no VALUE argument just returns the attribute's value:
$type = $msg->attr("content-type"); ### returns "text/html"
$name = $msg->attr("content-type.name"); ### returns "homepage.html"
=cut
sub attr {
my ( $self, $attr, $value ) = @_;
my $attrs = $self->{Attrs};
$attr = lc($attr);
### Break attribute name up:
my ( $tag, $subtag ) = split /\./, $attr;
if (defined($subtag)) {
$attrs = $self->{SubAttrs}{$tag} ||= {};
$tag = $subtag;
}
### Set or get?
if ( @_ > 2 ) { ### set:
if ( defined($value) ) {
$attrs->{$tag} = $value;
} else {
delete $attrs->{$tag};
}
}
### Return current value:
$attrs->{$tag};
}
sub _safe_attr {
my ( $self, $attr ) = @_;
return defined $self->{Attrs}{$attr} ? $self->{Attrs}{$attr} : '';
}
#------------------------------
=item delete TAG
I<Instance method.>
Delete field TAG with the given VALUE to the end of the header.
The TAG will be converted to all-lowercase.
$msg->delete("Subject");
I<Note:> the name comes from Mail::Header.
=cut
sub delete {
my $self = shift;
my $tag = lc(shift);
### Delete from the header:
my $hdr = [];
my $field;
foreach $field ( @{ $self->{Header} } ) {
push @$hdr, $field if ( $field->[0] ne $tag );
}
$self->{Header} = $hdr;
$self;
}
#------------------------------
=item field_order FIELD,...FIELD
I<Class/instance method.>
Change the order in which header fields are output for this object:
$msg->field_order('from', 'to', 'content-type', 'subject');
When used as a class method, changes the default settings for
all objects:
MIME::Lite->field_order('from', 'to', 'content-type', 'subject');
Case does not matter: all field names will be coerced to lowercase.
In either case, supply the empty array to restore the default ordering.
=cut
sub field_order {
my $self = shift;
if ( ref($self) ) {
$self->{FieldOrder} = [ map { lc($_) } @_ ];
} else {
@FieldOrder = map { lc($_) } @_;
}
}
#------------------------------
=item fields
I<Instance method.>
Return the full header for the object, as a ref to an array
of C<[TAG, VALUE]> pairs, where each TAG is all-lowercase.
Note that any fields the user has explicitly set will override the
corresponding MIME fields that we would otherwise generate.
So, don't say...
$msg->set("Content-type" => "text/html; charset=US-ASCII");
unless you want the above value to override the "Content-type"
MIME field that we would normally generate.
I<Note:> I called this "fields" because the header() method of
Mail::Header returns something different, but similar enough to
be confusing.
You can change the order of the fields: see L</field_order>.
You really shouldn't need to do this, but some people have to
deal with broken mailers.
=cut
sub fields {
my $self = shift;
my @fields;
my $attrs = $self->{Attrs};
my $sub_attrs = $self->{SubAttrs};
### Get a lookup-hash of all *explicitly-given* fields:
my %explicit = map { $_->[0] => 1 } @{ $self->{Header} };
### Start with any MIME attributes not given explicitly:
my $tag;
foreach $tag ( sort keys %{ $self->{Attrs} } ) {
### Skip if explicit:
next if ( $explicit{$tag} );
# get base attr value or skip if not available
my $value = $attrs->{$tag};
defined $value or next;
### handle sub-attrs if available
if (my $subs = $sub_attrs->{$tag}) {
$value .= '; ' .
join('; ', map { qq{$_="$subs->{$_}"} } sort keys %$subs);
}
# handle stripping \r\n now since we're not doing it in attr()
# anymore
$value =~ tr/\r\n//;
### Add to running fields;
push @fields, [ $tag, $value ];
}
### Add remaining fields (note that we duplicate the array for safety):
foreach ( @{ $self->{Header} } ) {
push @fields, [ @{$_} ];
}
### Final step:
### If a suggested ordering was given, we "sort" by that ordering.
### The idea is that we give each field a numeric rank, which is
### (1000 * order(field)) origposition.
my @order = @{ $self->{FieldOrder} || [] }; ### object-specific
@order or @order = @FieldOrder; ### no? maybe generic
if (@order) { ### either?
### Create hash mapping field names to 1-based rank:
my %rank = map { $order[$_] => ( 1 $_ ) } ( 0 .. $#order );
### Create parallel array to @fields, called @ranked.
### It contains fields tagged with numbers like 2003, where the
### 3 is the original 0-based position, and 2000 indicates that
### we wanted this type of field to go second.
my @ranked = map {
[ ( $_ 1000 * ( $rank{ lc( $fields[$_][0] ) } || ( 2 $#order ) ) ), $fields[$_] ]
} ( 0 .. $#fields );
# foreach (@ranked) {
# print STDERR "RANKED: $_->[0] $_->[1][0] $_->[1][1]\n";
# }
### That was half the Schwartzian transform. Here's the rest:
@fields = map { $_->[1] }
sort { $a->[0] <=> $b->[0] } @ranked;
}
### Done!
return \@fields;
}
#------------------------------
=item filename [FILENAME]
I<Instance method.>
Set the filename which this data will be reported as.
This actually sets both "standard" attributes.
With no argument, returns the filename as dictated by the
content-disposition.
=cut
sub filename {
my ( $self, $filename ) = @_;
my $sub_attrs = $self->{SubAttrs};
if ( @_ > 1 ) {
$sub_attrs->{'content-type'}{'name'} = $filename;
$sub_attrs->{'content-disposition'}{'filename'} = $filename;
}
return $sub_attrs->{'content-disposition'}{'filename'};
}
#------------------------------
=item get TAG,[INDEX]
I<Instance method.>
Get the contents of field TAG, which might have been set
with set() or replace(). Returns the text of the field.
$ml->get('Subject', 0);
If the optional 0-based INDEX is given, then we return the INDEX'th
occurrence of field TAG. Otherwise, we look at the context:
In a scalar context, only the first (0th) occurrence of the
field is returned; in an array context, I<all> occurrences are returned.
I<Warning:> this should only be used with non-MIME fields.
Behavior with MIME fields is TBD, and will raise an exception for now.
=cut
sub get {
my ( $self, $tag, $index ) = @_;
$tag = lc($tag);
Carp::croak "get: can't be used with MIME fields\n" if is_mime_field($tag);
my @all = map { ( $_->[0] eq $tag ) ? $_->[1] : () } @{ $self->{Header} };
( defined($index) ? $all[$index] : ( wantarray ? @all : $all[0] ) );
}
#------------------------------
=item get_length
I<Instance method.>
Recompute the content length for the message I<if the process is trivial>,
setting the "content-length" attribute as a side-effect:
$msg->get_length;
Returns the length, or undefined if not set.
I<Note:> the content length can be difficult to compute, since it
involves assembling the entire encoded body and taking the length
of it (which, in the case of multipart messages, means freezing
all the sub-parts, etc.).
This method only sets the content length to a defined value if the
message is a singlepart with C<"binary"> encoding, I<and> the body is
available either in-core or as a simple file. Otherwise, the content
length is set to the undefined value.
Since content-length is not a standard MIME field anyway (that's right, kids:
it's not in the MIME RFCs, it's an HTTP thing), this seems pretty fair.
=cut
#----
# Miko's note: I wasn't quite sure how to handle this, so I waited to hear
# what you think. Given that the content-length isn't always required,
# and given the performance cost of calculating it from a file handle,
# I thought it might make more sense to add some sort of computelength
# property. If computelength is false, then the length simply isn't
# computed. What do you think?
#
# Eryq's reply: I agree; for now, we can silently leave out the content-type.
sub get_length {
my $self = shift;
my $attrs = $self->{Attrs};
my $is_multipart = ( $attrs->{'content-type'} =~ m{^multipart/}i );
my $enc = lc( $attrs->{'content-transfer-encoding'} || 'binary' );
my $length;
if ( !$is_multipart && ( $enc eq "binary" ) ) { ### might figure it out cheap:
if ( defined( $self->{Data} ) ) { ### it's in core
$length = length( $self->{Data} );
} elsif ( defined( $self->{FH} ) ) { ### it's in a filehandle
### no-op: it's expensive, so don't bother
} elsif ( defined( $self->{Path} ) ) { ### it's a simple file!
$length = ( -s $self->{Path} ) if ( -e $self->{Path} );
}
}
$attrs->{'content-length'} = $length;
return $length;
}
#------------------------------
=item parts
I<Instance method.>
Return the parts of this entity, and this entity only.
Returns empty array if this entity has no parts.
This is B<not> recursive! Parts can have sub-parts; use
parts_DFS() to get everything.
=cut
sub parts {
my $self = shift;
@{ $self->{Parts} || [] };
}
#------------------------------
=item parts_DFS
I<Instance method.>
Return the list of all MIME::Lite objects included in the entity,
starting with the entity itself, in depth-first-search order.
If this object has no parts, it alone will be returned.
=cut
sub parts_DFS {
my $self = shift;
return ( $self, map { $_->parts_DFS } $self->parts );
}
#------------------------------
=item preamble [TEXT]
I<Instance method.>
Get/set the preamble string, assuming that this object has subparts.
Set it to undef for the default string.
=cut
sub preamble {
my $self = shift;
$self->{Preamble} = shift if @_;
$self->{Preamble};
}
#------------------------------
=item replace TAG,VALUE
I<Instance method.>
Delete all occurrences of fields named TAG, and add a new
field with the given VALUE. TAG is converted to all-lowercase.
B<Beware> the special MIME fields (MIME-version, Content-*):
if you "replace" a MIME field, the replacement text will override
the I<actual> MIME attributes when it comes time to output that field.
So normally you use attr() to change MIME fields and add()/replace() to
change I<non-MIME> fields:
$msg->replace("Subject" => "Hi there!");
Giving VALUE as the I<empty string> will effectively I<prevent> that
field from being output. This is the correct way to suppress
the special MIME fields:
$msg->replace("Content-disposition" => "");
Giving VALUE as I<undefined> will just cause all explicit values
for TAG to be deleted, without having any new values added.
I<Note:> the name of this method comes from Mail::Header.
=cut
sub replace {
my ( $self, $tag, $value ) = @_;
$self->delete($tag);
$self->add( $tag, $value ) if defined($value);
}
#------------------------------
=item scrub
I<Instance method.>
B<This is Alpha code. If you use it, please let me know how it goes.>
Recursively goes through the "parts" tree of this message and tries
to find MIME attributes that can be removed.
With an array argument, removes exactly those attributes; e.g.:
$msg->scrub(['content-disposition', 'content-length']);
Is the same as recursively doing:
$msg->replace('Content-disposition' => '');
$msg->replace('Content-length' => '');
=cut
sub scrub {
my ( $self, @a ) = @_;
my ($expl) = @a;
local $QUIET = 1;
### Scrub me:
if ( !@a ) { ### guess
### Scrub length always:
$self->replace( 'content-length', '' );
### Scrub disposition if no filename, or if content-type has same info:
if ( !$self->_safe_attr('content-disposition.filename')
|| $self->_safe_attr('content-type.name') )
{
$self->replace( 'content-disposition', '' );
}
### Scrub encoding if effectively unencoded:
if ( $self->_safe_attr('content-transfer-encoding') =~ /^(7bit|8bit|binary)$/i ) {
$self->replace( 'content-transfer-encoding', '' );
}
### Scrub charset if US-ASCII:
if ( $self->_safe_attr('content-type.charset') =~ /^(us-ascii)/i ) {
$self->attr( 'content-type.charset' => undef );
}
### TBD: this is not really right for message/digest:
if ( ( keys %{ $self->{Attrs}{'content-type'} } == 1 )
and ( $self->_safe_attr('content-type') eq 'text/plain' ) )
{
$self->replace( 'content-type', '' );
}
} elsif ( $expl and ( ref($expl) eq 'ARRAY' ) ) {
foreach ( @{$expl} ) { $self->replace( $_, '' ); }
}
### Scrub my kids:
foreach ( @{ $self->{Parts} } ) { $_->scrub(@a); }
}
=back
=cut
#==============================
#==============================
=head2 Setting/getting message data
=over 4
=cut
#------------------------------
=item binmode [OVERRIDE]
I<Instance method.>
With no argument, returns whether or not it thinks that the data
(as given by the "Path" argument of C<build()>) should be read using
binmode() (for example, when C<read_now()> is invoked).
The default behavior is that any content type other than
C<text/*> or C<message/*> is binmode'd; this should in general work fine.
With a defined argument, this method sets an explicit "override"
value. An undefined argument unsets the override.
The new current value is returned.
=cut
sub binmode {
my $self = shift;
$self->{Binmode} = shift if (@_); ### argument? set override
return ( defined( $self->{Binmode} )
? $self->{Binmode}
: ( $self->{Attrs}{"content-type"} !~ m{^(text|message)/}i )
);
}
#------------------------------
=item data [DATA]
I<Instance method.>
Get/set the literal DATA of the message. The DATA may be
either a scalar, or a reference to an array of scalars (which
will simply be joined).
I<Warning:> setting the data causes the "content-length" attribute
to be recomputed (possibly to nothing).
=cut
sub data {
my $self = shift;
if (@_) {
$self->{Data} = ( ( ref( $_[0] ) eq 'ARRAY' ) ? join( '', @{ $_[0] } ) : $_[0] );
$self->get_length;
}
$self->{Data};
}
#------------------------------
=item fh [FILEHANDLE]
I<Instance method.>
Get/set the FILEHANDLE which contains the message data.
Takes a filehandle as an input and stores it in the object.
This routine is similar to path(); one important difference is that
no attempt is made to set the content length.
=cut
sub fh {
my $self = shift;
$self->{FH} = shift if @_;
$self->{FH};
}
#------------------------------
=item path [PATH]
I<Instance method.>
Get/set the PATH to the message data.
I<Warning:> setting the path recomputes any existing "content-length" field,
and re-sets the "filename" (to the last element of the path if it
looks like a simple path, and to nothing if not).
=cut
sub path {
my $self = shift;
if (@_) {
### Set the path, and invalidate the content length:
$self->{Path} = shift;
### Re-set filename, extracting it from path if possible:
my $filename;
if ( $self->{Path} and ( $self->{Path} !~ /\|$/ ) ) { ### non-shell path:
( $filename = $self->{Path} ) =~ s/^<//;
### Consult File::Basename, maybe:
if ($HaveFileBasename) {
$filename = File::Basename::basename($filename);
} else {
($filename) = ( $filename =~ m{([^\/] )\Z} );
}
}
$self->filename($filename);
### Reset the length:
$self->get_length;
}
$self->{Path};
}
#------------------------------
=item resetfh [FILEHANDLE]
I<Instance method.>
Set the current position of the filehandle back to the beginning.
Only applies if you used "FH" in build() or attach() for this message.
Returns false if unable to reset the filehandle (since not all filehandles
are seekable).
=cut
#----
# Miko's note: With the Data and Path, the same data could theoretically
# be reused. However, file handles need to be reset to be reused,
# so I added this routine.
#
# Eryq reply: beware... not all filehandles are seekable (think about STDIN)!
sub resetfh {
my $self = shift;
seek( $self->{FH}, 0, 0 );
}
#------------------------------
=item read_now
I<Instance method.>
Forces data from the path/filehandle (as specified by C<build()>)
to be read into core immediately, just as though you had given it
literally with the C<Data> keyword.
Note that the in-core data will always be used if available.
Be aware that everything is slurped into a giant scalar: you may not want
to use this if sending tar files! The benefit of I<not> reading in the data
is that very large files can be handled by this module if left on disk
until the message is output via C<print()> or C<print_body()>.
=cut
sub read_now {
my $self = shift;
local $/ = undef;
if ( $self->{FH} ) { ### data from a filehandle:
my $chunk;
my @chunks;
CORE::binmode( $self->{FH} ) if $self->binmode;
while ( read( $self->{FH}, $chunk, 1024 ) ) {
push @chunks, $chunk;
}
$self->{Data} = join '', @chunks;
} elsif ( $self->{Path} ) { ### data from a path:
open SLURP, $self->{Path} or Carp::croak "open $self->{Path}: $!\n";
CORE::binmode(SLURP) if $self->binmode;
$self->{Data} = <SLURP>; ### sssssssssssssslurp...
close SLURP; ### ...aaaaaaaaahhh!
}
}
#------------------------------
=item sign PARAMHASH
I<Instance method.>
Sign the message. This forces the message to be read into core,
after which the signature is appended to it.
=over 4
=item Data
As in C<build()>: the literal signature data.
Can be either a scalar or a ref to an array of scalars.
=item Path
As in C<build()>: the path to the file.
=back
If no arguments are given, the default is:
Path => "$ENV{HOME}/.signature"
The content-length is recomputed.
=cut
sub sign {
my $self = shift;
my %params = @_;
### Default:
@_ or $params{Path} = "$ENV{HOME}/.signature";
### Force message in-core:
defined( $self->{Data} ) or $self->read_now;
### Load signature:
my $sig;
if ( !defined( $sig = $params{Data} ) ) { ### not given explicitly:
local $/ = undef;
open SIG, $params{Path} or Carp::croak "open sig $params{Path}: $!\n";
$sig = <SIG>; ### sssssssssssssslurp...
close SIG; ### ...aaaaaaaaahhh!
}
$sig = join( '', @$sig ) if ( ref($sig) and ( ref($sig) eq 'ARRAY' ) );
### Append, following Internet conventions:
$self->{Data} .= "\n-- \n$sig";
### Re-compute length:
$self->get_length;
1;
}
#------------------------------
#
# =item suggest_encoding CONTENTTYPE
#
# I<Class/instance method.>
# Based on the CONTENTTYPE, return a good suggested encoding.
# C<text> and C<message> types have their bodies scanned line-by-line
# for 8-bit characters and long lines; lack of either means that the
# message is 7bit-ok. Other types are chosen independent of their body:
#
# Major type: 7bit ok? Suggested encoding:
# ------------------------------------------------------------
# text yes 7bit
# no quoted-printable
# unknown binary
#
# message yes 7bit
# no binary
# unknown binary
#
# multipart n/a binary (in case some parts are not ok)
#
# (other) n/a base64
#
#=cut
sub suggest_encoding {
my ( $self, $ctype ) = @_;
$ctype = lc($ctype);
### Consult MIME::Types, maybe:
if ($HaveMimeTypes) {
### Mappings contain [suffix,mimetype,encoding]
my @mappings = MIME::Types::by_mediatype($ctype);
if ( scalar(@mappings) ) {
### Just pick the first one:
my ( $suffix, $mimetype, $encoding ) = @{ $mappings[0] };
if ( $encoding
&& $encoding =~ /^(base64|binary|[78]bit|quoted-printable)$/i )
{
return lc($encoding); ### sanity check
}
}
}
### If we got here, then MIME::Types was no help.
### Extract major type:
my ($type) = split '/', $ctype;
if ( ( $type eq 'text' ) || ( $type eq 'message' ) ) { ### scan message body?
return 'binary';
} else {
return ( $type eq 'multipart' ) ? 'binary' : 'base64';
}
}
#------------------------------
#
# =item suggest_type PATH
#
# I<Class/instance method.>
# Suggest the content-type for this attached path.
# We always fall back to "application/octet-stream" if no good guess
# can be made, so don't use this if you don't mean it!
#
sub suggest_type {
my ( $self, $path ) = @_;
### If there's no path, bail:
$path or return 'application/octet-stream';
### Consult MIME::Types, maybe:
if ($HaveMimeTypes) {
# Mappings contain [mimetype,encoding]:
my ( $mimetype, $encoding ) = MIME::Types::by_suffix($path);
return $mimetype if ( $mimetype && $mimetype =~ /^\S \/\S $/ ); ### sanity check
}
### If we got here, then MIME::Types was no help.
### The correct thing to fall back to is the most-generic content type:
return 'application/octet-stream';
}
#------------------------------
=item verify_data
I<Instance method.>
Verify that all "paths" to attached data exist, recursively.
It might be a good idea for you to do this before a print(), to
prevent accidental partial output if a file might be missing.
Raises exception if any path is not readable.
=cut
sub verify_data {
my $self = shift;
### Verify self:
my $path = $self->{Path};
if ( $path and ( $path !~ /\|$/ ) ) { ### non-shell path:
$path =~ s/^<//;
( -r $path ) or die "$path: not readable\n";
}
### Verify parts:
foreach my $part ( @{ $self->{Parts} } ) { $part->verify_data }
1;
}
=back
=cut
#==============================
#==============================
=head2 Output
=over 4
=cut
#------------------------------
=item print [OUTHANDLE]
I<Instance method.>
Print the message to the given output handle, or to the currently-selected
filehandle if none was given.
All OUTHANDLE has to be is a filehandle (possibly a glob ref), or
any object that responds to a print() message.
=cut
sub print {
my ( $self, $out ) = @_;
### Coerce into a printable output handle:
$out = MIME::Lite::IO_Handle->wrap($out);
### Output head, separator, and body:
$self->verify_data if $AUTO_VERIFY; ### prevents missing parts!
$out->print( $self->header_as_string );
$out->print( "\n" );
$self->print_body($out);
}
#------------------------------
#
# print_for_smtp
#
# Instance method, private.
# Print, but filter out the topmost "Bcc" field.
# This is because qmail apparently doesn't do this for us!
#
sub print_for_smtp {
my ( $self, $out ) = @_;
### Coerce into a printable output handle:
$out = MIME::Lite::IO_Handle->wrap($out);
### Create a safe head:
my @fields = grep { $_->[0] ne 'bcc' } @{ $self->fields };
my $header = $self->fields_as_string( \@fields );
### Output head, separator, and body:
$out->print( $header );
$out->print( "\n" );
$self->print_body( $out, '1' );
}
#------------------------------
=item print_body [OUTHANDLE] [IS_SMTP]
I<Instance method.>
Print the body of a message to the given output handle, or to
the currently-selected filehandle if none was given.
All OUTHANDLE has to be is a filehandle (possibly a glob ref), or
any object that responds to a print() message.
B<Fatal exception> raised if unable to open any of the input files,
or if a part contains no data, or if an unsupported encoding is
encountered.
IS_SMPT is a special option to handle SMTP mails a little more
intelligently than other send mechanisms may require. Specifically this
ensures that the last byte sent is NOT '\n' (octal \012) if the last two
bytes are not '\r\n' (\015\012) as this will cause some SMTP servers to
hang.
=cut
sub print_body {
my ( $self, $out, $is_smtp ) = @_;
my $attrs = $self->{Attrs};
my $sub_attrs = $self->{SubAttrs};
### Coerce into a printable output handle:
$out = MIME::Lite::IO_Handle->wrap($out);
### Output either the body or the parts.
### Notice that we key off of the content-type! We expect fewer
### accidents that way, since the syntax will always match the MIME type.
my $type = $attrs->{'content-type'};
if ( $type =~ m{^multipart/}i ) {
my $boundary = $sub_attrs->{'content-type'}{'boundary'};
### Preamble:
$out->print( defined( $self->{Preamble} )
? $self->{Preamble}
: "This is a multi-part message in MIME format.\n"
);
### Parts:
my $part;
foreach $part ( @{ $self->{Parts} } ) {
$out->print("\n--$boundary\n");
$part->print($out);
}
### Epilogue:
$out->print("\n--$boundary--\n");
} elsif ( $type =~ m{^message/} ) {
my @parts = @{ $self->{Parts} };
### It's a toss-up; try both data and parts:
if ( @parts == 0 ) { $self->print_simple_body( $out, $is_smtp ) }
elsif ( @parts == 1 ) { $parts[0]->print($out) }
else { Carp::croak "can't handle message with >1 part\n"; }
} else {
$self->print_simple_body( $out, $is_smtp );
}
1;
}
#------------------------------
#
# print_simple_body [OUTHANDLE]
#
# I<Instance method, private.>
# Print the body of a simple singlepart message to the given
# output handle, or to the currently-selected filehandle if none
# was given.
#
# Note that if you want to print "the portion after
# the header", you don't want this method: you want
# L<print_body()|/print_body>.
#
# All OUTHANDLE has to be is a filehandle (possibly a glob ref), or
# any object that responds to a print() message.
#
# B<Fatal exception> raised if unable to open any of the input files,
# or if a part contains no data, or if an unsupported encoding is
# encountered.
#
sub print_simple_body {
my ( $self, $out, $is_smtp ) = @_;
my $attrs = $self->{Attrs};
### Coerce into a printable output handle:
$out = MIME::Lite::IO_Handle->wrap($out);
### Get content-transfer-encoding:
my $encoding = uc( $attrs->{'content-transfer-encoding'} );
warn "M::L >>> Encoding using $encoding, is_smtp=" . ( $is_smtp || 0 ) . "\n"
if $MIME::Lite::DEBUG;
### Notice that we don't just attempt to slurp the data in from a file:
### by processing files piecemeal, we still enable ourselves to prepare
### very large MIME messages...
### Is the data in-core? If so, blit it out...
if ( defined( $self->{Data} ) ) {
DATA:
{
local $_ = $encoding;
/^BINARY$/ and do {
$is_smtp and $self->{Data} =~ s/(?!\r)\n\z/\r/;
$out->print( $self->{Data} );
last DATA;
};
/^8BIT$/ and do {
$out->print( encode_8bit( $self->{Data} ) );
last DATA;
};
/^7BIT$/ and do {
$out->print( encode_7bit( $self->{Data} ) );
last DATA;
};
/^QUOTED-PRINTABLE$/ and do {
### UNTAINT since m//mg on tainted data loops forever:
my ($untainted) = ( $self->{Data} =~ m/\A(.*)\Z/s );
### Encode it line by line:
while ( $untainted =~ m{^(.*[\r\n]*)}smg ) {
### have to do it line by line...
my $line = $1; # copy to avoid weird bug; rt 39334
$out->print( encode_qp($line) );
}
last DATA;
};
/^BASE64/ and do {
$out->print( encode_base64( $self->{Data} ) );
last DATA;
};
Carp::croak "unsupported encoding: `$_'\n";
}
}
### Else, is the data in a file? If so, output piecemeal...
### Miko's note: this routine pretty much works the same with a path
### or a filehandle. the only difference in behaviour is that it does
### not attempt to open anything if it already has a filehandle
elsif ( defined( $self->{Path} ) || defined( $self->{FH} ) ) {
no strict 'refs'; ### in case FH is not an object
my $DATA;
### Open file if necessary:
if ( defined( $self->{Path} ) ) {
$DATA = new FileHandle || Carp::croak "can't get new filehandle\n";
$DATA->open("$self->{Path}")
or Carp::croak "open $self->{Path}: $!\n";
} else {
$DATA = $self->{FH};
}
CORE::binmode($DATA) if $self->binmode;
### Encode piece by piece:
PATH:
{
local $_ = $encoding;
/^BINARY$/ and do {
my $last = "";
while ( read( $DATA, $_, 2048 ) ) {
$out->print($last) if length $last;
$last = $_;
}
if ( length $last ) {
$is_smtp and $last =~ s/(?!\r)\n\z/\r/;
$out->print($last);
}
last PATH;
};
/^8BIT$/ and do {
$out->print( encode_8bit($_) ) while (<$DATA>);
last PATH;
};
/^7BIT$/ and do {
$out->print( encode_7bit($_) ) while (<$DATA>);
last PATH;
};
/^QUOTED-PRINTABLE$/ and do {
$out->print( encode_qp($_) ) while (<$DATA>);
last PATH;
};
/^BASE64$/ and do {
$out->print( encode_base64($_) ) while ( read( $DATA, $_, 45 ) );
last PATH;
};
Carp::croak "unsupported encoding: `$_'\n";
}
### Close file:
close $DATA if defined( $self->{Path} );
}
else {
Carp::croak "no data in this part\n";
}
1;
}
#------------------------------
=item print_header [OUTHANDLE]
I<Instance method.>
Print the header of the message to the given output handle,
or to the currently-selected filehandle if none was given.
All OUTHANDLE has to be is a filehandle (possibly a glob ref), or
any object that responds to a print() message.
=cut
sub print_header {
my ( $self, $out ) = @_;
### Coerce into a printable output handle:
$out = MIME::Lite::IO_Handle->wrap($out);
### Output the header:
$out->print( $self->header_as_string );
1;
}
#------------------------------
=item as_string
I<Instance method.>
Return the entire message as a string, with a header and an encoded body.
=cut
sub as_string {
my $self = shift;
my $buf = "";
my $io = ( wrap MIME::Lite::IO_Scalar \$buf);
$self->print($io);
return $buf;
}
*stringify = \&as_string; ### backwards compatibility
*stringify = \&as_string; ### ...twice to avoid warnings :)
#------------------------------
=item body_as_string
I<Instance method.>
Return the encoded body as a string.
This is the portion after the header and the blank line.
I<Note:> actually prepares the body by "printing" to a scalar.
Proof that you can hand the C<print*()> methods any blessed object
that responds to a C<print()> message.
=cut
sub body_as_string {
my $self = shift;
my $buf = "";
my $io = ( wrap MIME::Lite::IO_Scalar \$buf);
$self->print_body($io);
return $buf;
}
*stringify_body = \&body_as_string; ### backwards compatibility
*stringify_body = \&body_as_string; ### ...twice to avoid warnings :)
#------------------------------
#
# fields_as_string FIELDS
#
# PRIVATE! Return a stringified version of the given header
# fields, where FIELDS is an arrayref like that returned by fields().
#
sub fields_as_string {
my ( $self, $fields ) = @_;
my $out = "";
foreach (@$fields) {
my ( $tag, $value ) = @$_;
next if ( $value eq '' ); ### skip empties
$tag =~ s/\b([a-z])/uc($1)/ge; ### make pretty
$tag =~ s/^mime-/MIME-/i; ### even prettier
$out .= "$tag: $value\n";
}
return $out;
}
#------------------------------
=item header_as_string
I<Instance method.>
Return the header as a string.
=cut
sub header_as_string {
my $self = shift;
$self->fields_as_string( $self->fields );
}
*stringify_header = \&header_as_string; ### backwards compatibility
*stringify_header = \&header_as_string; ### ...twice to avoid warnings :)
=back
=cut
#==============================
#==============================
=head2 Sending
=over 4
=cut
#------------------------------
=item send
=item send HOW, HOWARGS...
I<Class/instance method.>
This is the principal method for sending mail, and for configuring
how mail will be sent.
I<As a class method> with a HOW argument and optional HOWARGS, it sets
the default sending mechanism that the no-argument instance method
will use. The HOW is a facility name (B<see below>),
and the HOWARGS is interpreted by the facility.
The class method returns the previous HOW and HOWARGS as an array.
MIME::Lite->send('sendmail', "d:\\programs\\sendmail.exe");
...
$msg = MIME::Lite->new(...);
$msg->send;
I<As an instance method with arguments>
(a HOW argument and optional HOWARGS), sends the message in the
requested manner; e.g.:
$msg->send('sendmail', "d:\\programs\\sendmail.exe");
I<As an instance method with no arguments,> sends the
message by the default mechanism set up by the class method.
Returns whatever the mail-handling routine returns: this
should be true on success, false/exception on error:
$msg = MIME::Lite->new(From=>...);
$msg->send || die "you DON'T have mail!";
On Unix systems (or rather non-Win32 systems), the default
setting is equivalent to:
MIME::Lite->send("sendmail", "/usr/lib/sendmail -t -oi -oem");
On Win32 systems the default setting is equivalent to:
MIME::Lite->send("smtp");
The assumption is that on Win32 your site/lib/Net/libnet.cfg
file will be preconfigured to use the appropriate SMTP
server. See below for configuring for authentication.
There are three facilities:
=over 4
=item "sendmail", ARGS...
Send a message by piping it into the "sendmail" command.
Uses the L<send_by_sendmail()|/send_by_sendmail> method, giving it the ARGS.
This usage implements (and deprecates) the C<sendmail()> method.
=item "smtp", [HOSTNAME, [NAMEDPARMS] ]
Send a message by SMTP, using optional HOSTNAME as SMTP-sending host.
L<Net::SMTP> will be required. Uses the L<send_by_smtp()|/send_by_smtp>
method. Any additional arguments passed in will also be passed through to
send_by_smtp. This is useful for things like mail servers requiring
authentication where you can say something like the following
MIME::Lite->send('smtp', $host, AuthUser=>$user, AuthPass=>$pass);
which will configure things so future uses of
$msg->send();
do the right thing.
=item "sub", \&SUBREF, ARGS...
Sends a message MSG by invoking the subroutine SUBREF of your choosing,
with MSG as the first argument, and ARGS following.
=back
I<For example:> let's say you're on an OS which lacks the usual Unix
"sendmail" facility, but you've installed something a lot like it, and
you need to configure your Perl script to use this "sendmail.exe" program.
Do this following in your script's setup:
MIME::Lite->send('sendmail', "d:\\programs\\sendmail.exe");
Then, whenever you need to send a message $msg, just say:
$msg->send;
That's it. Now, if you ever move your script to a Unix box, all you
need to do is change that line in the setup and you're done.
All of your $msg-E<gt>send invocations will work as expected.
After sending, the method last_send_successful() can be used to determine
if the send was successful or not.
=cut
sub send {
my $self = shift;
my $meth = shift;
if ( ref($self) ) { ### instance method:
my ( $method, @args );
if (@_) { ### args; use them just this once
$method = 'send_by_' . $meth;
@args = @_;
} else { ### no args; use defaults
$method = "send_by_$Sender";
@args = @{ $SenderArgs{$Sender} || [] };
}
$self->verify_data if $AUTO_VERIFY; ### prevents missing parts!
Carp::croak "Unknown send method '$meth'" unless $self->can($method);
return $self->$method(@args);
} else { ### class method:
if (@_) {
my @old = ( $Sender, @{ $SenderArgs{$Sender} } );
$Sender = $meth;
$SenderArgs{$Sender} = [@_]; ### remaining args
return @old;
} else {
Carp::croak "class method send must have HOW... arguments\n";
}
}
}
#------------------------------
=item send_by_sendmail SENDMAILCMD
=item send_by_sendmail PARAM=>VALUE, ARRAY, HASH...
I<Instance method.>
Send message via an external "sendmail" program
(this will probably only work out-of-the-box on Unix systems).
Returns true on success, false or exception on error.
You can specify the program and all its arguments by giving a single
string, SENDMAILCMD. Nothing fancy is done; the message is simply
piped in.
However, if your needs are a little more advanced, you can specify
zero or more of the following PARAM/VALUE pairs (or a reference to hash
or array of such arguments as well as any combination thereof); a
Unix-style, taint-safe "sendmail" command will be constructed for you:
=over 4
=item Sendmail
Full path to the program to use.
Default is "/usr/lib/sendmail".
=item BaseArgs
Ref to the basic array of arguments we start with.
Default is C<["-t", "-oi", "-oem"]>.
=item SetSender
Unless this is I<explicitly> given as false, we attempt to automatically
set the C<-f> argument to the first address that can be extracted from
the "From:" field of the message (if there is one).
I<What is the -f, and why do we use it?>
Suppose we did I<not> use C<-f>, and you gave an explicit "From:"
field in your message: in this case, the sendmail "envelope" would
indicate the I<real> user your process was running under, as a way
of preventing mail forgery. Using the C<-f> switch causes the sender
to be set in the envelope as well.
I<So when would I NOT want to use it?>
If sendmail doesn't regard you as a "trusted" user, it will permit
the C<-f> but also add an "X-Authentication-Warning" header to the message
to indicate a forged envelope. To avoid this, you can either
(1) have SetSender be false, or
(2) make yourself a trusted user by adding a C<T> configuration
command to your I<sendmail.cf> file
(e.g.: C<Teryq> if the script is running as user "eryq").
=item FromSender
If defined, this is identical to setting SetSender to true,
except that instead of looking at the "From:" field we use
the address given by this option.
Thus:
FromSender => '[email protected]'
=back
After sending, the method last_send_successful() can be used to determine
if the send was successful or not.
=cut
sub _unfold_stupid_params {
my $self = shift;
my %p;
STUPID_PARAM: for (my $i = 0; $i < @_; $i ) { ## no critic Loop
my $item = $_[$i];
if (not ref $item) {
$p{ $item } = $_[ $i ];
} elsif (UNIVERSAL::isa($item, 'HASH')) {
$p{ $_ } = $item->{ $_ } for keys %$item;
} elsif (UNIVERSAL::isa($item, 'ARRAY')) {
for (my $j = 0; $j < @$item; $j = 2) {
$p{ $item->[ $j ] } = $item->[ $j 1 ];
}
}
}
return %p;
}
sub send_by_sendmail {
my $self = shift;
my $return;
if ( @_ == 1 and !ref $_[0] ) {
### Use the given command...
my $sendmailcmd = shift @_;
Carp::croak "No sendmail command available" unless $sendmailcmd;
### Do it:
local *SENDMAIL;
open SENDMAIL, "|$sendmailcmd" or Carp::croak "open |$sendmailcmd: $!\n";
$self->print( \*SENDMAIL );
close SENDMAIL;
$return = ( ( $? >> 8 ) ? undef: 1 );
} else { ### Build the command...
my %p = $self->_unfold_stupid_params(@_);
$p{Sendmail} = $SENDMAIL unless defined $p{Sendmail};
### Start with the command and basic args:
my @cmd = ( $p{Sendmail}, @{ $p{BaseArgs} || [ '-t', '-oi', '-oem' ] } );
# SetSender default is true
$p{SetSender} = 1 unless defined $p{SetSender};
### See if we are forcibly setting the sender:
$p{SetSender} ||= defined( $p{FromSender} );
### Add the -f argument, unless we're explicitly told NOT to:
if ( $p{SetSender} ) {
my $from = $p{FromSender} || ( $self->get('From') )[0];
if ($from) {
my ($from_addr) = extract_only_addrs($from);
push @cmd, "-f$from_addr" if $from_addr;
}
}
### Open the command in a taint-safe fashion:
my $pid = open SENDMAIL, "|-";
defined($pid) or die "open of pipe failed: $!\n";
if ( !$pid ) { ### child
exec(@cmd) or die "can't exec $p{Sendmail}: $!\n";
### NOTREACHED
} else { ### parent
$self->print( \*SENDMAIL );
close SENDMAIL || die "error closing $p{Sendmail}: $! (exit $?)\n";
$return = 1;
}
}
return $self->{last_send_successful} = $return;
}
#------------------------------
=item send_by_smtp HOST, ARGS...
=item send_by_smtp REF, HOST, ARGS
I<Instance method.>
Send message via SMTP, using Net::SMTP -- which will be required for this
feature.
HOST is the name of SMTP server to connect to, or undef to have
L<Net::SMTP|Net::SMTP> use the defaults in Libnet.cfg.
ARGS are a list of key value pairs which may be selected from the list
below. Many of these are just passed through to specific
L<Net::SMTP|Net::SMTP> commands and you should review that module for
details.
Please see L<Good-vs-bad email addresses with send_by_smtp()|/Good-vs-bad email addresses with send_by_smtp()>
=over 4
=item Hello
=item LocalAddr
=item LocalPort
=item Timeout
=item Port
=item ExactAddresses
=item Debug
See L<Net::SMTP::new()|Net::SMTP/"mail"> for details.
=item Size
=item Return
=item Bits
=item Transaction
=item Envelope
See L<Net::SMTP::mail()|Net::SMTP/mail> for details.
=item SkipBad
If true doesn't throw an error when multiple email addresses are provided
and some are not valid. See L<Net::SMTP::recipient()|Net::SMTP/recipient>
for details.
=item AuthUser
Authenticate with L<Net::SMTP::auth()|Net::SMTP/auth> using this username.
=item AuthPass
Authenticate with L<Net::SMTP::auth()|Net::SMTP/auth> using this password.
=item NoAuth
Normally if AuthUser and AuthPass are defined MIME::Lite will attempt to
use them with the L<Net::SMTP::auth()|Net::SMTP/auth> command to
authenticate the connection, however if this value is true then no
authentication occurs.
=item To
Sets the addresses to send to. Can be a string or a reference to an
array of strings. Normally this is extracted from the To: (and Cc: and
Bcc: fields if $AUTO_CC is true).
This value overrides that.
=item From
Sets the email address to send from. Normally this value is extracted
from the Return-Path: or From: field of the mail itself (in that order).
This value overrides that.
=back
I<Returns:>
True on success, croaks with an error message on failure.
After sending, the method last_send_successful() can be used to determine
if the send was successful or not.
=cut
# Derived from work by Andrew McRae. Version 0.2 anm 09Sep97
# Copyright 1997 Optimation New Zealand Ltd.
# May be modified/redistributed under the same terms as Perl.
# external opts
my @_mail_opts = qw( Size Return Bits Transaction Envelope );
my @_recip_opts = qw( SkipBad Notify );
my @_net_smtp_opts = qw( Hello LocalAddr LocalPort Timeout
AuthUser AuthPass SSL
Port ExactAddresses Debug );
# internal: qw( NoAuth AuthUser AuthPass To From Host);
sub __opts {
my $args=shift;
return map { exists $args->{$_} ? ( $_ => $args->{$_} ) : () } @_;
}
sub send_by_smtp {
require Net::SMTP;
my ($self,$hostname,%args) = @_;
# We may need the "From:" and "To:" headers to pass to the
# SMTP mailer also.
$self->{last_send_successful}=0;
my @hdr_to = extract_only_addrs( scalar $self->get('To') );
if ($AUTO_CC) {
foreach my $field (qw(Cc Bcc)) {
push @hdr_to, extract_only_addrs($_) for $self->get($field);
}
}
Carp::croak "send_by_smtp: nobody to send to for host '$hostname'?!\n"
unless @hdr_to;
$args{To} ||= \@hdr_to;
$args{From} ||= extract_only_addrs( scalar $self->get('Return-Path') );
$args{From} ||= extract_only_addrs( scalar $self->get('From') ) ;
# Create SMTP client.
# MIME::Lite::SMTP is just a wrapper giving a print method
# to the SMTP object.
my %opts = __opts(\%args, @_net_smtp_opts);
my $smtp = MIME::Lite::SMTP->new( $hostname, %opts )
or Carp::croak "SMTP Failed to connect to mail server: $!\n";
# Possibly authenticate
if ( defined $args{AuthUser} and defined $args{AuthPass}
and !$args{NoAuth} )
{
if ($smtp->supports('AUTH',500,["Command unknown: 'AUTH'"])) {
$smtp->auth( $args{AuthUser}, $args{AuthPass} )
or die "SMTP auth() command failed: $!\n"
. $smtp->message . "\n";
} else {
die "SMTP auth() command not supported on $hostname\n";
}
}
# Send the mail command
%opts = __opts( \%args, @_mail_opts);
$smtp->mail( $args{From}, %opts ? \%opts : () )
or die "SMTP mail() command failed: $!\n"
. $smtp->message . "\n";
# Send the recipients command
%opts = __opts( \%args, @_recip_opts);
$smtp->recipient( @{ $args{To} }, %opts ? \%opts : () )
or die "SMTP recipient() command failed: $!\n"
. $smtp->message . "\n";
# Send the data
$smtp->data()
or die "SMTP data() command failed: $!\n"
. $smtp->message . "\n";
$self->print_for_smtp($smtp);
# Finish the mail
$smtp->dataend()
or Carp::croak "Net::CMD (Net::SMTP) DATAEND command failed.\n"
. "Last server message was:"
. $smtp->message
. "This probably represents a problem with newline encoding ";
# terminate the session
$smtp->quit;
return $self->{last_send_successful} = 1;
}
=item send_by_testfile FILENAME
I<Instance method.>
Print message to a file (namely FILENAME), which will default to
mailer.testfile
If file exists, message will be appended.
=cut
sub send_by_testfile {
my $self = shift;
### Use the default filename...
my $filename = 'mailer.testfile';
if ( @_ == 1 and !ref $_[0] ) {
### Use the given filename if given...
$filename = shift @_;
Carp::croak "no filename given to send_by_testfile" unless $filename;
}
### Do it:
local *FILE;
open FILE, ">> $filename" or Carp::croak "open $filename: $!\n";
$self->print( \*FILE );
close FILE;
my $return = ( ( $? >> 8 ) ? undef: 1 );
return $self->{last_send_successful} = $return;
}
=item last_send_successful
This method will return TRUE if the last send() or send_by_XXX() method call was
successful. It will return defined but false if it was not successful, and undefined
if the object had not been used to send yet.
=cut
sub last_send_successful {
my $self = shift;
return $self->{last_send_successful};
}
### Provided by Andrew McRae. Version 0.2 anm 09Sep97
### Copyright 1997 Optimation New Zealand Ltd.
### May be modified/redistributed under the same terms as Perl.
### Aditional changes by Yves.
### Until 3.01_03 this was send_by_smtp()
sub send_by_smtp_simple {
my ( $self, @args ) = @_;
$self->{last_send_successful} = 0;
### We need the "From:" and "To:" headers to pass to the SMTP mailer:
my $hdr = $self->fields();
my $from_header = $self->get('From');
my ($from) = extract_only_addrs($from_header);
warn "M::L>>> $from_header => $from" if $MIME::Lite::DEBUG;
my $to = $self->get('To');
### Sanity check:
defined($to)
or Carp::croak "send_by_smtp: missing 'To:' address\n";
### Get the destinations as a simple array of addresses:
my @to_all = extract_only_addrs($to);
if ($AUTO_CC) {
foreach my $field (qw(Cc Bcc)) {
my $value = $self->get($field);
push @to_all, extract_only_addrs($value)
if defined($value);
}
}
### Create SMTP client:
require Net::SMTP;
my $smtp = MIME::Lite::SMTP->new(@args)
or Carp::croak("Failed to connect to mail server: $!\n");
$smtp->mail($from)
or Carp::croak( "SMTP MAIL command failed: $!\n" . $smtp->message . "\n" );
$smtp->to(@to_all)
or Carp::croak( "SMTP RCPT command failed: $!\n" . $smtp->message . "\n" );
$smtp->data()
or Carp::croak( "SMTP DATA command failed: $!\n" . $smtp->message . "\n" );
### MIME::Lite can print() to anything with a print() method:
$self->print_for_smtp($smtp);
$smtp->dataend()
or Carp::croak( "Net::CMD (Net::SMTP) DATAEND command failed.\n"
. "Last server message was:"
. $smtp->message
. "This probably represents a problem with newline encoding " );
$smtp->quit;
$self->{last_send_successful} = 1;
1;
}
#------------------------------
#
# send_by_sub [\&SUBREF, [ARGS...]]
#
# I<Instance method, private.>
# Send the message via an anonymous subroutine.
#
sub send_by_sub {
my ( $self, $subref, @args ) = @_;
$self->{last_send_successful} = &$subref( $self, @args );
}
#------------------------------
=item sendmail COMMAND...
I<Class method, DEPRECATED.>
Declare the sender to be "sendmail", and set up the "sendmail" command.
I<You should use send() instead.>
=cut
sub sendmail {
my $self = shift;
$self->send( 'sendmail', join( ' ', @_ ) );
}
=back
=cut
#==============================
#==============================
=head2 Miscellaneous
=over 4
=cut
#------------------------------
=item quiet ONOFF
I<Class method.>
Suppress/unsuppress all warnings coming from this module.
MIME::Lite->quiet(1); ### I know what I'm doing
I recommend that you include that comment as well. And while
you type it, say it out loud: if it doesn't feel right, then maybe
you should reconsider the whole line. C<;-)>
=cut
sub quiet {
my $class = shift;
$QUIET = shift if @_;
$QUIET;
}
=back
=cut
#============================================================
package MIME::Lite::SMTP;
#============================================================
# This class just adds a print() method to Net::SMTP.
# Notice that we don't use/require it until it's needed!
use strict;
use vars qw( @ISA );
@ISA = qw(Net::SMTP);
# some of the below is borrowed from Data::Dumper
my %esc = ( "\a" => "\\a",
"\b" => "\\b",
"\t" => "\\t",
"\n" => "\\n",
"\f" => "\\f",
"\r" => "\\r",
"\e" => "\\e",
);
sub _hexify {
local $_ = shift;
my @split = m/(.{1,16})/gs;
foreach my $split (@split) {
( my $txt = $split ) =~ s/([\a\b\t\n\f\r\e])/$esc{$1}/sg;
$split =~ s/(.)/sprintf("X ",ord($1))/sge;
print STDERR "M::L >>> $split : $txt\n";
}
}
sub print {
my $smtp = shift;
$MIME::Lite::DEBUG and _hexify( join( "", @_ ) );
$smtp->datasend(@_)
or Carp::croak( "Net::CMD (Net::SMTP) DATASEND command failed.\n"
. "Last server message was:"
. $smtp->message
. "This probably represents a problem with newline encoding " );
}
#============================================================
package MIME::Lite::IO_Handle;
#============================================================
### Wrap a non-object filehandle inside a blessed, printable interface:
### Does nothing if the given $fh is already a blessed object.
sub wrap {
my ( $class, $fh ) = @_;
no strict 'refs';
### Get default, if necessary:
$fh or $fh = select; ### no filehandle means selected one
ref($fh) or $fh = \*$fh; ### scalar becomes a globref
### Stop right away if already a printable object:
return $fh if ( ref($fh) and ( ref($fh) ne 'GLOB' ) );
### Get and return a printable interface:
bless \$fh, $class; ### wrap it in a printable interface
}
### Print:
sub print {
my $self = shift;
print {$$self} @_;
}
#============================================================
package MIME::Lite::IO_Scalar;
#============================================================
### Wrap a scalar inside a blessed, printable interface:
sub wrap {
my ( $class, $scalarref ) = @_;
defined($scalarref) or $scalarref = \"";
bless $scalarref, $class;
}
### Print:
sub print {
${$_[0]} .= join( '', @_[1..$#_] );
1;
}
#============================================================
package MIME::Lite::IO_ScalarArray;
#============================================================
### Wrap an array inside a blessed, printable interface:
sub wrap {
my ( $class, $arrayref ) = @_;
defined($arrayref) or $arrayref = [];
bless $arrayref, $class;
}
### Print:
sub print {
my $self = shift;
push @$self, @_;
1;
}
1;
__END__
#============================================================
=head1 NOTES
=head2 How do I prevent "Content" headers from showing up in my mail reader?
Apparently, some people are using mail readers which display the MIME
headers like "Content-disposition", and they want MIME::Lite not
to generate them "because they look ugly".
Sigh.
Y'know, kids, those headers aren't just there for cosmetic purposes.
They help ensure that the message is I<understood> correctly by mail
readers. But okay, you asked for it, you got it...
here's how you can suppress the standard MIME headers.
Before you send the message, do this:
$msg->scrub;
You can scrub() any part of a multipart message independently;
just be aware that it works recursively. Before you scrub,
note the rules that I follow:
=over 4
=item Content-type
You can safely scrub the "content-type" attribute if, and only if,
the part is of type "text/plain" with charset "us-ascii".
=item Content-transfer-encoding
You can safely scrub the "content-transfer-encoding" attribute
if, and only if, the part uses "7bit", "8bit", or "binary" encoding.
You are far better off doing this if your lines are under 1000
characters. Generally, that means you I<can> scrub it for plain
text, and you can I<not> scrub this for images, etc.
=item Content-disposition
You can safely scrub the "content-disposition" attribute
if you trust the mail reader to do the right thing when it decides
whether to show an attachment inline or as a link. Be aware
that scrubbing both the content-disposition and the content-type
means that there is no way to "recommend" a filename for the attachment!
B<Note:> there are reports of brain-dead MUAs out there that
do the wrong thing if you I<provide> the content-disposition.
If your attachments keep showing up inline or vice-versa,
try scrubbing this attribute.
=item Content-length
You can always scrub "content-length" safely.
=back
=head2 How do I give my attachment a [different] recommended filename?
By using the Filename option (which is different from Path!):
$msg->attach(Type => "image/gif",
Path => "/here/is/the/real/file.GIF",
Filename => "logo.gif");
You should I<not> put path information in the Filename.
=head2 Working with UTF-8 and other character sets
All text that is added to your mail message should be properly encoded.
MIME::Lite doesn't do this for you. For instance, if you want to
send your mail in UTF-8, where C<$to>, C<$subject> and C<$text> have
these values:
=over
=item *
To: "RamE<oacute>n NuE<ntilde>ez E<lt>[email protected]<gt>"
=item *
Subject: "E<iexcl>AquE<iacute> estE<aacute>!"
=item *
Text: "E<iquest>Quieres ganar muchos E<euro>'s?"
=back
use MIME::Lite;
use Encode qw(encode encode_utf8 );
my $to = "Ram\363n Nu\361ez <foo\@bar.com>";
my $subject = "\241Aqu\355 est\341!";
my $text = "\277Quieres ganar muchos \x{20ac}'s?";
### Create a new message encoded in UTF-8:
my $msg = MIME::Lite->new(
From => '[email protected]',
To => encode( 'MIME-Header', $to ),
Subject => encode( 'MIME-Header', $subject ),
Data => encode_utf8($text)
);
$msg->attr( 'content-type' => 'text/plain; charset=utf-8' );
$msg->send;
B<Note:>
=over
=item *
The above example assumes that the values you want to encode are in
Perl's "internal" form, i.e. the strings contain decoded UTF-8
characters, not the bytes that represent those characters.
See L<perlunitut>, L<perluniintro>, L<perlunifaq> and L<Encode> for
more.
=item *
If, for the body of the email, you want to use a character set
other than UTF-8, then you should encode appropriately, and set the
correct C<content-type>, eg:
...
Data => encode('iso-8859-15',$text)
...
$msg->attr( 'content-type' => 'text/plain; charset=iso-8859-15' );
=item *
For the message headers, L<Encode::MIME::Header> only support UTF-8,
but most modern mail clients should be able to handle this. It is not
a problem to have your headers in a different encoding from the message
body.
=back
=head2 Benign limitations
This is "lite", after all...
=over 4
=item *
There's no parsing. Get MIME-tools if you need to parse MIME messages.
=item *
MIME::Lite messages are currently I<not> interchangeable with
either Mail::Internet or MIME::Entity objects. This is a completely
separate module.
=item *
A content-length field is only inserted if the encoding is binary,
the message is a singlepart, and all the document data is available
at C<build()> time by virtue of residing in a simple path, or in-core.
Since content-length is not a standard MIME field anyway (that's right, kids:
it's not in the MIME RFCs, it's an HTTP thing), this seems pretty fair.
=item *
MIME::Lite alone cannot help you lose weight. You must supplement
your use of MIME::Lite with a healthy diet and exercise.
=back
=head2 Cheap and easy mailing
I thought putting in a default "sendmail" invocation wasn't too bad an
idea, since a lot of Perlers are on UNIX systems. (As of version 3.02 this is
default only on Non-Win32 boxen. On Win32 boxen the default is to use SMTP and the
defaults specified in the site/lib/Net/libnet.cfg)
The out-of-the-box configuration is:
MIME::Lite->send('sendmail', "/usr/lib/sendmail -t -oi -oem");
By the way, these arguments to sendmail are:
-t Scan message for To:, Cc:, Bcc:, etc.
-oi Do NOT treat a single "." on a line as a message terminator.
As in, "-oi vey, it truncated my message... why?!"
-oem On error, mail back the message (I assume to the
appropriate address, given in the header).
When mail returns, circle is complete. Jai Guru Deva -oem.
Note that these are the same arguments you get if you configure to use
the smarter, taint-safe mailing:
MIME::Lite->send('sendmail');
If you get "X-Authentication-Warning" headers from this, you can forgo
diddling with the envelope by instead specifying:
MIME::Lite->send('sendmail', SetSender=>0);
And, if you're not on a Unix system, or if you'd just rather send mail
some other way, there's always SMTP, which these days probably requires
authentication so you probably need to say
MIME::Lite->send('smtp', "smtp.myisp.net",
AuthUser=>"YourName",AuthPass=>"YourPass" );
Or you can set up your own subroutine to call.
In any case, check out the L<send()|/send> method.
=head1 WARNINGS
=head2 Good-vs-bad email addresses with send_by_smtp()
If using L<send_by_smtp()|/send_by_smtp>, be aware that unless you
explicitly provide the email addresses to send to and from you will be
forcing MIME::Lite to extract email addresses out of a possible list
provided in the C<To:>, C<Cc:>, and C<Bcc:> fields. This is tricky
stuff, and as such only the following sorts of addresses will work
reliably:
username
[email protected]
"Name, Full" <[email protected]>
B<Disclaimer:>
MIME::Lite was never intended to be a Mail User Agent, so please
don't expect a full implementation of RFC-822. Restrict yourself to
the common forms of Internet addresses described herein, and you should
be fine. If this is not feasible, then consider using MIME::Lite
to I<prepare> your message only, and using Net::SMTP explicitly to
I<send> your message.
B<Note:>
As of MIME::Lite v3.02 the mail name extraction routines have been
beefed up considerably. Furthermore if Mail::Address is provided then
name extraction is done using that. Accordingly the above advice is now
less true than it once was. Funky email names I<should> work properly
now. However the disclaimer remains. Patches welcome. :-)
=head2 Formatting of headers delayed until print()
This class treats a MIME header in the most abstract sense,
as being a collection of high-level attributes. The actual
RFC-822-style header fields are not constructed until it's time
to actually print the darn thing.
=head2 Encoding of data delayed until print()
When you specify message bodies
(in L<build()|/build> or L<attach()|/attach>) --
whether by B<FH>, B<Data>, or B<Path> -- be warned that we don't
attempt to open files, read filehandles, or encode the data until
L<print()|/print> is invoked.
In the past, this created some confusion for users of sendmail
who gave the wrong path to an attachment body, since enough of
the print() would succeed to get the initial part of the message out.
Nowadays, $AUTO_VERIFY is used to spot-check the Paths given before
the mail facility is employed. A whisker slower, but tons safer.
Note that if you give a message body via FH, and try to print()
a message twice, the second print() will not do the right thing
unless you explicitly rewind the filehandle.
You can get past these difficulties by using the B<ReadNow> option,
provided that you have enough memory to handle your messages.
=head2 MIME attributes are separate from header fields!
B<Important:> the MIME attributes are stored and manipulated separately
from the message header fields; when it comes time to print the
header out, I<any explicitly-given header fields override the ones that
would be created from the MIME attributes.> That means that this:
### DANGER ### DANGER ### DANGER ### DANGER ### DANGER ###
$msg->add("Content-type", "text/html; charset=US-ASCII");
will set the exact C<"Content-type"> field in the header I write,
I<regardless of what the actual MIME attributes are.>
I<This feature is for experienced users only,> as an escape hatch in case
the code that normally formats MIME header fields isn't doing what
you need. And, like any escape hatch, it's got an alarm on it:
MIME::Lite will warn you if you attempt to C<set()> or C<replace()>
any MIME header field. Use C<attr()> instead.
=head2 Beware of lines consisting of a single dot
Julian Haight noted that MIME::Lite allows you to compose messages
with lines in the body consisting of a single ".".
This is true: it should be completely harmless so long as "sendmail"
is used with the -oi option (see L<"Cheap and easy mailing">).
However, I don't know if using Net::SMTP to transfer such a message
is equally safe. Feedback is welcomed.
My perspective: I don't want to magically diddle with a user's
message unless absolutely positively necessary.
Some users may want to send files with "." alone on a line;
my well-meaning tinkering could seriously harm them.
=head2 Infinite loops may mean tainted data!
Stefan Sautter noticed a bug in 2.106 where a m//gc match was
failing due to tainted data, leading to an infinite loop inside
MIME::Lite.
I am attempting to correct for this, but be advised that my fix will
silently untaint the data (given the context in which the problem
occurs, this should be benign: I've labelled the source code with
UNTAINT comments for the curious).
So: don't depend on taint-checking to save you from outputting
tainted data in a message.
=head2 Don't tweak the global configuration
Global configuration variables are bad, and should go away.
Until they do, please follow the hints with each setting
on how I<not> to change it.
=head1 A MIME PRIMER
=head2 Content types
The "Type" parameter of C<build()> is a I<content type>.
This is the actual type of data you are sending.
Generally this is a string of the form C<"majortype/minortype">.
Here are the major MIME types.
A more-comprehensive listing may be found in RFC-2046.
=over 4
=item application
Data which does not fit in any of the other categories, particularly
data to be processed by some type of application program.
C<application/octet-stream>, C<application/gzip>, C<application/postscript>...
=item audio
Audio data.
C<audio/basic>...
=item image
Graphics data.
C<image/gif>, C<image/jpeg>...
=item message
A message, usually another mail or MIME message.
C<message/rfc822>...
=item multipart
A message containing other messages.
C<multipart/mixed>, C<multipart/alternative>...
=item text
Textual data, meant for humans to read.
C<text/plain>, C<text/html>...
=item video
Video or video audio data.
C<video/mpeg>...
=back
=head2 Content transfer encodings
The "Encoding" parameter of C<build()>.
This is how the message body is packaged up for safe transit.
Here are the 5 major MIME encodings.
A more-comprehensive listing may be found in RFC-2045.
=over 4
=item 7bit
Basically, no I<real> encoding is done. However, this label guarantees that no
8-bit characters are present, and that lines do not exceed 1000 characters
in length.
=item 8bit
Basically, no I<real> encoding is done. The message might contain 8-bit
characters, but this encoding guarantees that lines do not exceed 1000
characters in length.
=item binary
No encoding is done at all. Message might contain 8-bit characters,
and lines might be longer than 1000 characters long.
The most liberal, and the least likely to get through mail gateways.
Use sparingly, or (better yet) not at all.
=item base64
Like "uuencode", but very well-defined. This is how you should send
essentially binary information (tar files, GIFs, JPEGs, etc.).
=item quoted-printable
Useful for encoding messages which are textual in nature, yet which contain
non-ASCII characters (e.g., Latin-1, Latin-2, or any other 8-bit alphabet).
=back
=cut
=begin FOR_README_ONLY
=head1 INSTALLATION
Install using
perl makefile.pl
make test
make install
Adjust the make command as is appropriate for your OS.
'nmake' is the usual name under Win32
In order to read the docmentation please use
perldoc MIME::Lite
from the command line or visit
http://search.cpan.org/search?query=MIME::Lite&mode=all
for a list of all MIME::Lite related materials including the
documentation in HTML of all of the released versions of
MIME::Lite.
=cut
=end FOR_README_ONLY
=cut
=head1 HELPER MODULES
MIME::Lite works nicely with other certain other modules if they are present.
Good to have installed are the latest L<MIME::Types|MIME::Types>,
L<Mail::Address|Mail::Address>, L<MIME::Base64|MIME::Base64>,
L<MIME::QuotedPrint|MIME::QuotedPrint>, and L<Net::SMTP>.
L<Email::Date::Format> is strictly required.
If they aren't present then some functionality won't work, and other features
won't be as efficient or up to date as they could be. Nevertheless they are optional
extras.
=head1 BUNDLED GOODIES
MIME::Lite comes with a number of extra files in the distribution bundle.
This includes examples, and utility modules that you can use to get yourself
started with the module.
The ./examples directory contains a number of snippets in prepared
form, generally they are documented, but they should be easy to understand.
The ./contrib directory contains a companion/tool modules that come bundled
with MIME::Lite, they don't get installed by default. Please review the POD
they come with.
=head1 BUGS
The whole reason that version 3.0 was released was to ensure that MIME::Lite is
up to date and patched. If you find an issue please report it.
As far as I know MIME::Lite doesn't currently have any serious bugs, but my
usage is hardly comprehensive.
Having said that there are a number of open issues for me, mostly caused by the
progress in the community as whole since Eryq last released. The tests are
based around an interesting but non standard test framework. I'd like to change
it over to using Test::More.
Should tests fail please review the ./testout directory, and in any bug reports
please include the output of the relevant file. This is the only redeeming
feature of not using Test::More that I can see.
Bug fixes / Patches / Contribution are welcome, however I probably won't apply
them unless they also have an associated test. This means that if I don't have
the time to write the test the patch won't get applied, so please, include tests
for any patches you provide.
=head1 VERSION
Version: 3.033
=head1 CHANGE LOG
Moved to ./changes.pod
NOTE: Users of the "advanced features" of 3.01_0x smtp sending
should take care: These features have been REMOVED as they never
really fit the purpose of the module. Redundant SMTP delivery is
a task that should be handled by another module.
=head1 TERMS AND CONDITIONS
Copyright (c) 1997 by Eryq.
Copyright (c) 1998 by ZeeGee Software Inc.
Copyright (c) 2003,2005 Yves Orton. (demerphq)
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl
itself.
This software comes with B<NO WARRANTY> of any kind.
See the COPYING file in the distribution for details.
=head1 NUTRITIONAL INFORMATION
For some reason, the US FDA says that this is now required by law
on any products that bear the name "Lite"...
Version 3.0 is now new and improved! The distribution is now 30% smaller!
MIME::Lite |
------------------------------------------------------------
Serving size: | 1 module
Servings per container: | 1
Calories: | 0
Fat: | 0g
Saturated Fat: | 0g
Warning: for consumption by hardware only! May produce
indigestion in humans if taken internally.
=head1 AUTHOR
Eryq (F<[email protected]>).
President, ZeeGee Software Inc. (F<http://www.zeegee.com>).
Go to F<http://www.cpan.org> for the latest downloads
and on-line documentation for this module. Enjoy.
Patches And Maintenance by Yves Orton and many others.
Consult ./changes.pod
=cut
|