sqrtminusone.github.io/public/configs/mail/index.html

604 lines
43 KiB
HTML

<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Mail</title>
<meta name="description" content="Freedom is a state of mind">
<meta name="author" content='SqrtMinusOne'>
<link href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<link rel="stylesheet" href="/sass/researcher.css">
<link rel="icon" type="image/ico" href="https://sqrtminusone.xyz/favicon.ico">
</head>
<body><div class="container mt-5">
<nav class="navbar navbar-expand-sm flex-column flex-sm-row text-nowrap p-0">
<a class="navbar-brand mx-0 mr-sm-auto" href="https://sqrtminusone.xyz/">
SqrtMinusOne
</a>
<div class="navbar-nav flex-row flex-wrap justify-content-center">
<a class="nav-item nav-link" href="/">
Index
</a>
<span class="nav-item navbar-text mx-1">/</span>
<a class="nav-item nav-link" href="/posts/">
Posts
</a>
<span class="nav-item navbar-text mx-1">/</span>
<a class="nav-item nav-link" href="/configs/readme">
Configs
</a>
</div>
</nav>
</div>
<hr>
<div id="content">
<div class="container">
<p>My email configration. Currently uses <a href="https://github.com/gauteh/lieer">lieer</a> to fetch emails from Gmail, <a href="http://davmail.sourceforge.net/">davmail</a> &amp; <a href="http://www.offlineimap.org/">offlineimap</a> to fetch emails from MS Exchange, <a href="https://notmuchmail.org/">notmuch</a> to index, <a href="https://marlam.de/msmtp/">msmtp</a> to send emails. Also using notmuch frontend from Emacs.</p>
<p>My problem with any particular mail setup was that I use Gmail labels quite extensively, and handling these over IMAP is rather awkward. Notmuch seems to be the only software that provides the same first-class support for labels.</p>
<p>But I also have an Exchange account, with which I communicate via IMAP/SMTP adapter, and in this case, I synchronize notmuch tags and IMAP folders.</p>
<p>References:</p>
<ul>
<li><a href="https://sqrtminusone.xyz/posts/2021-02-27-gmail/">My post</a> about email configuration. I wrote it some time ago, but the general idea remains.</li>
</ul>
<div class="ox-hugo-toc toc">
<div></div>
<div class="heading">Table of Contents</div>
<ul>
<li><a href="#lieer">Lieer</a></li>
<li><a href="#davmail">DavMail</a></li>
<li><a href="#offlineimap">OfflineIMAP</a></li>
<li><a href="#notmuch">Notmuch</a>
<ul>
<li><a href="#config">Config</a></li>
<li><a href="#hooks">Hooks</a>
<ul>
<li><a href="#pre-new"><code>pre_new</code></a></li>
<li><a href="#post-new"><code>post_new</code></a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#sync-script">Sync script</a></li>
<li><a href="#msmtp">MSMTP</a></li>
<li><a href="#emacs">Emacs</a>
<ul>
<li><a href="#saved-filters-and-keybindings">Saved filters and keybindings</a></li>
<li><a href="#signing-messages">Signing messages</a></li>
</ul>
</li>
<li><a href="#mailcap">mailcap</a></li>
<li><a href="#guix-settings">Guix settings</a></li>
</ul>
</div>
<!--endtoc-->
<h2 id="lieer">Lieer</h2>
<table>
<thead>
<tr>
<th>Guix dependency</th>
</tr>
</thead>
<tbody>
<tr>
<td>python-lieer</td>
</tr>
</tbody>
</table>
<p>Lieer is a program to link up Gmail and notmuch. Basically, it downloads mail from Gmail via API, stores them in Maildir, and synchronizes labels with notmuch.</p>
<p>I have a separate directory in my <code>~/Mail</code> for each address. To init lieer, run the following command in the directory:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text">gmi init &lt;address&gt;
</code></pre></div><p>After which the settings will be stored in <code>gmailieer.json</code> and the credentials in <code>.credentials.gmailieer.json</code>. The latter file is stored encrypted.</p>
<p>My preferred settings:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text">gmi set --replace-slash-with-dot
gmi set --ignore-tags-local new
</code></pre></div><p>Running <code>gmi sync</code> in the required directory performs the synchronization. The first sync takes a while, the subsequent syncs are pretty fast.</p>
<h2 id="davmail">DavMail</h2>
<p>is a gateway between MS Exchange and the rest of the world, which uses IMAP/SMTP/LDAP/etc. As I have one corporate MS Exchange address, this is just the program I need. As of yet, it isn&rsquo;t packaged for Guix, but it&rsquo;s easy enough to download.</p>
<p>It has a GUI mode, but I prefer headless config.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#a6e22e">davmail.server</span><span style="color:#f92672">=</span><span style="color:#e6db74">true</span>
<span style="color:#a6e22e">davmail.mode</span><span style="color:#f92672">=</span><span style="color:#e6db74">Auto</span>
<span style="color:#a6e22e">davmail.url</span><span style="color:#f92672">=</span><span style="color:#e6db74">https://mail.etu.ru/owa/</span>
<span style="color:#a6e22e">davmail.caldavPort</span><span style="color:#f92672">=</span><span style="color:#e6db74">1080</span>
<span style="color:#a6e22e">davmail.imapPort</span><span style="color:#f92672">=</span><span style="color:#e6db74">1143</span>
<span style="color:#a6e22e">davmail.ldapPort</span><span style="color:#f92672">=</span><span style="color:#e6db74">1389</span>
<span style="color:#a6e22e">davmail.popPort</span><span style="color:#f92672">=</span><span style="color:#e6db74">1110</span>
<span style="color:#a6e22e">davmail.smtpPort</span><span style="color:#f92672">=</span><span style="color:#e6db74">1025</span>
<span style="color:#a6e22e">davmail.imapAutoExpunge</span><span style="color:#f92672">=</span><span style="color:#e6db74">false</span>
<span style="color:#a6e22e">davmail.enableKeepalive</span><span style="color:#f92672">=</span><span style="color:#e6db74">false</span>
</code></pre></div><p>Also it&rsquo;s a bit of problem to get it launched as it looks for some jars in pwd, so here is a script.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">cd $HOME/bin/davmail-6.0.0-3375
./davmail davmail.properties
</code></pre></div><p>Shepherd service is defined in <a href="/configs/desktop/">Desktop.org</a>.</p>
<h2 id="offlineimap">OfflineIMAP</h2>
<table>
<thead>
<tr>
<th>Guix dependency</th>
</tr>
</thead>
<tbody>
<tr>
<td>offlineimap</td>
</tr>
</tbody>
</table>
<p><a href="https://github.com/OfflineIMAP/offlineimap">OfflineIMAP</a> is a program that can synchronize IMAP mailbox and Maildir. Lieer does everything by itself, but my pirate Exchange IMAP needs this program. There is also <a href="https://isync.sourceforge.io/">isync</a>, but I had some weird issues with duplicate UIDs, which don&rsquo;t occur for OfflineIMAP.</p>
<p>I have a few options for setting a username and password. First, I can run <code>pass</code> in <code>remotepasswordeval</code>, and this is fine, but it will keep by keyring unlocked because I want to run <code>offlineimap</code> every couple of minutes.</p>
<p>Another option is to use noweb and not push the file below to the version control. Then I have a plaintext password of email on my computer, but I think it&rsquo;s a lesser evil than the entire keyring.</p>
<p>I would use <code>password-store-get</code> from password-store.el, but I want this to be able to run without any 3rd party packages, so it&rsquo;s just bash.</p>
<p><a id="code-snippet--mail-username"></a></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">pass show Job/Infrastructure/pvkorytov@etu.ru | sed -n <span style="color:#e6db74">&#39;s/username: //;2p&#39;</span>
</code></pre></div><p><a id="code-snippet--mail-password"></a></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">pass show Job/Infrastructure/pvkorytov@etu.ru | head -n <span style="color:#ae81ff">1</span>
</code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#66d9ef">[general]</span>
<span style="color:#a6e22e">accounts</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">pvkorytov</span>
<span style="color:#66d9ef">[Account pvkorytov]</span>
<span style="color:#a6e22e">localrepository</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">pvkorytov-local</span>
<span style="color:#a6e22e">remoterepository</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">pvkorytov-remote</span>
<span style="color:#66d9ef">[Repository pvkorytov-local]</span>
<span style="color:#a6e22e">type</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">Maildir</span>
<span style="color:#a6e22e">localfolders</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">~/Mail/pvkorytov_etu/</span>
<span style="color:#66d9ef">[Repository pvkorytov-remote]</span>
<span style="color:#a6e22e">type</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">IMAP</span>
<span style="color:#a6e22e">remotehost</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">localhost</span>
<span style="color:#a6e22e">remoteuser</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&lt;&lt;mail-username()&gt;&gt;</span>
<span style="color:#a6e22e">remotepass</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&lt;&lt;mail-password()&gt;&gt;</span>
<span style="color:#a6e22e">remoteport</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">1143</span>
<span style="color:#a6e22e">starttls</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">no</span>
<span style="color:#a6e22e">ssl</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">no</span>
</code></pre></div><h2 id="notmuch">Notmuch</h2>
<table>
<thead>
<tr>
<th>Guix dependency</th>
</tr>
</thead>
<tbody>
<tr>
<td>notmuch</td>
</tr>
<tr>
<td>parallel</td>
</tr>
</tbody>
</table>
<p>Notmuch is an email indexer program, which handles labels in a way somewhat like Gmail. It also provides a frontend for Emacs, but it&rsquo;s not the only one available.</p>
<h3 id="config">Config</h3>
<p>Not much is going on here.</p>
<p>First, the database path.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#66d9ef">[database]</span>
<span style="color:#a6e22e">path</span><span style="color:#f92672">=</span><span style="color:#e6db74">/home/pavel/Mail</span>
</code></pre></div><p>My name and list of emails. It&rsquo;s not like it&rsquo;s a secret anyhow.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#66d9ef">[user]</span>
<span style="color:#a6e22e">name</span><span style="color:#f92672">=</span><span style="color:#e6db74">Pavel Korytov</span>
<span style="color:#a6e22e">primary_email</span><span style="color:#f92672">=</span><span style="color:#e6db74">thexcloud@gmail.com</span>
<span style="color:#a6e22e">other_email</span><span style="color:#f92672">=</span><span style="color:#e6db74">progin6304@gmail.com;pvkorytov@etu.ru</span>
</code></pre></div><p>A list of tags which will be added by <code>notmuch new</code> and directory names which will be ignored by <code>notmuch new</code>.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#66d9ef">[new]</span>
<span style="color:#a6e22e">tags</span><span style="color:#f92672">=</span><span style="color:#e6db74">new;</span>
<span style="color:#a6e22e">ignore</span><span style="color:#f92672">=</span><span style="color:#e6db74">.osync_workdir;.mbsyncstate;.uidvalidity;.lock;/.*gmailieer\.json.*/</span>
</code></pre></div><p>Exclude these tags from search by default.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#66d9ef">[search]</span>
<span style="color:#a6e22e">exclude_tags</span><span style="color:#f92672">=</span><span style="color:#e6db74">trash;spam;</span>
</code></pre></div><p>Maildir compatibility.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-ini" data-lang="ini"><span style="color:#66d9ef">[maildir]</span>
<span style="color:#a6e22e">synchronize_flags</span><span style="color:#f92672">=</span><span style="color:#e6db74">true</span>
</code></pre></div><h3 id="hooks">Hooks</h3>
<p>Now we have to link up lieer &amp; Maildir and with notmuch. This is done via the notmuch hook system, which allows running custom scripts before and after any command.</p>
<p>With lieer and Gmail, it is enough to simply run the program, because Gmail has first-class support for tags. Maildir does not, so I decide to synchronize notmuch tags and IMAP folders. In essence, the idea is to:</p>
<ul>
<li>move emails to their folders by tags <em>before</em> the synchronization</li>
<li>tag mails by their folders <em>after</em> the synchronization</li>
</ul>
<p>The problem is that with that approach one email can have only one tag, but it&rsquo;s better than nothing.</p>
<p>So, here are the rules which match tags &amp; folders:</p>
<p><a id="table--pvkorytov-tags"></a></p>
<table>
<thead>
<tr>
<th>tag</th>
<th>folder</th>
</tr>
</thead>
<tbody>
<tr>
<td>inbox</td>
<td>INBOX</td>
</tr>
<tr>
<td>sent</td>
<td>Sent</td>
</tr>
<tr>
<td>spam</td>
<td>Junk</td>
</tr>
<tr>
<td>trash</td>
<td>Trash</td>
</tr>
<tr>
<td>job.digital</td>
<td>Job_Digital</td>
</tr>
<tr>
<td>job.digital.docs</td>
<td>Job_Digital.Docs</td>
</tr>
<tr>
<td>job.digital.support</td>
<td>Job_Digital.Support</td>
</tr>
<tr>
<td>job.digital.superservice</td>
<td>Job_Digital.Superservice</td>
</tr>
</tbody>
</table>
<p>And below is a noweb function, which generates the following commands for notmuch to execute:</p>
<ul>
<li><em>before</em> sync:
<ul>
<li><code>notmuch search --output files &quot;NOT path:[PATH] AND tag:[TAG] AND tag:[ROOT_TAG]&quot; | xargs -I ! mv ! [PATH]</code>
Move emails with <code>TAG</code> but outside the matching <code>PATH</code> to the latter</li>
<li><code>notmuch search --output=files &quot;NOT path:[ARCHIVE_PATH] AND tag:[ROOT_TAG] AND NOT tag:[TAG1] ... AND NOT tag:[TAGN]&quot; | xargs -I ! mv ! [ARCHIVE_PATH]</code>
Move untagged emails to the <code>ARCHIVE_PATH</code></li>
</ul>
</li>
<li><em>after</em> sync:
<ul>
<li><code>notmuch tag +[TAG] &quot;path:[PATH] AND NOT tag:[TAG]&quot;</code>
Tag emails in <code>PATH</code> which do not yet have the matching <code>TAG</code></li>
<li><code>notmuch tag -[TAG] &quot;NOT path:[PATH] AND tag:[TAG] AND tag:[ROOT_TAG]&quot;</code>
Remove <code>TAG</code> from emails which are outside the matching <code>PATH</code></li>
</ul>
</li>
</ul>
<p>These rules are getting included in the respective hooks.</p>
<p><a id="code-snippet--mail-tags"></a></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(setq my/maildir-root <span style="color:#e6db74">&#34;~/Mail&#34;</span>)
(let ((rules <span style="color:#f92672">&#39;</span>()))
(dolist (row tags)
(let ((tag (<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">0</span> row))
(folder (<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">1</span> row)))
(unless (string-empty-p make_tag)
(add-to-list
<span style="color:#e6db74">&#39;rules</span>
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34;notmuch tag +%s \&#34;path:%s/%s/cur/** AND NOT tag:%s\&#34;&#34;</span>
tag root folder tag)
<span style="color:#66d9ef">t</span>))
(unless (string-empty-p remove)
(add-to-list
<span style="color:#e6db74">&#39;rules</span>
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34;notmuch tag -%s \&#34;NOT path:%s/%s/cur/** AND tag:%s AND tag:%s\&#34;&#34;</span>
tag root folder tag root_tag)
<span style="color:#66d9ef">t</span>))
(unless (string-empty-p move)
(add-to-list
<span style="color:#e6db74">&#39;rules</span>
(<span style="color:#a6e22e">concat</span>
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34;notmuch search --output=files \&#34;NOT path:%s/%s/cur/** AND tag:%s AND tag:%s\&#34;&#34;</span>
root folder tag root_tag)
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34; | xargs -I ! mv ! %s/%s/%s/cur/&#34;</span> my/maildir-root root folder))
<span style="color:#66d9ef">t</span>))))
(unless (string-empty-p archive_root)
(add-to-list
<span style="color:#e6db74">&#39;rules</span>
(<span style="color:#a6e22e">concat</span>
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34;notmuch search --output=files \&#34;NOT path:%s/%s/cur/** AND %s AND tag:%s\&#34;&#34;</span>
root archive_root
(<span style="color:#a6e22e">mapconcat</span>
(lambda (row)
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34;NOT tag:%s&#34;</span> (<span style="color:#a6e22e">car</span> row)))
tags
<span style="color:#e6db74">&#34; AND &#34;</span>)
root_tag)
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34; | xargs -I ! mv ! %s/%s/%s/cur/&#34;</span> my/maildir-root root archive_root))
<span style="color:#66d9ef">t</span>))
(string-join rules <span style="color:#e6db74">&#34;\n&#34;</span>))
</code></pre></div><h4 id="pre-new"><code>pre_new</code></h4>
<p>This hook runs fetch from Gmail &amp; offlineimap in parallel before the <code>notmuch new</code> command. The <code>parallel</code> command is provided by <a href="https://www.gnu.org/software/parallel/">GNU Parallel</a>.</p>
<p>It isn&rsquo;t necessary to run <code>cd</code> for offlineimap, but it&rsquo;s easier to write that way.</p>
<p><a id="code-snippet--pre-new-pvkorytov-tags"></a></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(my/mail-format-tags-rules tags <span style="color:#e6db74">&#34;pvkorytov_etu&#34;</span> <span style="color:#e6db74">&#34;pvkorytov&#34;</span> <span style="color:#66d9ef">nil</span> <span style="color:#66d9ef">nil</span> <span style="color:#66d9ef">t</span> <span style="color:#e6db74">&#34;Archive&#34;</span>)
</code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#75715e"># GMI=&#34;/home/pavel/Programs/miniconda3/envs/mail/bin/gmi&#34;</span>
GMI<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;gmi&#34;</span>
echo <span style="color:#e6db74">&#34;Running pre-new filters&#34;</span>
<span style="color:#e6db74">&lt;&lt;mail-tags(move=&#34;t&#34;,archive_root=&#34;Archive&#34;)&gt;&gt;
</span><span style="color:#e6db74">echo &#34;Pre-new filters done&#34;
</span><span style="color:#e6db74">
</span><span style="color:#e6db74">parallel --link -j0 &#34;(cd /home/pavel/Mail/{1}/ &amp;&amp; {2} {3})&#34; ::: thexcloud progin6304 pvkorytov_etu ::: &#34;$GMI&#34; &#34;$GMI&#34; &#34;offlineima</span>p<span style="color:#e6db74">&#34; ::: sync sync &#34;&#34;
</span></code></pre></div><h4 id="post-new"><code>post_new</code></h4>
<p>And this hook tags different mailboxes with different tags.</p>
<p><a id="code-snippet--post-new-pvkorytov-tags"></a></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(my/mail-format-tags-rules tags <span style="color:#e6db74">&#34;pvkorytov_etu&#34;</span> <span style="color:#e6db74">&#34;pvkorytov&#34;</span> <span style="color:#66d9ef">t</span> <span style="color:#66d9ef">t</span>)
</code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">notmuch tag +main <span style="color:#e6db74">&#34;path:thexcloud/** AND tag:new&#34;</span>
notmuch tag +progin <span style="color:#e6db74">&#34;path:progin6304/** AND tag:new&#34;</span>
notmuch tag +pvkorytov <span style="color:#e6db74">&#34;path:pvkorytov_etu/** AND tag:new&#34;</span>
echo <span style="color:#e6db74">&#34;Running post-new filters&#34;</span>
<span style="color:#e6db74">&lt;&lt;mail-tags(ma</span>ke_tag<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;t&#34;</span>,remove<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;t&#34;</span><span style="color:#f92672">)</span>&gt;&gt;
echo <span style="color:#e6db74">&#34;Post-new filters done&#34;</span>
notmuch tag -new <span style="color:#e6db74">&#34;tag:new&#34;</span>
</code></pre></div><h2 id="sync-script">Sync script</h2>
<p>A script to run <code>notmuch new</code> and push a notification if there is new mail.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">export DISPLAY<span style="color:#f92672">=</span>:0
CHECK_FILE<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;/home/pavel/Mail/.last_check&#34;</span>
QUERY<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;tag:unread&#34;</span>
ALL_QUERY<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;tag:unread&#34;</span>
<span style="color:#66d9ef">if</span> <span style="color:#f92672">[</span> -f <span style="color:#e6db74">&#34;</span>$CHECK_FILE<span style="color:#e6db74">&#34;</span> <span style="color:#f92672">]</span>; <span style="color:#66d9ef">then</span>
DATE<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>cat <span style="color:#e6db74">&#34;</span>$CHECK_FILE<span style="color:#e6db74">&#34;</span><span style="color:#66d9ef">)</span>
QUERY<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span>$QUERY<span style="color:#e6db74"> and date:@</span>$DATE<span style="color:#e6db74">..&#34;</span>
<span style="color:#66d9ef">fi</span>
notmuch new
NEW_UNREAD<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>notmuch count <span style="color:#e6db74">&#34;</span>$QUERY<span style="color:#e6db74">&#34;</span><span style="color:#66d9ef">)</span>
ALL_UNREAD<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>notmuch count <span style="color:#e6db74">&#34;</span>$ALL_QUERY<span style="color:#e6db74">&#34;</span><span style="color:#66d9ef">)</span>
<span style="color:#66d9ef">if</span> <span style="color:#f92672">[</span> $NEW_UNREAD -gt <span style="color:#ae81ff">0</span> <span style="color:#f92672">]</span>; <span style="color:#66d9ef">then</span>
MAIN_UNREAD<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>notmuch count <span style="color:#e6db74">&#34;tag:unread AND tag:main&#34;</span><span style="color:#66d9ef">)</span>
PROGIN_UNREAD<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>notmuch count <span style="color:#e6db74">&#34;tag:unread AND tag:progin&#34;</span><span style="color:#66d9ef">)</span>
ETU_UNREAD<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>notmuch count <span style="color:#e6db74">&#34;tag:unread AND tag:pvkorytov&#34;</span><span style="color:#66d9ef">)</span>
read -r -d <span style="color:#e6db74">&#39;&#39;</span> NOTIFICATION <span style="color:#e6db74">&lt;&lt;EOM
</span><span style="color:#e6db74">$NEW_UNREAD new messages
</span><span style="color:#e6db74">$MAIN_UNREAD thexcloud@gmail.com
</span><span style="color:#e6db74">$PROGIN_UNREAD progin6304@gmail.com
</span><span style="color:#e6db74">$ETU_UNREAD pvkorytov@etu.ru
</span><span style="color:#e6db74">$ALL_UNREAD total
</span><span style="color:#e6db74">EOM</span>
notify-send <span style="color:#e6db74">&#34;New Mail&#34;</span> <span style="color:#e6db74">&#34;</span>$NOTIFICATION<span style="color:#e6db74">&#34;</span>
<span style="color:#66d9ef">fi</span>
echo <span style="color:#e6db74">&#34;</span><span style="color:#66d9ef">$(</span>date +%s<span style="color:#66d9ef">)</span><span style="color:#e6db74">&#34;</span> &gt; $CHECK_FILE
</code></pre></div><p>The script is ran via GNU Mcron every 5 minutes.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-scheme" data-lang="scheme">(<span style="color:#a6e22e">job</span> <span style="color:#e6db74">&#34;*/5 * * * * &#34;</span> <span style="color:#e6db74">&#34;~/bin/scripts/check-email&#34;</span>)
</code></pre></div><h2 id="msmtp">MSMTP</h2>
<table>
<thead>
<tr>
<th>Guix dependency</th>
</tr>
</thead>
<tbody>
<tr>
<td>msmtp</td>
</tr>
</tbody>
</table>
<p>Sending emails can be done with MSMTP. It automatially chooses the email address and server based on the contents of the message, which is handy if there are multiple mailboxes to be managed.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vim" data-lang="vim"><span style="color:#a6e22e">defaults</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">auth</span> <span style="color:#a6e22e">on</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">tls</span> <span style="color:#a6e22e">on</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">tls_trust_file</span> <span style="color:#e6db74">/etc/</span><span style="color:#a6e22e">ssl</span><span style="color:#e6db74">/certs/</span><span style="color:#a6e22e">ca</span>-<span style="color:#a6e22e">certificates</span>.<span style="color:#a6e22e">crt</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">logfile</span> ~/.<span style="color:#a6e22e">msmtp</span>.<span style="color:#a6e22e">log</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">account</span> <span style="color:#a6e22e">main</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">host</span> <span style="color:#a6e22e">smtp</span>.<span style="color:#a6e22e">gmail</span>.<span style="color:#a6e22e">com</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">port</span> <span style="color:#ae81ff">587</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">from</span> <span style="color:#a6e22e">thexcloud</span>@<span style="color:#a6e22e">gmail</span>.<span style="color:#a6e22e">com</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">user</span> <span style="color:#a6e22e">thexcloud</span>@<span style="color:#a6e22e">gmail</span>.<span style="color:#a6e22e">com</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">passwordeval</span> <span style="color:#e6db74">&#34;pass show My_Online/APIs/google-main-app-password | head -n 1&#34;</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">account</span> <span style="color:#a6e22e">progin</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">host</span> <span style="color:#a6e22e">smtp</span>.<span style="color:#a6e22e">gmail</span>.<span style="color:#a6e22e">com</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">port</span> <span style="color:#ae81ff">587</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">from</span> <span style="color:#a6e22e">progin6304</span>@<span style="color:#a6e22e">gmail</span>.<span style="color:#a6e22e">com</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">user</span> <span style="color:#a6e22e">progin6304</span>@<span style="color:#a6e22e">gmail</span>.<span style="color:#a6e22e">com</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">passwordeval</span> <span style="color:#e6db74">&#34;pass show My_Online/ETU/progin6304@gmail.com | head -n 1&#34;</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">account</span> <span style="color:#a6e22e">pvkorytov</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">tls</span> <span style="color:#a6e22e">off</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">auth</span> <span style="color:#a6e22e">plain</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">host</span> <span style="color:#a6e22e">localhost</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">port</span> <span style="color:#ae81ff">1025</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">from</span> <span style="color:#a6e22e">pvkorytov</span>@<span style="color:#a6e22e">etu</span>.<span style="color:#a6e22e">ru</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">user</span> <span style="color:#a6e22e">pvkorytov</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">passwordeval</span> <span style="color:#e6db74">&#34;pass show Job/Infrastructure/pvkorytov@etu.ru | head -n 1&#34;</span><span style="color:#960050;background-color:#1e0010">
</span></code></pre></div><h2 id="emacs">Emacs</h2>
<p>Finally, Emacs configuration. Let&rsquo;s start with some variables:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(setq user-mail-address <span style="color:#e6db74">&#34;thexcloud@gmail.com&#34;</span>)
(setq <span style="color:#a6e22e">user-full-name</span> <span style="color:#e6db74">&#34;Pavel Korytov&#34;</span>)
</code></pre></div><p>Then, the problem with my Guix setup is that Emacs by default doesn&rsquo;t see the elisp files of notmuch, so here is a small workaround:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(let ((default-directory <span style="color:#e6db74">&#34;/home/pavel/.guix-extra-profiles/mail/mail/share/emacs/site-lisp&#34;</span>))
(normal-top-level-add-subdirs-to-load-path))
</code></pre></div><p>Finally the proper notmuch settings:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(use-package notmuch
<span style="color:#75715e">;; :ensure nil</span>
:commands (notmuch notmuch-search)
:config
(setq mail-specify-envelope-from <span style="color:#66d9ef">t</span>)
(setq message-sendmail-envelope-from <span style="color:#e6db74">&#39;header</span>)
(setq mail-envelope-from <span style="color:#e6db74">&#39;header</span>)
(setq notmuch-always-prompt-for-sender <span style="color:#66d9ef">t</span>)
(setq sendmail-program (executable-find <span style="color:#e6db74">&#34;msmtp&#34;</span>))
(setq send-mail-function <span style="color:#a6e22e">#&#39;</span>sendmail-send-it)
(setq mml-secure-openpgp-sign-with-sender <span style="color:#66d9ef">t</span>)
(setq notmuch-mua-user-agent-function <span style="color:#e6db74">&#39;notmuch-mua-user-agent-full</span>)
(add-hook <span style="color:#e6db74">&#39;notmuch-hello-mode-hook</span>
(lambda () (display-line-numbers-mode <span style="color:#ae81ff">0</span>))))
</code></pre></div><p>The file to which this is tangled is read in the init.el.</p>
<h3 id="saved-filters-and-keybindings">Saved filters and keybindings</h3>
<p>I want to have the saved filters available in both notmuch interface as as keybindings. So a bit more of abusing org tables.</p>
<p>Root keybindings:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(my-leader-def
:infix <span style="color:#e6db74">&#34;am&#34;</span>
<span style="color:#e6db74">&#34;&#34;</span> <span style="color:#f92672">&#39;</span>(:which-key <span style="color:#e6db74">&#34;notmuch&#34;</span>)
<span style="color:#e6db74">&#34;m&#34;</span> <span style="color:#e6db74">&#39;notmuch</span>)
</code></pre></div><p><a id="table--root-tags"></a></p>
<table>
<thead>
<tr>
<th>Root tag</th>
<th>Prefix</th>
<th>Keybinding description</th>
</tr>
</thead>
<tbody>
<tr>
<td>main</td>
<td>t</td>
<td><a href="mailto:thexcloud@gmail.com">thexcloud@gmail.com</a></td>
</tr>
<tr>
<td>progin</td>
<td>p</td>
<td><a href="mailto:progin6304@gmail.com">progin6304@gmail.com</a></td>
</tr>
<tr>
<td>pvkorytov</td>
<td>e</td>
<td><a href="mailto:pvkorytov@etu.ru">pvkorytov@etu.ru</a></td>
</tr>
</tbody>
</table>
<p><a id="table--filter-tags"></a></p>
<table>
<thead>
<tr>
<th>Tag</th>
<th>Prefix</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>inbox</td>
<td>i</td>
<td>inbox</td>
</tr>
<tr>
<td>unread</td>
<td>u</td>
<td>unread</td>
</tr>
<tr>
<td>sent</td>
<td>s</td>
<td>sent</td>
</tr>
<tr>
<td></td>
<td>a</td>
<td>all mail</td>
</tr>
</tbody>
</table>
<p>The following formats the tables above to a proper syntax for <code>setq notmuch-saved-searches</code>:</p>
<p><a id="code-snippet--format-notmuch-saved-searches"></a></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(let ((searches <span style="color:#f92672">&#39;</span>()))
(dolist (root_tag root_tags)
(dolist (tag filter_tags)
(add-to-list
<span style="color:#e6db74">&#39;searches</span>
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34;(:name \&#34;%s\&#34; :query \&#34;%s\&#34;)&#34;</span>
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34;%s (%s)&#34;</span>
(<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">0</span> root_tag)
(<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">2</span> tag))
(<span style="color:#a6e22e">concat</span> <span style="color:#e6db74">&#34;tag:&#34;</span> (<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">0</span> root_tag)
(unless (string-empty-p (<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">0</span> tag))
(<span style="color:#a6e22e">concat</span> <span style="color:#e6db74">&#34; AND tag:&#34;</span> (<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">0</span> tag)))))
<span style="color:#66d9ef">t</span>)))
(string-join searches <span style="color:#e6db74">&#34;\n&#34;</span>))
</code></pre></div><p>And the following does the same for my general.el definer:</p>
<p><a id="code-snippet--format-notmuch-keybindings"></a></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(let ((bindings <span style="color:#f92672">&#39;</span>()))
(dolist (root_tag root_tags)
(add-to-list
<span style="color:#e6db74">&#39;bindings</span>
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34;\&#34;%s\&#34; &#39;(:which-key \&#34;%s\&#34;)&#34;</span>
(<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">1</span> root_tag)
(<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">2</span> root_tag))
<span style="color:#66d9ef">t</span>)
(dolist (tag filter_tags)
(add-to-list
<span style="color:#e6db74">&#39;bindings</span>
(<span style="color:#a6e22e">format</span> <span style="color:#e6db74">&#34;\&#34;%s\&#34; &#39;((lambda () (interactive) (notmuch-search \&#34;%s\&#34;)) :which-key \&#34;%s\&#34;)&#34;</span>
(<span style="color:#a6e22e">concat</span> (<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">1</span> root_tag) (<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">1</span> tag))
(<span style="color:#a6e22e">concat</span> <span style="color:#e6db74">&#34;tag:&#34;</span> (<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">0</span> root_tag)
(unless (string-empty-p (<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">0</span> tag))
(<span style="color:#a6e22e">concat</span> <span style="color:#e6db74">&#34; AND tag:&#34;</span> (<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">0</span> tag))))
(<span style="color:#a6e22e">nth</span> <span style="color:#ae81ff">2</span> tag))
<span style="color:#66d9ef">t</span>)))
(string-join bindings <span style="color:#e6db74">&#34;\n&#34;</span>))
</code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(setq notmuch-saved-searches
<span style="color:#f92672">&#39;</span>((:name <span style="color:#e6db74">&#34;drafts&#34;</span> :query <span style="color:#e6db74">&#34;tag:draft&#34;</span>)
&lt;&lt;format-notmuch-saved-searches()&gt;&gt;))
(my-leader-def
:infix <span style="color:#e6db74">&#34;am&#34;</span>
&lt;&lt;format-notmuch-keybindings()&gt;&gt;)
</code></pre></div><h3 id="signing-messages">Signing messages</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(with-eval-after-load <span style="color:#e6db74">&#39;notmuch</span>
(add-hook <span style="color:#e6db74">&#39;message-setup-hook</span> <span style="color:#e6db74">&#39;mml-secure-sign-pgpmime</span>))
(setq mml-secure-key-preferences
<span style="color:#f92672">&#39;</span>((OpenPGP
(sign
(<span style="color:#e6db74">&#34;thexcloud@gmail.com&#34;</span> <span style="color:#e6db74">&#34;914472A1FD6775C166F96EBEED739ADF81C78160&#34;</span>))
(encrypt))
(CMS
(sign)
(encrypt))))
</code></pre></div><h2 id="mailcap">mailcap</h2>
<p>mailcap file is a file which defines how to read to different MIME types. Notmuch also uses it, so why not keep it here.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text">audio/*; mpc add %s
image/*; feh %s
application/msword; /usr/bin/xdg-open %s
application/pdf; zathura %s
application/postscript ; zathura %s
text/html; firefox %s
</code></pre></div><h2 id="guix-settings">Guix settings</h2>
<p><a id="code-snippet--packages"></a></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-emacs-lisp" data-lang="emacs-lisp">(my/format-guix-dependencies)
</code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-scheme" data-lang="scheme">(<span style="color:#a6e22e">specifications-&gt;manifest</span>
<span style="color:#f92672">&#39;</span>(
&lt;&lt;packages()&gt;&gt;))
</code></pre></div>
</div>
</div><div id="footer" class="mb-5">
<hr>
<div class="container text-center">
<a href="https://sqrtminusone.xyz/"><small>Pavel Korytov, 2021</small></a>
</div>
</div>
</body>
</html>