I use Fastmail for my personal email, and I like to
keep a backup of my email on my personal computer. Why make a backup? When I am done reading or
replying to an email, I make a split-second decision on whether to delete or
archive it on Fastmail’s server. If it turns out I deleted something that I need later, I can always
look in my backup. The backup also predates my use of Fastmail and serves as a
service-independent store of my email.
My old method of backing up the email was to forward all my email to a Gmail
account, then use POP to download the email with a hacked-together script. This
had the added benefit that the Gmail account also served as a searchable backup.
Unfortunately the Gmail account ran out of storage and the POP script kept
hanging for some reason, which together motivated me to get away from this
convoluted backup strategy.
The replacement script uses JMAP to connect directly to
Fastmail and download all messages. It is intended to run periodically, and
what it does is pick an end time 24 hours in the past, download all email older than
that, and then record the end time. The next time it runs, it searches for mail
between the previous end time and a new end time, which is again 24 hours in
Why pick a time in the past? Well, I’m not confident that if you search up until
this exact moment, you are guaranteed to get every message. A message could come
in, then two seconds later you send a query, but it hits a server that doesn’t
know about your message yet. I’m sure an hour is more than enough leeway, but
since this is a backup, we might as well make it a 24-hour delay.
Note that I am querying all mail, regardless of which mailbox it is in, so even
if I have put a message in the trash, my backup script will find it and
JMAP is a modern JSON-based replacement for IMAP and much easier to use, such
that the entire script is 135 lines, even with my not-exactly-terse use of
Here is the script, with some notes below.
The get_session function is run once at the beginning of the script, and
fetches some important data from the server including the account ID and a
URL to use to download individual emails.
The query function does the bulk of the work, sending a single JSON request
multiple times to page through the search results. It is actually a two-part
request, first Email/query, which returns a list of ids, and then
Email/get, which gets some email metadata for each result. I wrote this as a
generator to make the
main part of my script simpler. The paging is performed by capturing the ID of
the final result of one query, and asking the next query to start at that
position plus one (lines 73-74). We are done when the query returns no results
The download_email function uses the blob ID to fetch the entire email and
saves it to disk. This doesn’t really need to be its own function, but it
will help if I later decide to use multiple threads to do the downloading.
Finally, the main part of the script reads configuration from a YAML file,
including the last end time. It loops through the results of query, calling
download_email on each result. Finally, it writes the configuration data back
out to the YAML file, including the updated last_end_time.
I understand the popularity of email newsletters,
especially for publishers.
It’s a simple way to get paid content out, easier for users than a private RSS feed.
But that doesn’t mean I want to read newsletters in my email app.
Feedbin, which I am already using for my regular RSS subscriptions, bridges the gap.
As part of my Feedbin account, I get a secret email address,
and anything sent to that address ends up in my RSS reader.
But it quickly gets annoying to sign up for newsletters (often creating an account)
with an email address that is neither memorable nor truly mine.
Fastmail, which I am already using for my regular email, makes it easy to
find specified emails sent to my regular address, forward them to my feedbin address, and put the original in the trash.
In fact, Fastmail lets me use “from a member of a given contact group” as the trigger
for this automatic rule, which makes the setup for a new newsletter very simple:
Subscribe to the newsletter
Add the sender to my Fastmail address book
Add the newly created contact to my “Feedbin” group
This is very convenient, for newsletters as well as other mail that is more of a notification than an email. Here are some of the emails that I now read as though they were feeds:
This is the fourth and last in a series of posts
describing the system I built to monitor long-running commands.
The third post explained my mon script, which together
with my twait script from the second post,
lights up an LED when a command in a tmux pane or iTerm window completes.
If I want to monitor a command, I use mon twait ID, where ID is the pane or
window ID. Or I can run another command only if the first command is successful,
by writing twait ID && ./other_command.
Of course it would be annoying to type the window ID manually, so I have two
keyboard shortcuts. One to start mon twait ID in a separate window that closes
automatically when it is done. The other to open a new window with the text
twait ID && followed by a space.
Tmux can be scripted through the use of unix-like commands, with arguments and
options. These can either be run from the command line or assigned keyboard
I use Ctrl-A plus uppercase E for the first.
This splits to form a new window, 5 lines high, and runs mon twait. The #D
parameter in a tmux run command gets translated into the pane ID.
I use Ctrl-A plus lowercase E for the second.
You can do very similar things with iTerm using AppleScript.
To open the monitor in a temporary pane, I use this script.
It is possible to control the height of the new pane, but I haven’t
figured it out yet.
I prefer new windows over tabs or split windows, so I use this script to
open a new window prepped to run a follow-up command.
This is the third in a series of posts
describing the system I built to monitor long-running commands.
The second post explained my twait script, which
waits for a command in a separate tmux pane or iTerm window to complete,
and then exits with the same code.
The easiest way to get notified when twait finishes is to chain the commands:
But this ignores the return value, which is a useful part of the notification.
You could solve this by passing the return value as an argument:
The $? variable contains the return code of the last command.
I mostly use a script called mon that takes the other command as an argument:
Since mon is running the command, it has access to its exit code. It can also
do some prep work before running the command. The downside is that you have to
keep the command fairly simple; using pipes, multiple commands, or shell aliases
does not work properly.
Here is the code to my monitor script, which lights my blink LED and
sends a Pushover notification.
This sets the LED to a dim white at the beginning of the command, prints a
message, and then runs the command. The $@ variable contains all of the
arguments to the script, which should be the command to run, followed by its
arguments. When we are done with the command, we capture the return code.
Depending on the value of the return code,
I turn the LED green, red, or blue, and send a message to my phone and watch.
The pushover script uses curl to send a request to the pushover servers.
Those are not my token or userid. You have to get your own from pushover.
The final post in this series will show some tmux and AppleScript
integrations to make this monitoring easier to launch.