Python receives gmail new mail and sends it to the Gtalk method _python

Source: Internet
Author: User
Tags rfc822

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.