用Python方法把Gmail邮件转发到gtalk有多难,想知道gmail如何转发邮件?gmail自动转发已收到邮件的设置方法。本文就来把这段详细的python学习参考代码贴出来,大家一起参考下吧。
gmail转发邮件到gtalk的Python方法源码:
#!/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 Gmail邮件相关文章推荐:如何用Python实现Gmail发信的方法
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/mail/m2554.html
相关文章 Recommend