Categories
Productivity

Taking back email – Part 5: msmtp

Recently, I decided to revamp my email setup. I don’t really love email, but it’s such an important method of communication that I needed to get it under control. I’m going to put together a several-post guide explaining my reasoning, technical details, and my new email process.

Other posts in this series (all will be linked here as the posts go up:)

Intro

msmtp is the final piece in the puzzle. So far we have:

  • OfflineIMAP to fetch mail from an IMAP server
  • notmuch to index and tag your email
  • alot to read and compose email, and khard for integrating an address book

msmtp is a utility for sending email through an SMTP server. After you compose an email using alot, msmtp will handle sending it out. You’ll want msmtp installed on the machine you are using to compose email – so I have it installed on my two workstations rather than on a centralized server.

(In practice, I have it on the server as well in case I ever need to ssh in and write an email, but I haven’t used that option yet).

Installing msmtp

In Arch Linux, msmtp is in the standard repositories. Install it like so:

sudo pacman -S msmtp

Configuration

I should mention that the Arch Linux wiki is really helpful for just about everything, and msmtp is no exception. They have a great article here.

After installing, copy the example configuration into your home directory and start editing!

cp /usr/share/doc/msmtp/msmtp-user.example ~/.msmtprc
vim ~/.msmtprc

Overall, editing this file is pretty easy – just follow the example. Your SMTP server may require some specific settings, so play with it a bit. See some testing tips in the next section of this post.

Here’s an example of one of my accounts, with some sensitive information edited:

account ajy
host smtp.example.com
port 587 # Alternatively, try port 465 if this one doesn't work
from aaron@example.com
user aaron@example.com
# tls and password - you should decide how you will handle these

For the TLS certificate and password settings, follow the examples in the config file based on what strategy you will use.

Set a default account:

account default : ajy # Choose an account name as specified above

Testing your configuration

To test your SMTP configuration, I recommend creating an email in a text file: vim test-mail.txt with something like the following content:

To: username@domain.com
From: username@gmail.com
Subject: A test

Hello there.

Use real To and From emails so you can confirm the mail actually goes through.

Then, run this command:

cat test-mail.txt | msmtp -a account-name

The -a flag specifies which account will be used. Replace account-name with the name of an account from your .msmtprc file.

Integrating with alot

In practice, you won’t have to deal directly with msmtp. Instead, the send command in alot should invoke msmtp automatically.

To set this up, see the previous post in this series (linked at top). The example alot configuration I supplied includes configuration for msmtp on a per-account basis.

Next

A final wrap-up!

Categories
Productivity

Taking back email – Part 4: alot and khard

Recently, I decided to revamp my email setup. I don’t really love email, but it’s such an important method of communication that I needed to get it under control. I’m going to put together a several-post guide explaining my reasoning, technical details, and my new email process.

Other posts in this series (all will be linked here as the posts go up:)

Intro

alot is a simple email client that is meant mainly to be a front-end to work with your notmuch email database. alot provides easy ways to view and read your email, to search, and to compose email. Alot does not, however:

  • Receive email (we’re using OfflineIMAP to grab email from the server)
  • Send email (in the next post, I’ll cover using msmtp to handle sending out email)

khard is a command line contacts manager that can read a CardDav-generated contacts directory. I won’t cover too much about khard in this post, other than how it integrates with alot to make it easier to write your emails.

Installing alot and khard

For Arch Linux, both alot and khard are in the Arch User Repository. Use your favorite AUR helper, something like:

packer -S alot khard

Configuring alot

When configuring alot, the documentation will be very helpful for you: alot documentation

Write a configuration file: vim ~/.config/alot/config.

I’ll go ahead and put my entire alot configuration here. I’m editing out my email addresses, but otherwise it is unchanged:

editor_cmd = terminator -T "Alot Compose" -x vim
tabwidth = 2
attachment_prefix = "~/Downloads"
bug_on_exit = true
editor_in_thread = true

[bindings]

up = move up
down = move down
page up = move page up
page down = move page down
j = move down
k = move up
'g g' = move first
G = move last
' ' = move page down
'ctrl d' = move halfpage down
'ctrl u' = move halfpage up
@ = refresh
? = help bindings
I = search tag:inbox AND NOT tag:killed
'#' = taglist
shift tab = bprevious
U = search tag:unread
tab = bnext
\ = prompt 'search '
x = bclose
$ = flush
m = compose
o = prompt 'search '
q = exit
';' = bufferlist
':' = prompt
. = repeat

[[bufferlist]]
    x = close
    enter = open

[[search]]
    enter = untag unread; select
        a = tag archive; untag inbox; untag todo;
        d = retag delete
    & = toggletags killed
    ! = toggletags flagged
    s = toggletags unread
    t = tag todo; untag inbox;
    i = toggletags inbox
    l = retagprompt
    O = refineprompt
    | = refineprompt

[[envelope]]
    a = prompt 'attach ~/'
    y = send
    P = save
    s = 'refine Subject'
    f = prompt 'set From '
    t = 'refine To'
    b = 'refine Bcc'
    c = 'refine Cc'
    S = togglesign
    enter = edit
    'g f' = togglesource

[[taglist]]
    enter = select

[[thread]]
    enter = select
    C = fold *
    E = unfold *
    c = fold
    e = unfold
    < = fold
    > = unfold
    'g f' = togglesource
    H = toggleheaders
    P = print --all --separately --add_tags
    S = save --all
    g = reply --all
    f = tag forwarded; forward
    p = print --add_tags
    n = editnew
    b= bounce
    s = save
    r = tag replied; reply
    | = prompt 'pipeto '

    'g j' = move next sibling
    'g k' = move previous sibling
    'g h' = move parent
    'g l' = move first reply
    ' ' = move next

[accounts]
    [[aaron@address1.com]
        realname = Aaron Young
        address = aaron@address1.com
        sendmail_command = msmtp --account=ajy -t

                [[[abook]]]
            type = shellcommand
            command = khard email -s
            regexp = '^(?P<email>[^@]+@[^\t]+)\t+(?P<name>[^\t]+)'
            ignorecase = True

    [[aaron@address2.com]]
        realname = Aaron Young
        address = aaron@address2.com
        sendmail_command = msmtp --account=gmail -t

                [[[abook]]]
            type = shellcommand
            command = khard email -s
            regexp = '^(?P<email>[^@]+@[^\t]+)\t+(?P<name>[^\t]+)'
            ignorecase = True

    [[aaron@address3.com]]
        realname = Aaron Young
        address = aaron@address3.com
        sendmail_command = msmtp --account=work -t

                [[[abook]]]
            type = shellcommand
            command = khard email -s
            regexp = '^(?P<email>[^@]+@[^\t]+)\t+(?P<name>[^\t]+)'
            ignorecase = True

OK – a couple notes.

  1. I start with a few basic settings. I tell alot to open a new terminal window, running vim, when composing an email.
  2. Next, I define the keybindings – first, global keybindings, then the keybindings in different views (thread, envelope, etc.). Note that some of these keybindings can run a series of commands, one after the next.
  3. Finally, I define basic settings for each email account – the sendmail command, and the address book configuration. When I am composing email, I can use my tab key to find email addresses from the khard address book.

HTML email

You’ll likely want to be able to read HTML email. The easiest way to handle this is to install w3m, a basic console web browser:

  • sudo pacman -S w3m
  • Then, vim ~/.mailcap

Contents of your ~/.mailcap file:

text/html; w3m -dump -o -document_charset=%{charset} %s; nametemplate=%s.html; copiousoutput

That’ll do it.

Syncing your configuration files

I’ve mentioned that I use Syncthing for syncing my email folder. I also sync my alot and khard configs from one workstation to another. This way I don’t need to worry about having an out-of-date configuration file on one of my machines.

Configuring khard

I’m not going to get too far in here since it’s right at the edge of the scope of this series. Here are the basics:

  • If you are using a CardDav server to handle your contacts, you should set up vdirsyncer to periodically sync your contacts into a directory (the contacts directory is called a vdir). You can sync this directory between your computers.
  • Of course, after installing khard, you’ll want to set up your config file. Run vim ~/.config/khard/khard.conf. Here’s the example config. Modify as needed.
  • Using the above alot configuration settings, alot will use the khard address book to tab-complete email addresses for you when composing email.

Using alot

When you open alot, you’ll be greeted with any unread / inbox messages you have. Note that you’re not viewing an IMAP directory – instead, it’s a view of messages with a certain tag.

You can press o, or type :search to start searching:

  • :search tag:archive
  • :search Chris
  • etc.

To compose, type :compose and fill out the information you’re prompted for. You’ll get a Vim window where you can write the email. When you’re done, type :wq in vim to save and quit. Then, back at your alot window, type :send to send the email (but you’ll need msmtp installed and configured to actually send the mail).

These are a couple basic usage tips. You’ll pick up alot pretty quickly. It’s handy to remember that typing a question mark ? will show you a list of keybindings for your current view, and you can use the :help command to give you additional hints about a particular command.

Next

Next, we need to get msmtp installed, so you can send email. Stay tuned!

Categories
Productivity

Taking back email – Part 3: notmuch

Recently, I decided to revamp my email setup. I don’t have too much going on with email, but it’s such an important method of communication that I needed to get it under control. I’m going to put together a several-post guide explaining my reasoning, technical details, and my new email process.

Other posts in this series (all will be linked here as the posts go up:)

Intro

notmuch is an email indexer and tagger. It’s used to create a searchable database of mails and to allow you to arbitrarily tag emails with different keywords. Searching for your email is easy and SUPER fast with notmuch.

I have notmuch installed on my home server along with OfflineIMAP, although using a remote VPS would work just as well. This way, my email folders and database have a central home, and I can sync them to my various workstations with Syncthing.

Installing notmuch

This is easy in Arch Linux:

sudo pacman -S notmuch

Notmuch might be packaged for your Linux distro as well. Check in the repositories – if not, then check the notmuch website.

Configuration

Instead of copying and editing a config file, notmuch bootstraps the configuration. Just run a command:

notmuch setup

And you’ll be dropped into an interactive setup. Simply answer the questions:

  • Your full name (“Aaron Young”)
  • Your primary email address
  • Add any other email addresses you are configuring (this all will mirror your OfflineIMAP config)
  • The path to your mail directory. This is the same folder where OfflineIMAP is downloading your email.
  • Tags to apply to new mail: “unread” and “inbox” are the defaults and they’re just fine, but feel free to add or remove tags to fit your workflow. These tags will be automatically applied by notmuch on new emails that come in.
  • Tags to exclude when searching email – these are the emails that you never really want to see.

Now, you can take a look at the config that was generated:

vim ~/.notmuch-config

and see how you like it.

Running after OfflineIMAP sync

So the basic setup so far has OfflineIMAP checking for new email, once an hour with your cron job, and putting it in your local mail directory. Let’s have notmuch tag those new emails.

Use crontab -e to edit your crontab, and add:

47 5-21 * * * offlineimap && notmuch new

Now, notmuch will process your email after offlineimap has finished downloading it. Of course, this is the same cron command that I mentioned in the last post in this series, but now we understand the second part.

Next Steps

Now, our central server is fetching and indexing your emails, and you may have set up Syncthing or a similar service to sync your mail folder between all your workstations. Next, I’ll discuss how to use alot and khard to read and manage your email messages.

Categories
Productivity

Taking back email – Part 2: OfflineIMAP

Recently, I decided to revamp my email setup. I don’t much like email, but it’s such an important method of communication that I needed to get it under control. I’m putting together a several-post guide explaining my reasoning, technical details, and my new email process.

Other posts in this series (all will be linked here as the posts go up:)

Intro

OfflineIMAP is a simple Linux command-line tool that syncs email messages and folders from an IMAP server to your computer (and back). I use it to aggregate emails from my three accounts into a directory on my home server. I have it set up to fetch my email once an hour during my awake-time of the day.

I have OfflineIMAP installed on my home server. My mail is collected and indexed on my server, then I have the folders synced to my home and work computers with Syncthing.

Installing OfflineIMAP

OfflineIMAP is likely packaged for your Linux distro of choice. In Arch Linux:

sudo pacman -S offlineimap

After installing, you should copy the default configuration into your user directory and edit it:

cp /usr/share/offlineimap/offlineimap.conf ~/.offlineimaprc
vim ~/.offlineimaprc

The default configuration file is VERY heavily commented and is pretty clear. I’m just going to hit some highlights here.

Configuration

General

[general]
accounts = ajy,gmail,work

Start with a comma-separated list of accounts. Remember those account names because you’ll be needing them shortly. In fact, I like keeping my account names consistent from OfflineIMAP to notmuch to alot and msmtp, through the whole process.

[mbnames]

So far I am doing nothing with this section.

Accounts

[Account ajy]
localrepository = ajyLocal
remoterepository = ajyRemote
maxage = 730

I’m initally only syncing the last two years (730 days) of mail. I don’t need to go further back. Note that I used the account name from above (ajy), and I’m specifying two repositories that I’m going to define next:

[Repository ajyLocal]
type = Maildir
localfolders = ~/mail/ajy
sep = /

[Repository ajyRemote]
type = IMAP
remotehost = imap.example.com
remoteuser = aaron@example.com
## do something for password ##
cert_fingerprint = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
folderfilter = lambda foldername: foldername not in [
        'Trash', 'Spam', 'Templates', 'Queue', 'Drafts']

In the local repository, everything is pretty straightforward. Using / as a separator means that your email messages will be organized in hierarchical folders to match your IMAP structure. If you’re using Gmail, use type = GmailMaildir.

The remote repository has a bit more going on:

  • For normal accounts, type = IMAP is appropriate. For Gmail, use: type = Gmail as this contains a bit of auto-configuration that will be handy for you.
  • You have some options for how to handle your password. I’m not going to get into that here, but read the config file documentation.
  • The folderfilter setting above excludes certain folders from being synced.

To find the cert_fingerprint setting for your server, run this command:

openssl s_client -connect imap.example.com:993 < /dev/null 2>/dev/null | openssl x509 -fingerprint -noout -in /dev/stdin

Of course, replace imap.example.com with your remotehost name. You’ll probably get the fingerprint with colons: XX:XX:.... Remove the colons for the purpose of this config file.

More accounts?

Add a new [Account ...] section and configure two repositories for each account as above.

Testing

Run offlineimap > offlineimaplog.txt to do an initial sync and see if it all works. Hopefully you should get a meaningful error if something goes wrong. You’ll see it in the offlineimaplog.txt file that’s created in the above command.

Running with cron

Right on, it’s all working great. Add this to your crontab:

47 5-21 * * * offlineimap && notmuch new

I haven’t gotten to notmuch in this tutorial yet, but the && notmuch new part just refreshes the tag and index database after offlineimap is done.

This will run offlineimap on the 47th minute of the hour, from 5:47 AM till 9:47 PM, each day.

A note about syncing

I’ve mentioned that since I have multiple computers that I spend a lot of time on (home and work) I want to have my email in the same state from machine to machine. When I get into how to use notmuch, you’ll see that I’m making changes to my email messages that are not going to be synced back to the IMAP server, so I need to use my home server (or an online VPS) to act as the central server.

To accomplish this, I use Syncthing to sync my mail folder (and thus, the notmuch database) to both my workstations. The only drawback is that syncing, though fast, is not immediate – so if I know there has been a change in the last 30 seconds or so (like, the cron above has just run), I’ll wait a minute or so to refresh my mailbox.

Up Next: configuring notmuch to work with OfflineIMAP

Categories
Productivity

Taking back email – Part 1: My basic setup

Recently, I decided to revamp my email setup. I don’t have too much going on with email, but it’s such an important method of communication that I needed to get it under control. I’m going to put together a several-post guide explaining my reasoning, technical details, and my new email process.

All posts in this series (they will be linked here as the posts go up):

I’ll be sharing config and usage tips along the way. I haven’t seen an in-depth tutorial to getting all these tools working together, so that’s one of my goals with this series.

Introduction

I have three email accounts that I use regularly. Each account duplicates some things – I have three inboxes, three archive folders, etc. Email clients take different approaches to showing mail from multiple accounts. Some, like Thunderbird, include an option to view “Unified folders” – i.e., all of your inboxes go together into one, etc. Others, like Evolution, prefer to keep things separate.

I pride myself on having a minimal email workflow:

  • Stuff comes in. I don’t want it to come in very often, so I like to only check once an hour.
  • If it’s actionable, I take a note of it outside my email system
  • Optionally, I leave the message in my Inbox to be replied to later
  • When I’m done with the message, it gets archived. I will search to find it again later.
  • I get some email that I never want to see again (notifications, etc). That gets deleted.

With three separate email accounts to handle this, though, it’s easy to miss things or to let email pile up in the Inbox (“I’ll take care of that later”). I decided I needed a system that would keep me a bit more accountable to myself. And logging into each account via webmail separately? Not an option. For me, this came with a change in the technical details of how I handle email.

The new setup: Basic email process

As I’m about to describe below, I aggregate all my email into one place now (I am aware that it’s three accounts – my setup will use the correct “from” address when replying to mail, for example).

  • All email is funneled into one inbox. I only check once an hour.
  • As soon as it comes to me, I’m going to get it out of the inbox in one of three ways:
  • Email I never want to see again is deleted directly (if applicable, I’ll unsubscribe first)
  • Email I want to keep, but is not actionable, is archived
  • Email I need to do something with (reply, finish a task, etc) is tagged “todo”.

After this, my inbox is empty.

When I’m done with a todo item, I archive it.

The new setup: Tools

OfflineIMAP

My home server (this would work on a remote VPS as well) uses OfflineIMAP to fetch mail from all my accounts once an hour. They go into a folder structure:

  • mail/
    • account-1/
      • subfolder/
      • another-subfolder/
    • account-2/
      • etc…

notmuch

notmuch is an email indexer and tagger. It’s used to create a searchable database of mails and to allow you to arbitrarily tag emails with different keywords.

After OfflineIMAP is finished pulling new email from the server, notmuch runs to make sure the mail database is up to date. Then, I use Syncthing to sync my mail collection and database to my home and work computers.

alot and khard

These are the programs I see and interface with in my process.

alot is an email client. It is specifically made for reading and working with databases created by notmuch. See the picture above to see alot in its full glory.

khard is a contacts manager which reads CardDav-generated contact databases. alot can use khard to autofill email addresses from your addressbook when you write a new message. Like my emails, my contacts are also synced from my server to my home and work computers.

I run alot, khard, and msmtp (below) locally on my work and home computers.

msmtp

msmtp is a command line tool which sends out mail through your SMTP server. After you write the email in alot, msmtp handles the actual sending process.

Coming up next

I’ll dive into how I set up and configured OfflineIMAP on my home server.