← Back to team overview

zim-wiki team mailing list archive

Re: ZIm and email integration

 

Hello,

On Fri, 09 Nov 2018, nik wrote:
> Hello list!
> I need suggestions, is there any possibility to integrate zim with e-mail
> client (I use Thunderbird)?
> 
> What I need is links in zim, so that when I click on the link email message
> opens.

I'm not using thunderbird but I have implemented a Debian package that
implements the "mid:" (Message ID) URL handler pointing back to some custom
script of me that opens the message indicated by its Message ID. And I
then put "mid:" links into zim (using a shortcut that starts the quicknote
plugin of zim to insert the link I created).

https://github.com/rhertzog/message-id-url
(you need to edit open-msgid to do something useful for you)

My mails are stored in my laptop (delivered via fetchmail+procmail) and
they are indexed with "notmuch".

I have this in my crontab:
*/15 * * * * notmuch new --quiet

I have a small ~/.notmuch-config:

[database]
path=/home/rhertzog/Mail
[user]
name=Raphaël Hertzog
primary_email=raphael@xxxxxxxxx
other_email=hertzog@xxxxxxxxxx
[new]
tags=unread;inbox;
ignore=

And I have this helper script taking a message-ID and opening my mailer
(mutt) at the right place:

----
#!/bin/sh

id=${1#<}
id=${id%>}
muttid=$(echo -n $id | tr "+=" ".." | sed -e 's/\$/\\\\$/g')

sleep 1

if [ -z "$id" ]; then
    echo "Usage: $0 <message-id>"
    exit 127
fi

for file in $(notmuch search --output=files "id:$id"); do
    mdir=$(dirname $(dirname $file))
    if ! echo "$mdir" | grep -q ^/; then
        mdir=~/$mdir
    fi
    #echo "$muttid"
    mutt -f $mdir -e "push \"/~i '$muttid'<ENTER><ENTER>\""
    exit 0
done

echo "notmuch did not find a message with message-id <$id>..." >&2
exit 1
----

I also attach a mail2zim.py script that takes an email in its standard input
and pre-generates a note to be added to zim.

This is all very "custom" but I couldn't work without this zim <-> email
integration.

Cheers,
-- 
Raphaël Hertzog ◈ Debian Developer

Support Debian LTS: https://www.freexian.com/services/debian-lts.html
Learn to master Debian: https://debian-handbook.info/get/
#!/usr/bin/python
# coding=utf-8

import sys
import email
import email.utils
import email.header
import re
import subprocess
from datetime import date

def decode_header(value):
    try:
        return unicode(email.header.make_header(email.header.decode_header(
                       value)))
    except UnicodeDecodeError:
        try:
            return unicode(value, "utf-8", "strict")
        except UnicodeDecodeError:
            return unicode(value, "iso-8859-1", "replace")

msg = email.message_from_file(sys.stdin)

msgid = msg["Message-ID"]
result = re.match("<([^>]+)>", msgid)
if result:
    msgid = result.group(1)

ts = email.utils.mktime_tz(email.utils.parsedate_tz(msg["date"]))

d = date.fromtimestamp(ts)

subject = decode_header(msg["Subject"])
(realname, address) = email.utils.parseaddr(msg["From"])
realname = decode_header(realname)
if realname == "Raphael Hertzog" or realname == u"Raphaël Hertzog":
    (realname, address) = email.utils.getaddresses(msg.get_all("to", []))[0]
    realname = decode_header(realname)

if not realname:
    realname = address

zim_text = u"[ ] [[mid:%s|%s]] (%s)\n" % (msgid, realname, subject)
#zim_text = zim_text + u"[ ] Relance [[mid:%s|%s]] à propos de //%s//\n" \
#                      % (msgid, realname, subject)
#zim_text = zim_text + u"[ ] Classer pour référence: [[mid:%s|%s]] (%s, %s)" \
#                      % (msgid, subject, realname, d.strftime("%Y-%m-%d"))

#print zim_text

zim = subprocess.Popen(["zim", "--plugin", "quicknote",
                        "notebook=file:///home/rhertzog/Dropbox/Notes",
                        "namespace=:Gestion de ma vie:Tâches:INBOX",
                        "input=stdin"],
                       stdin=subprocess.PIPE)
zim.communicate(input=zim_text.encode('utf8'))


References