Skip to content

Instantly share code, notes, and snippets.

@thingsiplay
Created April 24, 2022 11:05
Show Gist options
  • Save thingsiplay/e03a84ebc6e01c9b475cd0277fb81def to your computer and use it in GitHub Desktop.
Save thingsiplay/e03a84ebc6e01c9b475cd0277fb81def to your computer and use it in GitHub Desktop.
Count unread mails in Thunderbird and write to stdout.
#!/bin/env bash
# Count unread mails in Thunderbird and write to stdout.
# Usage:
# checkmails.sh
# Path to your Thunderbird profile.
profile="$HOME/.thunderbird/aabbcc007.default"
# Regex to find the unread message lines.
# Adding "9F" in front of regex works for ImapMail, but not for pop3. in Mail.
regex='\(\^A2=([0-9]+)'
count_unreadmails() {
count=0
result=$(grep -Eo "$regex" "$1" | tail -1)
if [[ $result =~ $regex ]]
then
count="${BASH_REMATCH[1]}"
unreadmail_count=$(($unreadmail_count + $count))
fi
return $count
}
unreadmail_count=0
# Either use smart mailboxes or the Mail and ImapMail inboxes directly. Smart
# mailboxes are those specifically selected and enabled from within
# Thunderbird's inbox folder (right mouse click on inbox). Therefore don't
# enable SMART MAILBOXES and those in MAILBOXES at the same time.
# SMART MAILBOXES
# Includes all grouped mail specified in the right mouse menu on inbox folder.
for inbox in "$profile/Mail/smart mailboxes/Inbox.msf"
do
count_unreadmails "$inbox"
done
# MAILBOXES
# for inbox in $profile/ImapMail/*/INBOX.msf
# do
# count_unreadmails "$inbox"
# done
# for inbox in $profile/Mail/pop3.*/Inbox.msf
# do
# count_unreadmails "$inbox"
# done
# OUTPUT
# Use `echo -n` if no newline should be added.
echo -n "$unreadmail_count"
@thingsiplay
Copy link
Author

To make this work, you first need to change the profile variable to point to your actual Thunderbird profile.

  1. https://i.imgur.com/8oHIuVq.png - Show unified folders in Thunderbird.
  2. https://i.imgur.com/coJZ5ji.png - Open Properties for Inbox.
  3. https://i.imgur.com/e7b1Euw.png - Open Menu to select which mail accounts to search.
  4. https://i.imgur.com/rWyxBoF.png - Scroll the list and enable what you want to search for each account. Usually it is label "Inbox".
  5. https://i.imgur.com/0s3a46v.png - Update the new settings and restart Thunderbird.

Now if you run the script, it should count the mails in your inbox for every included account.
Alternatively comment the lines with SMART MAILBOXES out in the script and enable the lines below it. This will lookup the mailboxes inbox directly, so don't mix smart mailbox with this. Otherwise they are counted twice. You could even lookup the folder structure in your profile and select specific accounts directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment