{"id":1043,"date":"2025-11-29T02:25:15","date_gmt":"2025-11-29T09:25:15","guid":{"rendered":"https:\/\/strawhousepig.net\/wordpress\/?p=1043"},"modified":"2025-12-04T11:25:14","modified_gmt":"2025-12-04T18:25:14","slug":"parse-usps-clink-n-ship-receipt-e-mail","status":"publish","type":"post","link":"https:\/\/strawhousepig.net\/wordpress\/2025\/11\/29\/parse-usps-clink-n-ship-receipt-e-mail\/","title":{"rendered":"Parse USPS Clink-n-Ship receipt e-mail"},"content":{"rendered":"\n<p>UPDATE: I worked this out at home on Manjaro which uses GNU &#8216;date&#8217; (I assume), but macOS &#8216;date&#8217; isn&#8217;t as foolproof. Also can&#8217;t be used against stdin apparently. :\\<\/p>\n\n\n\n<p>The purpose being to pare their e-mail down to just the receipt part so you can print as few pages as possible.<\/p>\n\n\n\n<p>Used to do this with AppleScript using Mail.app. Going to Thunderbird means a different option needs to be found. Using a Folder Action can do that by calling this script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# Set to true to write variables to a tmp file (\/tmp\/USPS_debug.txt).\n_debug=false\n\n# cat contents of .eml file, awk removes newlines after '=', tr removes the rest.\n_mess=\"`cat \"$1\" | awk '1' RS='=\\r\\n' ORS= | tr -d '\\r\\n'`\"\nif &#91; $_debug = true ]; then echo \"_mess: $_mess\" > \/tmp\/USPS_debug.txt; fi\n\n# Extract the UTC 0 date from the Date header, date converts it to locale\n# The Linux\/GNU version of 'date' needs less massaging than the one included with macOS. Same for 'sed', actually.\n#_date=\"`echo $_mess | sed -E 's\/.+Date: (&#91;A-Za-z]{3}, &#91;0-9]+? &#91;A-Za-z]{3} &#91;0-9]{4} &#91;0-9]+(:&#91;0-9]+){2}).+\/\\1 UTC\/g' | date -f -`\"\n# The below two _date var lines are for macOS. You could subshell it into one line, but *meh*.\n_date=\"`echo $_mess | sed -E 's\/.+Date: (&#91;A-Za-z]{3}, &#91;0-9]{1,2} &#91;A-Za-z]{3} &#91;0-9]{4} &#91;0-9]{2}(:&#91;0-9]{2}){2}).+\/\\1\/g'`\"\nif &#91; $_debug = true ]; then echo \"_date: $_date\" >> \/tmp\/USPS_debug.txt; fi\n_date=\"`date -j -v-7H -f \\\"%a, %d %b %Y %R:%S\\\" \\\"$_date\\\"`\"\n\n\n# Extract the order number.\n_order=\"`echo $_mess | sed -E 's\/.+>(&#91;a-z0-9]{8}(-&#91;a-z0-9]{4}){3}-&#91;a-z0-9]{12})&lt;.+\/\\1\/g'`\"\nif &#91; $_debug = true ]; then echo \"_order: $_order\" >> \/tmp\/USPS_debug.txt; fi\n\n# Extract the section containing the juicy bits, remove more e-mail chaff, fix the tracking link, break up that one big line of code.\n_sec4=\"`echo $_mess | sed -E -e 's\/.+START SECTION 4 -->(.+)&lt;!-- END SECTION 4.+\/\\1\/g' -e 's\/(=3D)|(=C2)|(=AE)\/\/g' -e 's\/(.+)&lt;a hrefh(.+&#91;0-9]{34})>(.+)\/\\1&lt;a href=\\\"h\\2\\\">\\3\/g' -e 's\/(&#91;a-z]+>) (&lt;)\/\\1\\n\\2\/g'`\"\nif &#91; $_debug = true ]; then echo \"_sec4: $_sec4\" >> \/tmp\/USPS_debug.txt; fi\n\n# Get down to the nitty gritty.\necho \"&lt;html>\n&lt;head>\n&lt;style>\nbody { width: 576px; font-size: 0.8em; }\n.pt-5 {margin-bottom: 0.5em; margin-top: 0em; }\n.bold {line-height: 1em !important; }\n&lt;\/style>\n&lt;\/head>\n&lt;p class=\\\"pt-5\\\">Received: $_date&lt;\/p>\n&lt;p class=\\\"pt-5\\\">Order # $_order&lt;\/p>\n$_sec4\n&lt;\/body>\n&lt;\/html>\" > \/tmp\/USPS-receipt.html\n\nopen \/tmp\/USPS-receipt.html\n<\/code><\/pre>\n\n\n\n<p>Only problem I have found is the dragging of messages out of Thunderbird can be interrupted with a replace file warning at the temp folder being used to first store the dragged message.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: I worked this out at home on Manjaro which uses GNU &#8216;date&#8217; (I assume), but macOS &#8216;date&#8217; isn&#8217;t as foolproof. Also can&#8217;t be used against stdin apparently. :\\ The purpose being to pare their e-mail down to just the receipt part so you can print as few pages as possible. Used to do this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[19,33,34],"class_list":["post-1043","post","type-post","status-publish","format-standard","hentry","category-code","tag-internets","tag-script","tag-shell-script"],"_links":{"self":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/1043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/comments?post=1043"}],"version-history":[{"count":8,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/1043\/revisions"}],"predecessor-version":[{"id":1102,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/posts\/1043\/revisions\/1102"}],"wp:attachment":[{"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/media?parent=1043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/categories?post=1043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawhousepig.net\/wordpress\/wp-json\/wp\/v2\/tags?post=1043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}