python接收gmail新邮件并发送到gtalk的方法

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

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import imaplib
import string, random
import stringio, rfc822
import email
from google.appengine.api import xmpp
server1 = “imap.gmail.com”
user = “yeah”
password = “ohmyga”
# connect to server
server = imaplib(server1, 993)
# login
server.login(user, password)
server.select()
# list items on server
status, 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 + “:” + sbj
server.close()
server.logout()

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

Posted in 未分类

发表评论