Parse USPS Clink-n-Ship receipt e-mail

UPDATE: I worked this out at home on Manjaro which uses GNU ‘date’ (I assume), but macOS ‘date’ isn’t as foolproof. Also can’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 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:

#!/bin/bash

# Set to true to write variables to a tmp file (/tmp/USPS_debug.txt).
_debug=false

# cat contents of .eml file, awk removes newlines after '=', tr removes the rest.
_mess="`cat "$1" | awk '1' RS='=\r\n' ORS= | tr -d '\r\n'`"
if [ $_debug = true ]; then echo "_mess: $_mess" > /tmp/USPS_debug.txt; fi

# Extract the UTC 0 date from the Date header, date converts it to locale
# The Linux/GNU version of 'date' needs less massaging than the one included with macOS. Same for 'sed', actually.
#_date="`echo $_mess | sed -E 's/.+Date: ([A-Za-z]{3}, [0-9]+? [A-Za-z]{3} [0-9]{4} [0-9]+(:[0-9]+){2}).+/\1 UTC/g' | date -f -`"
# The below two _date var lines are for macOS. You could subshell it into one line, but *meh*.
_date="`echo $_mess | sed -E 's/.+Date: ([A-Za-z]{3}, [0-9]{1,2} [A-Za-z]{3} [0-9]{4} [0-9]{2}(:[0-9]{2}){2}).+/\1/g'`"
if [ $_debug = true ]; then echo "_date: $_date" >> /tmp/USPS_debug.txt; fi
_date="`date -j -v-7H -f \"%a, %d %b %Y %R:%S\" \"$_date\"`"


# Extract the order number.
_order="`echo $_mess | sed -E 's/.+>([a-z0-9]{8}(-[a-z0-9]{4}){3}-[a-z0-9]{12})<.+/\1/g'`"
if [ $_debug = true ]; then echo "_order: $_order" >> /tmp/USPS_debug.txt; fi

# Extract the section containing the juicy bits, remove more e-mail chaff, fix the tracking link, break up that one big line of code.
_sec4="`echo $_mess | sed -E -e 's/.+START SECTION 4 -->(.+)<!-- END SECTION 4.+/\1/g' -e 's/(=3D)|(=C2)|(=AE)//g' -e 's/(.+)<a hrefh(.+[0-9]{34})>(.+)/\1<a href=\"h\2\">\3/g' -e 's/([a-z]+>) (<)/\1\n\2/g'`"
if [ $_debug = true ]; then echo "_sec4: $_sec4" >> /tmp/USPS_debug.txt; fi

# Get down to the nitty gritty.
echo "<html>
<head>
<style>
body { width: 576px; font-size: 0.8em; }
.pt-5 {margin-bottom: 0.5em; margin-top: 0em; }
.bold {line-height: 1em !important; }
</style>
</head>
<p class=\"pt-5\">Received: $_date</p>
<p class=\"pt-5\">Order # $_order</p>
$_sec4
</body>
</html>" > /tmp/USPS-receipt.html

open /tmp/USPS-receipt.html

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.

Leave a Reply

Your email address will not be published. Required fields are marked *