首页 > 编程 > Python > 正文

Python接收Gmail新邮件并发送到gtalk的方法

2019-11-25 17:58:13
字体:
来源:转载
供稿:网友

本文实例讲述了Python接收Gmail新邮件并发送到gtalk的方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/env python# -*- coding: utf-8 -*-import imaplibimport string, randomimport StringIO, rfc822import emailfrom google.appengine.api import xmppSERVER1 = "imap.gmail.com"USER = "yeah"PASSWORD = "ohmyga"# connect to serverserver = imaplib(SERVER1, 993)# loginserver.login(USER, PASSWORD)server.select()# list items on serverstatus, data = server.search(None, "(UNSEEN)")mails = data[0].split()if data[0] != '':  print "has mails"  user_address = 'wangnaide@gmail.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(user_address, frm + ':' + sbj)      server.store(num, '+FLAGS', '//SEEN')    #print frm + ":" + sbjserver.close()server.logout()

希望本文所述对大家的Python程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表