2 Replies - 988 Views - Last Post: 07 February 2013 - 06:19 AM Rate Topic: -----

#1 LAPB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-January 13

daemon to check gmail for new mail

Posted 28 January 2013 - 12:03 PM

hello community of dreamincode, ive decided to write a python script to check my gmail account for new mail, its made of various pieces of code obtained from the interwebs:
#!/usr/bin/python -O
 
import daemon
import time
import os
import signal
import sys
import imaplib
 
class GmailCheckDaemon(daemon.DaemonContext):
    def __init__(self):
        super(GmailCheckDaemon, self).__init__()
        self.signal_map[signal.SIGTERM] = self.sigtermHandler
        self.stdout = open('/home/lapb/programs/gmailCheckDaemonStdOut.log', 'wb')
        self.stderr = open('/home/lapb/programs/gmailCheckDaemonStdErr.log', 'wb')
        self.mail = imaplib.IMAP4_SSL('imap.gmail.com','993')
        self.mail.login('myaddress@gmail.com','mypassword')
        self.mail.select()
    
    def run(self):
        with self:
            unreads = len(self.mail.search(None, 'UnSeen')[1][0].split())
            if unreads > 0:
                popup = 'kdialog --title "New mail" --passivepopup "there are' + str(unreads) + 'unread emails" 1'
                os.system(popup)
            while True:
                oldunreads = unreads
                unreads = len(self.mail.search(None, 'UnSeen')[1][0].split())
                count = unreads - oldunreads
                if count > 0:
                    popup = 'kdialog --title "New mail" --passivepopup "there are' + str(count) + 'unread emails" 1'
                    os.system(popup)
                time.sleep(60)
    
    def sigtermHandler(self, *args):
        self.mail.close()
        self.mail.logout()
        sys.exit(0)
 
 
if __name__ == '__main__':
    daemon = GmailCheckDaemon()
    daemon.run()



and i get the following output in the stderr file:

Traceback (most recent call last):
  File "/home/lapb/programs/checkgmail.py", line 43, in <module>
    daemon.run()
  File "/home/lapb/programs/checkgmail.py", line 22, in run
    unreads = len(self.mail.search(None, 'UnSeen')[1][0].split())
  File "/usr/lib/python2.7/imaplib.py", line 627, in search
    typ, dat = self._simple_command(name, *criteria)
  File "/usr/lib/python2.7/imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.7/imaplib.py", line 859, in _command
    raise self.abort('socket error: %s' % val)
imaplib.abort: socket error: [Errno 9] Bad file descriptor



sadly googleing for "python IMAP4.search "Bad file descriptor"" hasnt returned anything helpfull
thanks for any help

Is This A Good Question/Topic? 0
  • +

Replies To: daemon to check gmail for new mail

#2 bgammill  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 39
  • Joined: 11-June 12

Re: daemon to check gmail for new mail

Posted 06 February 2013 - 10:42 PM

I've scratched my head for a couple of hours at this one. If you move the self.stdout and self.stderr declarations into the run method, the program successfully writes to gmailCheckDaemonStdOut.log. I don't know if this the outcome you want, but it's a start.

#!/usr/bin/python -O
 
import daemon
import time
import os
import signal
import sys
import imaplib
 
class GmailCheckDaemon(daemon.DaemonContext):
    def __init__(self):
        super(GmailCheckDaemon, self).__init__()
        self.signal_map[signal.SIGTERM] = self.sigtermHandler
        self.mail = imaplib.IMAP4_SSL('imap.gmail.com','993')
        self.mail.login('myaddress@gmail.com','mypassword')
        self.mail.select()
    
    def run(self):
        with self:
		    self.stdout = open('/home/lapb/programs/gmailCheckDaemonStdOut.log', 'wb')
			self.stderr = open('/home/lapb/programs/gmailCheckDaemonStdErr.log', 'wb')
            unreads = len(self.mail.search(None, 'UnSeen')[1][0].split())
            if unreads > 0:
                popup = 'kdialog --title "New mail" --passivepopup "there are' + str(unreads) + 'unread emails" 1'
                os.system(popup)
            while True:
                oldunreads = unreads
                unreads = len(self.mail.search(None, 'UnSeen')[1][0].split())
                count = unreads - oldunreads
                if count > 0:
                    popup = 'kdialog --title "New mail" --passivepopup "there are' + str(count) + 'unread emails" 1'
                    os.system(popup)
                time.sleep(60)
    
    def sigtermHandler(self, *args):
        self.mail.close()
        self.mail.logout()
        sys.exit(0)
 
 
if __name__ == '__main__':
    daemon = GmailCheckDaemon()
    daemon.run()


Was This Post Helpful? 0
  • +
  • -

#3 LAPB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-January 13

Re: daemon to check gmail for new mail

Posted 07 February 2013 - 06:19 AM

well thanks for replying but that part of the program was already working. my problem is what causes the
imaplib.abort: socket error: [Errno 9] Bad file descriptor
. well anyway its not such a big deal my main motivation to write this little script was that the mail client that i was using at the time (claws mail) didnt recognize my new mail as new just as unread, but i decided to stop using it when it crashed when i tried to send a mail, ive been using thunderbird since and it does recognize my new mail as new and does the corresponding stuff, still would be didactic to know how to make this little script run properly
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1