[Python] python打包你的整个Gmail邮箱 →→→→→进入此内容的聊天室

来自 , 2021-02-04, 写在 Python, 查看 127 次.
URL http://www.code666.cn/view/a714ec67
  1. #!/usr/bin/python
  2. # -*- coding: iso-8859-1 -*-
  3. """ GMail archiver 1.1
  4.  
  5. This program will download and archive all you emails from GMail.
  6. Simply enter your login and password, and all your emails will
  7. be downloaded from GMail and stored in a standard mbox file.
  8. This inclues inbox, archived and sent mails, whatever label you applied.
  9. Spam is not downloaded.
  10.  
  11. This mbox files can later on be opened with almost any email client (eg. Evolution).
  12.  
  13. Author:
  14.    Sébastien SAUVAGE - sebsauvage at sebsauvage dot net
  15.    Webmaster for http://sebsauvage.net/
  16.  
  17. License:
  18.  
  19.    This program is distributed under the OSI-certified zlib/libpnglicense .
  20.    http://www.opensource.org/licenses/zlib-license.php
  21.  
  22.    This software is provided 'as-is', without any express or implied warranty.
  23.    In no event will the authors be held liable for any damages arising from
  24.    the use of this software.
  25.  
  26.    Permission is granted to anyone to use this software for any purpose,
  27.    including commercial applications, and to alter it and redistribute it freely,
  28.    subject to the following restrictions:
  29.  
  30.        1. The origin of this software must not be misrepresented; you must not
  31.           claim that you wrote the original software. If you use this software
  32.           in a product, an acknowledgment in the product documentation would be
  33.           appreciated but is not required.
  34.  
  35.        2. Altered source versions must be plainly marked as such, and must not
  36.           be misrepresented as being the original software.
  37.  
  38.        3. This notice may not be removed or altered from any source distribution.
  39.  
  40. Requirements:
  41.  
  42.    - a GMail account with IMAP enabled in settings.
  43.    - GMail settings in english
  44.    - Python 2.5
  45. """
  46. import imaplib,getpass,os
  47.  
  48. print "GMail archiver 1.0"
  49. user = raw_input("Enter your GMail username:")
  50. pwd = getpass.getpass("Enter your password: ")
  51. m = imaplib.IMAP4_SSL("imap.gmail.com")
  52. m.login(user,pwd)
  53. m.select("[Gmail]/All Mail")
  54. resp, items = m.search(None, "ALL")
  55. items = items[0].split()
  56. print "Found %d emails." % len(items)
  57. count = len(items)
  58. for emailid in items:
  59.     print "Downloading email %s (%d remaining)" % (emailid,count)
  60.     resp, data = m.fetch(emailid, "(RFC822)")
  61.     email_body = data[0][1]
  62.     # We duplicate the From: line to the beginning of the email because mbox format requires it.
  63.     from_line = "from:unknown@unknown"
  64.     try:
  65.         from_line = [line for line in email_body[:16384].split('\n') if line.lower().startswith('from:')][0].strip()
  66.     except IndexError:
  67.         print "  'from:' unreadable."
  68.     email_body = "From %s\n%s" % (from_line[5:].strip(),email_body)
  69.     file = open("%s.mbox"%user,"a")
  70.     file.write(email_body)
  71.     file.write("\n")
  72.     file.close()
  73.     count -= 1
  74. print "All done."
  75.  
  76. #//python/1877

回复 "python打包你的整个Gmail邮箱"

这儿你可以回复上面这条便签

captcha