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


Posted in Python onMarch 10, 2015

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
python实现对一个完整url进行分割的方法
Apr 29 Python
在Django中限制已登录用户的访问的方法
Jul 23 Python
Python与R语言的简要对比
Nov 14 Python
pycharm配置pyqt5-tools开发环境的方法步骤
Feb 11 Python
Python安装Flask环境及简单应用示例
May 03 Python
解决安装python3.7.4报错Can''t connect to HTTPS URL because the SSL module is not available
Jul 31 Python
Python基本语法之运算符功能与用法详解
Oct 22 Python
Python使用指定字符长度切分数据示例
Dec 05 Python
pytorch构建多模型实例
Jan 15 Python
Python 实现Image和Ndarray互相转换
Feb 19 Python
Python爬虫入门案例之爬取去哪儿旅游景点攻略以及可视化分析
Oct 16 Python
python微信智能AI机器人实现多种支付方式
Apr 12 Python
Python创建xml的方法
Mar 10 #Python
Python实现生成简单的Makefile文件代码示例
Mar 10 #Python
Python和GO语言实现的消息摘要算法示例
Mar 10 #Python
Windows和Linux下使用Python访问SqlServer的方法介绍
Mar 10 #Python
Python脚本实现代码行数统计代码分享
Mar 10 #Python
Windows系统配置python脚本开机启动的3种方法分享
Mar 10 #Python
Python自动化构建工具scons使用入门笔记
Mar 10 #Python
You might like
PHP运行出现Notice : Use of undefined constant 的完美解决方案分享
2012/03/05 PHP
修改apache配置文件去除thinkphp url中的index.php
2014/01/17 PHP
php常用文件操作函数汇总
2014/11/22 PHP
typecho插件编写教程(四):插件挂载
2015/05/28 PHP
thinkphp微信开之安全模式消息加密解密不成功的解决办法
2015/12/02 PHP
php 使用curl模拟登录人人(校内)网的简单实例
2016/06/06 PHP
php实现文件与16进制相互转换的方法示例
2017/02/16 PHP
Nginx下ThinkPHP5的配置方法详解
2017/08/01 PHP
thinkphp5 migrate数据库迁移工具
2018/02/20 PHP
js调试系列 控制台命令行API使用方法
2014/06/18 Javascript
基于JS实现数字+字母+中文的混合排序方法
2016/06/06 Javascript
AngularJs自定义服务之实现签名和加密
2016/08/02 Javascript
原生JS中slice()方法和splice()区别
2017/03/06 Javascript
Angular.JS中指令ng-if、ng-show/ng-hide和ng-switch的使用教程
2017/05/07 Javascript
详解用node-images 打造简易图片服务器
2017/05/08 Javascript
Vue2.0父子组件传递函数的教程详解
2017/10/16 Javascript
微信小程序按钮点击跳转页面详解
2019/05/06 Javascript
深入了解响应式React Native Echarts组件
2019/05/29 Javascript
JS实现网页时钟特效
2020/03/25 Javascript
[43:47]DOTA2上海特级锦标赛主赛事日 - 4 败者组第四轮#2 MVP.Phx VS Fnatic第一局
2016/03/05 DOTA
[01:16:37]【全国守擂赛】第三周决赛 Dark Knight vs. 一个弱队
2020/05/04 DOTA
[02:13] 完美世界DOTA2联赛PWL DAY5集锦
2020/11/03 DOTA
python代码制作configure文件示例
2014/07/28 Python
python使用socket连接远程服务器的方法
2015/04/29 Python
Python时间戳使用和相互转换详解
2017/12/11 Python
python3+PyQt5 使用三种不同的简便项窗口部件显示数据的方法
2019/06/17 Python
Python 基于jwt实现认证机制流程解析
2020/06/22 Python
简单的Python人脸识别系统
2020/07/14 Python
python中watchdog文件监控与检测上传功能
2020/10/30 Python
使用html5 canvas 画时钟代码实例分享
2015/11/11 HTML / CSS
篝火晚会策划方案
2014/05/16 职场文书
优秀班集体申报材料
2014/12/25 职场文书
自主招生专家推荐信
2015/03/26 职场文书
实习单位推荐信
2015/03/27 职场文书
房地产公司工程部经理岗位职责
2015/04/09 职场文书
HTML5中 rem适配方案与 viewport 适配问题详解
2021/04/27 HTML / CSS