{"id":9426,"date":"2022-03-08T15:57:06","date_gmt":"2022-03-08T07:57:06","guid":{"rendered":"https:\/\/blog.hoyo.idv.tw\/?p=9426"},"modified":"2022-03-22T19:48:09","modified_gmt":"2022-03-22T11:48:09","slug":"google-apps-%e8%87%aa%e6%95%91-05-%e5%8c%af%e5%85%a5-mbox","status":"publish","type":"post","link":"https:\/\/blog.hoyo.idv.tw\/?p=9426","title":{"rendered":"Google Apps \u81ea\u6551 \u2013 05 \u532f\u5165 MBOX"},"content":{"rendered":"<p>--<\/p>\n<ul>\n<li><a href=\"https:\/\/freepctips.com\/import-mbox-to-gmail\/\" target=\"_blank\" rel=\"noopener\">Free Ways to Import MBOX File to Gmail<\/a><\/li>\n<li><a class=\"question-hyperlink\" href=\"https:\/\/stackoverflow.com\/questions\/1385797\/accessing-my-gmail-inbox-via-php-code\" target=\"_blank\" rel=\"noopener\">accessing my gmail inbox via php code<\/a><\/li>\n<\/ul>\n<p>--<\/p>\n<h2>Mozilla Thunderbird +<\/h2>\n<p>\u4f7f\u7528\u6b64\u65b9\u6848\u53ef\u5c07 mbox \u532f\u5165\u5230\u300c\u672c\u5730\u300d\u4e0a\uff0c\u56e0\u70ba\u8981\u8de8\u88dd\u7f6e\u6240\u4ee5\u9019\u7a2e\u672c\u6a5f\u65b9\u6848\u5c31\u4e0d\u53ef\u884c\uff0c\u8981\u627e Server \u7684\u89e3\u6c7a\u65b9\u6848<\/p>\n<p>--<\/p>\n<h2>Mbox \u8f49 MailDir<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.cainiaojc.com\/note\/qahfk4.html\" target=\"_blank\" rel=\"noopener\">\u4ecePHP\u4e2d\u7684\u6587\u4ef6\u4e2d\u5220\u9664\u6700\u540e\u4e00\u884c<\/a><\/li>\n<\/ul>\n<p>\u73fe\u5728\u7684 Mail Server Postfix \u90f5\u4ef6\u5132\u5b58\u7684\u683c\u5f0f\u90fd\u662f MailDir \u6240\u4ee5 Google \u532f\u51fa\u7684 Mbox \u683c\u5f0f\u9700\u8981\u8f49\u63db\uff0c\u5176\u5be6\u53ea\u662f\u628a\u4fe1\u4ef6\u62c6\u958b\uff0c\u79c9\u8457\u4e0d\u9020\u8f2a\u5b50\u7684\u60f3\u6cd5\u5728\u7db2\u8def\u4e0a\u627e\u4e86\u4e00\u5708\uff0c\u5c31\u662f\u627e\u4e0d\u5230\u53ef\u4ee5\u7528\u7684\u8f49\u63db\u7a0b\u5f0f\uff0c\u660e\u660e\u5c31\u6c92\u6709\u5f88\u96e3\uff0c\u53ea\u597d\u81ea\u5df1\u5beb\u4e86\u4e00\u500b<\/p>\n<p>\u56e0\u70ba\u5224\u65b7\u9593\u9694\u65b9\u6cd5\u56e0\u7d20\uff0c\u5fc5\u9808\u628a\u4e0b\u4e00\u5c01\u4fe1\u7684 From \u522a\u9664\uff0c\u672c\u4f86\u4f7f\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u63db\u884c\u8f49\u6210\u9663\u5217\uff0c\u518d\u4f7f\u7528 array_pop() \u7684\u65b9\u5f0f\uff0c\u8003\u616e\u5230\u8d85\u5927\u8cc7\u6599\u6709\u53ef\u80fd\u6703\u8b93\u8a18\u61b6\u9ad4\u7206\u70b8\u6240\u4ee5\u63db\u4e86\u4e00\u500b\u65b9\u5f0f<\/p>\n<p>mbox2maildir.php<\/p>\n<pre class=\"lang:php decode:true\">&lt;?php\r\nset_time_limit(0);\r\nini_set('display_errors', true);\r\nini_set('error_reporting', E_ALL &amp; ~E_NOTICE &amp; ~E_STRICT &amp; ~E_DEPRECATED);\r\n\r\n\/\/ \u8f38\u5165\u53c3\u6578\u6aa2\u67e5\r\nif ( !$argv[1] || !$argv[2] ){\r\n    echo &lt;&lt;&lt;Output\r\n\r\nUsage: php mbox2maildir.php mbox_file output_dir\/\r\n\r\nOutput;\r\n    exit;\r\n}\r\n\r\nfunction remove_last_line($filename){\r\n    \/\/ \u6253\u5f00\u6587\u4ef6\r\n    $file_handle = fopen($filename, 'r');\r\n\r\n    \/\/ \u8bbe\u7f6e\u5faa\u73af\u53d8\u91cf\r\n    $linebreak  = false;\r\n    $file_start = false;\r\n\r\n    \/\/ \u8981\u67e5\u770b\u7684\u5b57\u8282\u6570\r\n    $bite = 256;\r\n\r\n    \/\/ \u6587\u4ef6\u5927\u5c0f\r\n    $filesize = filesize($filename);\r\n\r\n    \/\/ \u5c06\u6307\u9488\u653e\u5728\u6587\u4ef6\u672b\u5c3e\u3002\r\n    fseek($file_handle, 0, SEEK_END);\r\n\r\n    while ($linebreak === false &amp;&amp; $file_start === false) {\r\n        \/\/ \u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u4f4d\u7f6e\u3002\r\n        $pos = ftell($file_handle);\r\n\r\n        if ($pos &lt; $bite) {\r\n            \/\/ \u5982\u679c\u4f4d\u7f6e\u5c0f\u4e8e\u4e00\u53e3\uff0c\u5219\u8f6c\u5230\u6587\u4ef6\u7684\u5f00\u5934\r\n            rewind($file_handle);\r\n        } else {\r\n            \/\/ \u5c06$bite\u4e2a\u5b57\u7b26\u79fb\u56de\u6587\u4ef6\r\n            fseek($file_handle, -$bite, SEEK_CUR);\r\n        }\r\n\r\n        \/\/ \u5c06\u6587\u4ef6\u7684$bite\u4e2a\u5b57\u7b26\u8bfb\u5165\u5b57\u7b26\u4e32\u3002\r\n        $string = fread($file_handle, $bite) or die (\"Can't read from file \" . $filename . \".\");\r\n\r\n        \/* If we happen to have read to the end of the file then we need to ignore\r\n         * the last line as this will be a new line character.\r\n         *\/\r\n        if ($pos + $bite &gt;= $filesize) {\r\n            $string = substr_replace($string, '', -1);\r\n        }\r\n\r\n        \/\/ \u7531\u4e8e\u6211\u4eec\u5c06fred()\u524d\u8fdb\u5230\u6587\u4ef6\u4e2d\uff0c\u56e0\u6b64\u9700\u8981\u5907\u4efd$bite\u5b57\u7b26\u3002\r\n        if ($pos &lt; $bite) {\r\n            \/\/ \u5982\u679c\u4f4d\u7f6e\u5c0f\u4e8e\u4e00\u53e3\uff0c\u5219\u8f6c\u5230\u6587\u4ef6\u7684\u5f00\u5934\r\n            rewind($file_handle);\r\n        } else {\r\n            \/\/ \u5c06$bite\u4e2a\u5b57\u7b26\u79fb\u56de\u6587\u4ef6\r\n            fseek($file_handle, -$bite, SEEK_CUR);\r\n        }\r\n\r\n        \/\/ \u6211\u4eec\u8bfb\u53d6\u7684\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u6709\u6362\u884c\u7b26\uff1f\r\n        if (is_integer($lb = strrpos($string, \"\\n\"))) {\r\n            \/\/ \u5c06$linebreak\u8bbe\u7f6e\u4e3atrue\uff0c\u4ee5\u4fbf\u6211\u4eec\u8df3\u51fa\u5faa\u73af\r\n            $linebreak = true;\r\n            \/\/ \u6587\u4ef6\u4e2d\u7684\u6700\u540e\u4e00\u884c\u5728\u6362\u884c\u7b26\u4e4b\u540e\r\n            $line_end = ftell($file_handle) + $lb + 1;\r\n        }\r\n\r\n        if  (ftell($file_handle) == 0) {\r\n            \/\/ \u5982\u679c\u6211\u4eec\u4f4d\u4e8e\u6587\u4ef6\u7684\u5f00\u5934\uff0c\u8bf7\u8df3\u51fa\u5faa\u73af\u3002\r\n            $file_start = true;\r\n        }\r\n    }\r\n\r\n    if ($linebreak === true) {\r\n        \/\/ \u5982\u679c\u627e\u5230\u6362\u884c\u7b26\uff0c\u5219\u5c06\u6587\u4ef6\u8bfb\u5165\u5b57\u7b26\u4e32\u4ee5\u5199\u5165\u800c\u65e0\u9700\u6700\u540e\u4e00\u884c\u3002\r\n        rewind($file_handle);\r\n        $file_minus_lastline = fread($file_handle, $line_end);\r\n\r\n        \/\/ \u5173\u95ed\u6863\u6848\r\n        fclose($file_handle);\r\n\r\n        \/\/ \u4ee5\u5199\u5165\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u5e76\u622a\u65ad\u5b83\u3002\r\n        $file_handle = fopen($filename, 'w+');\r\n        fputs($file_handle, $file_minus_lastline);\r\n        fclose($file_handle);\r\n    } else {\r\n        \/\/ \u5173\u95ed\u6863\u6848, nothing else to do.\r\n        fclose($file_handle);\r\n    }\r\n}\r\n\r\n$size = filesize($argv[1]);\r\n$handle = fopen($argv[1], 'r');\r\n$new = '';\r\n\r\n$i = 1;\r\n$j = 1;\r\nif ($handle) {\r\n    while (($buffer = fgets($handle)) !== false) {\r\n        $new .= $buffer;\r\n\r\n        \/\/ \u986f\u793a\u9032\u5ea6\r\n        echo $i . '\/' . $j.\"\\n\";\r\n\r\n        \/\/ \u5224\u65b7\u4e0b\u4e00\u5c01\u4fe1\r\n        if ( preg_match(\"\/\\r\\n\\r\\nFrom \/\", $new) ){\r\n            $j = 1;\r\n\r\n            $filename = $argv[2] . microtime(true) .'.'. uniqid();\r\n            \/\/ \u5c07\u4fe1\u5132\u5b58\r\n            $file_name = '.\/'. microtime(true) .'.'. uniqid();\r\n            file_put_contents($file_name, $new);\r\n            exec('chown vmail:vmail '.$file_name);\r\n\r\n            \/\/ \u5c07\u4e0b\u4e00\u5c01\u4fe1\u7684 From \u79fb\u9664\r\n            remove_last_line($filename);\r\n            remove_last_line($filename);\r\n            remove_last_line($filename);\r\n\r\n            \/\/ \u4e0b\u4e00\u5c01\u4fe1\u958b\u59cb\r\n            $new = $buffer;\r\n            $i++;\r\n        }\r\n\r\n        $j++;\r\n    }\r\n}\r\n\r\n\/\/ \u6700\u5f8c\u4e00\u5c01\r\nfile_put_contents($argv[2]. microtime(true) .'.'. uniqid(), $new);\r\n\r\nfclose($handle);<\/pre>\n<p>\u7528\u6cd5<\/p>\n<p>\u5207\u63db\u5230\u4f7f\u7528\u8005\u7684 maildir cur \u76ee\u9304\uff0c\u518d\u57f7\u884c<\/p>\n<pre class=\"lang:default decode:true\"># php mbox2maildir.php mbox\u6a94\u6848 \u8f38\u51fa\u8def\u5f91<\/pre>\n<p>\u7d50\u679c\u5927\u6982\u5c31\u662f\u9577\u9019\u6a23<\/p>\n<p><a href=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/03\/Image-007.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-9660\" src=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/03\/Image-007-300x143.png\" alt=\"\" width=\"300\" height=\"143\" srcset=\"https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/03\/Image-007-300x143.png 300w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/03\/Image-007-768x366.png 768w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/03\/Image-007-1024x488.png 1024w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/03\/Image-007-500x239.png 500w, https:\/\/blog.hoyo.idv.tw\/wp-content\/uploads\/2022\/03\/Image-007.png 1371w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>--<\/p>\n<h2>\u4f7f\u7528 array \u522a\u9664\u6700\u5f8c\u884c\u7684\u65b9\u5f0f<\/h2>\n<pre class=\"lang:php decode:true\">$p = explode(\"\\r\\n\", $new);\r\narray_pop($p);\r\narray_pop($p);\r\n$c = implode(\"\\r\\n\", $p);<\/pre>\n<p>--<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"9426\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,865&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>-- Free Ways to...<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p class=\"pvc_stats all \" data-element-id=\"9426\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> &nbsp;1,865&nbsp;total views<\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[316],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/9426"}],"collection":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9426"}],"version-history":[{"count":14,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/9426\/revisions"}],"predecessor-version":[{"id":9760,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/9426\/revisions\/9760"}],"wp:attachment":[{"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hoyo.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}