This article describes how Python receives Gmail's new mail and sends it to gtalk. Share to everyone for your reference. The implementation method is as follows:
#!/usr/bin/env python #-*-coding:utf-8-*-import imaplib Import string, random import Stringio, rfc822 import email F Rom GOOGLE.APPENGINE.API import xmpp SERVER1 = "imap.gmail.com" USER = "Yeah" PASSWORD = "Ohmyga" # Connect to Server Serv ER = imaplib (SERVER1, 993) # login Server.login (USER, PASSWORD) server.select () # list items on server status, data = Serv Er.search (None, "(unseen)") mails = Data[0].split () if data[0]!= ': print ' has mails ' user_address = ' Wangnaide@gmai l.com ' for Num in Data[0].split (): TPE, raw_msg = Server.fetch (num, ' (RFC822) ') msg = email.message_from_string ( RAW_MSG[0][1]) #Subjects sbj, Ecode = email. Header.decode_header (msg[' subject ']) [0] #from, sender frm = ' for FTS, ecode in email. Header.decode_header (msg[' from ']): frm = frm + fts if Xmpp.get_presence (user_address): Xmpp.send_message (U ser_address, frm + ': ' + sbj) server.store (num, ' +flags ', ' \\SEEN ') #print frm + ":" + SBJ server.close () sErver.logout ()
I hope this article will help you with your Python programming.