Polybar new email tracking

Hello,

I've got together a nice script for tracking number of new emails with polybar. If anyone is interested fell free to test it.
It is nice that polybar allows actions on mouse click. This way I can open Thunderbird directly by clicking on the icon.
It can be probably easily modified for conky or some such.

Here it how it looks like for 2 email tracking.
email

Make sure .netrc file has 600 or 400 permission so no other user can read it.

email.sh
#!/bin/bash
##
## get IMAP unread emails and lauch email manager
## servers and login is in .netrc file in this format:
##
##   machine imap.gmail.com
##   login johndoe
##   password johnspassword
##
## don't use # for server names since the script will take it as a comment
##
## dependencies thunderbird curl networkmanager
##       networkmanager is for online check in the base file
##

case "$1" in
--primary)
    # lauch email manager
    thunderbird&
    ;;

--get)
    # get number of unred emails from all servers in .netrc file
    NETRC="$HOME/.config/polybar/scripts/.netrc"
    MAIL_COUNTER="$HOME/.cache/mail_counter"
    if [[ -f "$NETRC" ]]; then

        SRVR_LIST=$(grep -n "machine" "$NETRC" | grep -v '#' | sed "s/:/ /" | sort -nk1 | awk '{ print $3 }')


        COUNTER="0"
        OUTPUT=""
        for SERVER in $SRVR_LIST; do
            UNREAD=$(curl -sf --netrc-file "$NETRC" -X "STATUS INBOX (UNSEEN)" "imaps://$SERVER" | tr -d -c "[:digit:]")

            if [[ $COUNTER -eq "0" ]]; then
                OUTPUT="$UNREAD"
            else
                OUTPUT="$OUTPUT|$UNREAD"
            fi

            COUNTER=$(($COUNTER+1))
        done

        # compare output with last querry stored in memory and notify if there is a change
        if [[ -f "${MAIL_COUNTER}" ]]; then
            cnt="$(head -n1 ${MAIL_COUNTER})"
            if [[ "${cnt}" != "${OUTPUT}" ]]; then
                # stored value is not up-to-date

                IFS='|'
                read -r -a arr_out <<< "${OUTPUT}"
                read -r -a arr_cnt <<< "${cnt}"

                for index in "${!arr_out[@]}"; do
                    if [[ "${arr_out[index]}" -gt "${arr_cnt[index]}" ]]; then
                        # new unread mail is present
                        notify-send "New email. ${OUTPUT}"
                        break
                    fi
                done

                echo -n "${OUTPUT}" > "${MAIL_COUNTER}"
            fi
        else
            # create a new file
            echo -n "${OUTPUT}" > "${MAIL_COUNTER}"
            chmod 600 "${MAIL_COUNTER}"
        fi

        # output to polybar
        echo "$OUTPUT"

    else
        echo ".netrc file does not exist"
    fi
    ;;

esac
polybar config
[module/email]
type = custom/script
interval = 300
exec = ~/.config/polybar/scripts/email.sh --get
; remove if networkmanager is not installed and/or online connection is not required
exec-if = nm-online -t 10

format = <label>
label = ${env:POLYBAR_EMAIL}
Here is a bonus - polybar extra label formating in my polybar_start.sh.
export POLYBAR_EMAIL="%{A1:~/.config/polybar/scripts/email.sh --primary:}%{u$background}%{o$background}%{+u}%{+o}%{B$background}%{F$email_icon_bg}%{T3}%{B$email_icon_bg}%{F$icon_clr}%{T5}%{B$email_text_bg}%{F$email_icon_bg}%{T3}%{F$text_clr}%{T1} %output%%{B$background}%{F$email_text_bg}%{T3}%{-u}%{-o}%{A}"
1 Like

Forum kindly sponsored by