#!/usr/bin/env python
Firs import all need library
import imaplib
import email
Second, write you mail credential
IMAPSERVER = 'smtp.gmail.com'
USER = 'yuo_mail@gmail.com'
PASSWORD = '******************'
Write function
def my_email():
try:
mail = imaplib.IMAP4_SSL(IMAPSERVER)
mail.login(USER, PASSWORD)
where we try find new e-mail
mail.select("INBOX", True)
after we send credential to gmail server we want now connection happened or not
return_code, mail_ids = mail.search(None, 'UNSEEN')
search mail with UNSEEN keyword, if return empty mail id value. More about e-mail keyword you can find in RFC305 protocol document.
if mail_ids == ['']:
value 0 accept
count = 0
else:
if return value uneven 0
count = len(mail_ids[0].split(' '))
catch number of items in the object.
except:
count = 0
pass
print (count)
Now we know how many unread e-mail messages we have in Inbox. Change few parameters, UNSEEN change in ALL, get information about all inbox email. If repeat function twice, with different keywords, have information how all email and unread email.
try:
result, mail_id = mail.search(None, 'ALL')
ID = mail_id[0]
if ID == 0:
pass
else:
id_list = ID.split(' ')
latest_email_id = id_list[-1]
All collecting information can print or show.
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][-1]
raw_email_string = raw_email.decode('UTF-8') #ASCII')#CP1257') # 'iso-8859-13'
email_message = email.message_from_string(raw_email_string)
MESSAGE_LINE = (latest_email_id, 'e-mail', count,'unread')
MESSAGE_LINE1 =('The last From: ' + email_message['From'])
print (MESSAGE_LINE,MESSAGE_LINE1)
In that moment i have a little trouble, because I don't find the way how decode e-mail messages header, how you can see I try use 'UTF-8' ASCII' CP1257' 'iso-8859-13' decoding, but nothing work correctly.
Testing method when when function restarting "window.after(1200000, my_email)", I mean function repeat every 20 minutes or "N" seconds- work. But you must change code. Format message line for text, every time when sours repeat update text in label :
msg_line = ("Yuo have: " +latest_email_id + " e-mail" + "\n",(count),'unread'+'\n ','The last From: ' + email_message['From'])
if msg_line != msg_line1:
msg_line1 = msg_line
line_msg.config(text = msg_line)
When you have something like that:
try:
result, mail_id = mail.search(None, 'ALL')
ID = mail_id[0]
if ID == 0:
pass
else:
id_list = ID.split(' ')
latest_email_id = id_list[-1]
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][-1]
raw_email_string = raw_email.decode('UTF-8')
email_message = email.message_from_string(raw_email_string)
msg_line = "You have: {0:4}, e-mail \nUnread: {1:4}, e-mail \nThe last From : {2:20}" .format(latest_email_id, count, email_message['From'])
if msg_line != msg_line1:
msg_line1 = msg_line
line_msg.config(text = msg_line)
except:
ID = 0
pass
msg_no = Label (window, text="No Messages", font="times 22")
msg_no.grid()
window.after(120000, my_email)
This source line repeat function my_email every 20 minutes but not hide message box (tkinter), for messages hiding I find way when put in code window.after(30000,lambda:window.destroy()). But after destroy nothing to repeat. Maybe i have two way to get result:
1. The messages show all time and update every 20 minutes or "N" times. If you use this method leave code as is and comment window.after(30000,lambda:window.destroy()) with # ;
2. The messages show only 30 seconds every time when you need or every "N" time per day. If you want this method in code uncomment window.after(30000,lambda:window.destroy()), comment message update cycle if msn_line != ..., leave only line_msg.config(text=msg_line) and comment window.after(120000, my_email);
Firs import all need library
import imaplib
import email
Second, write you mail credential
IMAPSERVER = 'smtp.gmail.com'
USER = 'yuo_mail@gmail.com'
PASSWORD = '******************'
Write function
def my_email():
try:
mail = imaplib.IMAP4_SSL(IMAPSERVER)
mail.login(USER, PASSWORD)
where we try find new e-mail
mail.select("INBOX", True)
after we send credential to gmail server we want now connection happened or not
return_code, mail_ids = mail.search(None, 'UNSEEN')
search mail with UNSEEN keyword, if return empty mail id value. More about e-mail keyword you can find in RFC305 protocol document.
if mail_ids == ['']:
value 0 accept
count = 0
else:
if return value uneven 0
count = len(mail_ids[0].split(' '))
catch number of items in the object.
except:
count = 0
pass
print (count)
Now we know how many unread e-mail messages we have in Inbox. Change few parameters, UNSEEN change in ALL, get information about all inbox email. If repeat function twice, with different keywords, have information how all email and unread email.
try:
result, mail_id = mail.search(None, 'ALL')
ID = mail_id[0]
if ID == 0:
pass
else:
id_list = ID.split(' ')
latest_email_id = id_list[-1]
All collecting information can print or show.
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][-1]
raw_email_string = raw_email.decode('UTF-8') #ASCII')#CP1257') # 'iso-8859-13'
email_message = email.message_from_string(raw_email_string)
MESSAGE_LINE = (latest_email_id, 'e-mail', count,'unread')
MESSAGE_LINE1 =('The last From: ' + email_message['From'])
print (MESSAGE_LINE,MESSAGE_LINE1)
In that moment i have a little trouble, because I don't find the way how decode e-mail messages header, how you can see I try use 'UTF-8' ASCII' CP1257' 'iso-8859-13' decoding, but nothing work correctly.
Testing method when when function restarting "window.after(1200000, my_email)", I mean function repeat every 20 minutes or "N" seconds- work. But you must change code. Format message line for text, every time when sours repeat update text in label :
msg_line = ("Yuo have: " +latest_email_id + " e-mail" + "\n",(count),'unread'+'\n ','The last From: ' + email_message['From'])
if msg_line != msg_line1:
msg_line1 = msg_line
line_msg.config(text = msg_line)
When you have something like that:
try:
result, mail_id = mail.search(None, 'ALL')
ID = mail_id[0]
if ID == 0:
pass
else:
id_list = ID.split(' ')
latest_email_id = id_list[-1]
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][-1]
raw_email_string = raw_email.decode('UTF-8')
email_message = email.message_from_string(raw_email_string)
msg_line = "You have: {0:4}, e-mail \nUnread: {1:4}, e-mail \nThe last From : {2:20}" .format(latest_email_id, count, email_message['From'])
if msg_line != msg_line1:
msg_line1 = msg_line
line_msg.config(text = msg_line)
except:
ID = 0
pass
msg_no = Label (window, text="No Messages", font="times 22")
msg_no.grid()
window.after(120000, my_email)
This source line repeat function my_email every 20 minutes but not hide message box (tkinter), for messages hiding I find way when put in code window.after(30000,lambda:window.destroy()). But after destroy nothing to repeat. Maybe i have two way to get result:
1. The messages show all time and update every 20 minutes or "N" times. If you use this method leave code as is and comment window.after(30000,lambda:window.destroy()) with # ;
2. The messages show only 30 seconds every time when you need or every "N" time per day. If you want this method in code uncomment window.after(30000,lambda:window.destroy()), comment message update cycle if msn_line != ..., leave only line_msg.config(text=msg_line) and comment window.after(120000, my_email);