Polybar mail check module [Solved]


#1

Hi all geeks, I have created a polybar module for pop3 mail checking but I think there’s something wrong with my script because when there is no unread mail in my “inbox” I get the output “1”.
Here are the module and the script:

[module/pop-free]
type = custom/script
tail = true
exec = inbox-free.sh
interval = 600
label = %output%
click-left = firefox https://zimbra.free.fr/ &

#!/bin/sh

SERVER=“pop.free.fr
NETRC="/home/archus/.config/polybar/free.netrc"

inbox=$(curl -sf --netrc-file “$NETRC” pop3://"$SERVER" | wc -l)

  if [ "$inbox" -gt 0 ]; then
      echo " $inbox"
  else
      echo " 0"

fi

exit 0


#2

This is the script I use.

#!/usr/bin/python

import imaplib
obj = imaplib.IMAP4_SSL('imap.gmail.com',993)
obj.login('USERNAME','PASSWORD') # write your email and password
obj.select()
print(len(obj.search(None, 'UnSeen')[1][0].split()))

And my Module

[module/mail]
type = custom/script
interval = 30
format = <label>
format-prefix = ""
label-padding = 1
#format-prefix-foreground = ${colors.foreground-alt}
exec = $HOME/.config/polybar/mail
click-left = exo-open --launch WebBrowser https://mail.google.com/mail/u/0/#inbox >/dev/null 2>&1 &
click-right = exo-open --launch WebBrowser https://mail.protonmail.com/login >/dev/null 2>&1 &
format-padding = 5

#3

I use imap to check both my Gmail and provider account. The overall checking process is much the same. Without getting into the complexities of setting up mutt/imap/etc - the crux of the biscuit is simply this:

my pb-mail script:

#!/bin/sh
# Simple script that gathers new mail from locl directories.
# This works perfectly if you pull down emails from imap accounts.

du -a ~/.mutt/.mail/*/Inbox/new/* 2>/dev/null | wc -l

This is the only line I have in my mail checker script. Nothing else.

The mail module for my polybar:

[module/mail]
type = custom/script
exec = pb-mail
interval=180
label-padding = 1
format-prefix =" "
click-left = st -e neomutt &
click-right = st -e neomutt &

Super simple, tastes great AND less filling :wink:


#4

Hi guys, for the python script I got this message:

Blockquote import: attempt to perform an operation not allowed by the security policy ‘PS’ @ error/constitute.c/IsCoderAuthorized/408.

And to chris60601, I don’t use mutt for the moment.
Just want to use a simple script to check this mailbox.
Whatever thanks for your quick answers


#5

I dont suspect the mail agent makes a diff - its the dirs you want to monitor for an increase or decrease in the mail items and the command to use to do that…
The du command (IIRC) allows for the the 0 you are seeking.

Of course, if you are not checking mails that may be local (via imap or some other app that pulls pop mail local), then my script is useless.


#6

You need to make sure that Gmail is able to be used with less secure apps.


#7

I have to say that I don’t use a gmail account so I have of course changed the imap server adress and also tried port 143 (less secure) but I still have the message in polybar, this script works fine by launching it the terminal, no warning message and a real state of my inbox indeed.
To get my shell script telling the “thruth” I have to let 1 mail in so when I receive a new one the polybar module tells me I have 2 mails inside :joy:. I consider that the issue isn’t solved but I do use it like this anyway till I find the solution or someone else. Thanks guys.


#8

This causes the “Critical security alert” on my account, and Google doesn’t allow me to log in. Did you change your security settings?


#9

nop, but as I said this works good in the terminal way. Strange isn’t it ?


#10

Im curious; what might the result be if you do something like this for the above

exec = termite -n CheckMail -e inbox-free.sh

And of course, we are assuming that the path to the shell is included in you env.


#11

Do you suggest me to install checkmail ?


#12

exec = st -n CheckMail -e inbox-free.sh

runs the shell, st
CheckMail simply names the terminal when called
-e runs the script

Sorry - I meant to originally say st. You dont need the -n
For termite, I think -t names the terminal, again - not needed.


#13

Hmm I never struck that.

I don’t use 2FA and I have less secure apps enabled. I have never done anything special for this to work.


#14

Wonder if it depends of where one lives I think.


#15

It told me to allow use of less secure apps in settings. :slight_smile:


#16

Maybe this module might be a better idea


#17

thanks dobbie03,
Finaly I found how to make your previous python script work properly, just by telling in the module:

exec = python3 /full/path/to/the/script

No more warning message in polybar now ! :kissing_smiling_eyes: Thx

how to pass this thread as “solved” ?


#18

YAY! Good work.