<tfoot id='ecOiD'></tfoot>

      1. <i id='ecOiD'><tr id='ecOiD'><dt id='ecOiD'><q id='ecOiD'><span id='ecOiD'><b id='ecOiD'><form id='ecOiD'><ins id='ecOiD'></ins><ul id='ecOiD'></ul><sub id='ecOiD'></sub></form><legend id='ecOiD'></legend><bdo id='ecOiD'><pre id='ecOiD'><center id='ecOiD'></center></pre></bdo></b><th id='ecOiD'></th></span></q></dt></tr></i><div id='ecOiD'><tfoot id='ecOiD'></tfoot><dl id='ecOiD'><fieldset id='ecOiD'></fieldset></dl></div>

        • <bdo id='ecOiD'></bdo><ul id='ecOiD'></ul>
      2. <small id='ecOiD'></small><noframes id='ecOiD'>

        <legend id='ecOiD'><style id='ecOiD'><dir id='ecOiD'><q id='ecOiD'></q></dir></style></legend>

        如何使用 Python 在不接触附件的情况下有效地解析电子邮件

        时间:2023-10-20
          <tbody id='8spGp'></tbody>
      3. <i id='8spGp'><tr id='8spGp'><dt id='8spGp'><q id='8spGp'><span id='8spGp'><b id='8spGp'><form id='8spGp'><ins id='8spGp'></ins><ul id='8spGp'></ul><sub id='8spGp'></sub></form><legend id='8spGp'></legend><bdo id='8spGp'><pre id='8spGp'><center id='8spGp'></center></pre></bdo></b><th id='8spGp'></th></span></q></dt></tr></i><div id='8spGp'><tfoot id='8spGp'></tfoot><dl id='8spGp'><fieldset id='8spGp'></fieldset></dl></div>

        • <legend id='8spGp'><style id='8spGp'><dir id='8spGp'><q id='8spGp'></q></dir></style></legend>

            <small id='8spGp'></small><noframes id='8spGp'>

            • <bdo id='8spGp'></bdo><ul id='8spGp'></ul>

            • <tfoot id='8spGp'></tfoot>

                1. 本文介绍了如何使用 Python 在不接触附件的情况下有效地解析电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Python imaplib (Python 2.6) 从 GMail 获取电子邮件.我使用 http://docs.python.org/方法获取电子邮件的所有内容library/imaplib.html#imaplib.IMAP4.fetch 我收到了整封电子邮件.我只需要文本部分并解析附件名称,而不需要下载它们.如何做到这一点?我看到 GMail 返回的电子邮件与浏览器发送到 HTTP 服务器的格式相同.

                  I'm playing with Python imaplib (Python 2.6) to fetch emails from GMail. Everything I fetch an email with method http://docs.python.org/library/imaplib.html#imaplib.IMAP4.fetch I get whole email. I need only text part and also parse names of attachments, without downloading them. How this can be done? I see that emails returned by GMail follow the same format that browsers send to HTTP servers.

                  推荐答案

                  看看这个食谱:http://code.activestate.com/recipes/498189/

                  我稍微调整了它以打印发件人、主题、日期、附件名称和邮件正文(现在只是纯文本 - 添加 html 邮件很简单).

                  I adapted it slightly to print the From, Subject, Date, name of attachments, and message body (just plaintext for now -- its trivial to add html messages).

                  在这种情况下,我使用了 Gmail pop3 服务器,但它也应该适用于 IMAP.

                  I used the Gmail pop3 server in this case, but it should work for IMAP as well.

                  import poplib, email, string
                  
                  mailserver = poplib.POP3_SSL('pop.gmail.com')
                  mailserver.user('recent:YOURUSERNAME') #use 'recent mode'
                  mailserver.pass_('YOURPASSWORD') #consider not storing in plaintext!
                  
                  numMessages = len(mailserver.list()[1])
                  for i in reversed(range(numMessages)):
                      message = ""
                      msg = mailserver.retr(i+1)
                      str = string.join(msg[1], "
                  ")
                      mail = email.message_from_string(str)
                  
                      message += "From: " + mail["From"] + "
                  "
                      message += "Subject: " + mail["Subject"] + "
                  "
                      message += "Date: " + mail["Date"] + "
                  "
                  
                      for part in mail.walk():
                          if part.is_multipart():
                              continue
                          if part.get_content_type() == 'text/plain':
                              body = "
                  " + part.get_payload() + "
                  "
                          dtypes = part.get_params(None, 'Content-Disposition')
                          if not dtypes:
                              if part.get_content_type() == 'text/plain':
                                  continue
                              ctypes = part.get_params()
                              if not ctypes:
                                  continue
                              for key,val in ctypes:
                                  if key.lower() == 'name':
                                      message += "Attachment:" + val + "
                  "
                                      break
                              else:
                                  continue
                          else:
                              attachment,filename = None,None
                              for key,val in dtypes:
                                  key = key.lower()
                                  if key == 'filename':
                                      filename = val
                                  if key == 'attachment':
                                      attachment = 1
                              if not attachment:
                                  continue
                              message += "Attachment:" + filename + "
                  "
                          if body:
                              message += body + "
                  "
                      print message
                      print
                  

                  这应该足以让您朝着正确的方向前进.

                  This should be enough to get you heading in the right direction.

                  这篇关于如何使用 Python 在不接触附件的情况下有效地解析电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python、IMAP 和 GMail.将消息标记为已查看 下一篇:使用 Python 解析 Gmail 并将所有早于日期的内容标记为“已读"

                  相关文章

                2. <tfoot id='9ulzL'></tfoot>

                  1. <small id='9ulzL'></small><noframes id='9ulzL'>

                    <legend id='9ulzL'><style id='9ulzL'><dir id='9ulzL'><q id='9ulzL'></q></dir></style></legend>
                  2. <i id='9ulzL'><tr id='9ulzL'><dt id='9ulzL'><q id='9ulzL'><span id='9ulzL'><b id='9ulzL'><form id='9ulzL'><ins id='9ulzL'></ins><ul id='9ulzL'></ul><sub id='9ulzL'></sub></form><legend id='9ulzL'></legend><bdo id='9ulzL'><pre id='9ulzL'><center id='9ulzL'></center></pre></bdo></b><th id='9ulzL'></th></span></q></dt></tr></i><div id='9ulzL'><tfoot id='9ulzL'></tfoot><dl id='9ulzL'><fieldset id='9ulzL'></fieldset></dl></div>

                      • <bdo id='9ulzL'></bdo><ul id='9ulzL'></ul>