Python Operation Gmail Instance _python

Source: Internet
Author: User

The examples in this article describe how Python operates Gmail. Share to everyone for your reference.

The implementation methods are as follows:

Copy Code code as follows:
Import Imaplib, re

Class Pygmail (object):
def __init__ (self):
Self. Imap_server= ' imap.gmail.com '
Self. imap_port=993
Self. M = None
Self.response = None
Self.mailboxes = []

def login (self, username, password):
Self. M = Imaplib. Imap4_ssl (self. Imap_server, self. Imap_port)
RC, self.response = self. M.login (username, password)
return RC

def get_mailboxes (self):
RC, self.response = self. M.list ()
For item in Self.response:
Self.mailboxes.append (Item.split () [-1])
return RC

def get_mail_count (self, folder= ' Inbox '):
RC, self.response = self. M.select (folder)
return self.response[0]

def get_unread_count (self, folder= ' Inbox '):
RC, self.response = self. M.status (folder, "(unseen)")
Unreadcount = Re.search ("Unseen (\d+)", Self.response[0]). Group (1)
Return Unreadcount

def get_imap_quota (self):
Quotastr = self. M.getquotaroot ("Inbox") [1][1][0]
r = Re.compile (' \d+ '). FindAll (QUOTASTR)
if r = = []:
R.append (0)
R.append (0)
return float (r[1])/1024, float (r[0])/1024

def get_mails_from (self, uid, folder= ' Inbox '):
Status, Count = self. M.select (folder, Readonly=1)
Status, Response = self. M.search (None, ' from ', UID)
Email_ids = [e_id for e_id in Response[0].split ()]
Return Email_ids

def get_mail_from_id (self, id):
Status, Response = self. M.fetch (ID, ' (body[header.fields (subject)]) ')
return response

def rename_mailbox (self, Oldmailbox, Newmailbox):
RC, self.response = self. M.rename (Oldmailbox, Newmailbox)
return RC

def create_mailbox (self, mailbox):
RC, self.response = self. M.create (mailbox)
return RC

def delete_mailbox (self, mailbox):
RC, self.response = self. M.delete (mailbox)
return RC

def logout (self):
Self. M.logout ()

if __name__ = = "__main__":
Demo=pygmail ()
Demo.login ("renwenchao888@gmail.com", "qqq191430791")
Mailboxex=demo.get_mailboxes ()
For I in Demo.response:
Print I
Demo.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.