1. <tfoot id='Eym55'></tfoot>

      <legend id='Eym55'><style id='Eym55'><dir id='Eym55'><q id='Eym55'></q></dir></style></legend>
    2. <small id='Eym55'></small><noframes id='Eym55'>

      <i id='Eym55'><tr id='Eym55'><dt id='Eym55'><q id='Eym55'><span id='Eym55'><b id='Eym55'><form id='Eym55'><ins id='Eym55'></ins><ul id='Eym55'></ul><sub id='Eym55'></sub></form><legend id='Eym55'></legend><bdo id='Eym55'><pre id='Eym55'><center id='Eym55'></center></pre></bdo></b><th id='Eym55'></th></span></q></dt></tr></i><div id='Eym55'><tfoot id='Eym55'></tfoot><dl id='Eym55'><fieldset id='Eym55'></fieldset></dl></div>
        <bdo id='Eym55'></bdo><ul id='Eym55'></ul>
    3. python imaplib 获取 gmail 收件箱主题标题和发件人姓名

      python imaplib to get gmail inbox subjects titles and sender name(python imaplib 获取 gmail 收件箱主题标题和发件人姓名)
      <legend id='O6wyo'><style id='O6wyo'><dir id='O6wyo'><q id='O6wyo'></q></dir></style></legend>

        <tbody id='O6wyo'></tbody>
        <tfoot id='O6wyo'></tfoot>
        • <bdo id='O6wyo'></bdo><ul id='O6wyo'></ul>

          <small id='O6wyo'></small><noframes id='O6wyo'>

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

                本文介绍了python imaplib 获取 gmail 收件箱主题标题和发件人姓名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 pythons imaplib 连接到我的 gmail 帐户.我想检索前 15 条消息(未读或已读,没关系),只显示主题和发件人姓名(或地址),但不知道如何显示收件箱的内容.

                I'm using pythons imaplib to connect to my gmail account. I want to retrieve the top 15 messages (unread or read, it doesn't matter) and display just the subjects and sender name (or address) but don't know how to display the contents of the inbox.

                这是我目前的代码(连接成功)

                Here is my code so far (successful connection)

                import imaplib
                
                mail = imaplib.IMAP4_SSL('imap.gmail.com')
                mail.login('mygmail@gmail.com', 'somecrazypassword')
                mail.list()
                mail.select('inbox')
                
                #need to add some stuff in here
                
                mail.logout()
                

                我相信这应该足够简单,我只是对 imaplib 库的命令不够熟悉.任何帮助将不胜感激...

                I believe this should be simple enough, I'm just not familiar enough with the commands for the imaplib library. Any help would be must appreciated...

                更新感谢 Julian,我可以遍历每条消息并检索全部内容:

                UPDATE thanks to Julian I can iterate through each message and retrieve the entire contents with:

                typ, data = mail.search(None, 'ALL')
                for num in data[0].split():
                   typ, data = mail.fetch(num, '(RFC822)')
                   print 'Message %s
                %s
                ' % (num, data[0][1])
                mail.close()
                

                但我只想要主题和发件人.这些项目是否有 imaplib 命令,或者我是否必须解析 data[0][1] 的全部内容以获取文本:主题和发件人?

                but I'm wanting just the subject and the sender. Is there a imaplib command for these items or will I have to parse the entire contents of data[0][1] for the text: Subject, and Sender?

                更新好的,让主题和发件人部分工作,但迭代 (1, 15) 是按 desc 顺序完成的,显然首先向我显示最旧的消息.我怎样才能改变这个?我试过这样做:

                UPDATE OK, got the subject and sender part working but the iteration (1, 15) is done by desc order apparently showing me the oldest messages first. How can I change this? I tried doing this:

                for i in range( len(data[0])-15, len(data[0]) ):
                     print data
                

                但这只是给了我所有 15 次迭代的 None ......有什么想法吗?我也试过 mail.sort('REVERSE DATE', 'UTF-8', 'ALL') 但 gmail 不支持 .sort() 函数

                but that just gives me None for all 15 iterations... any ideas? I've also tried mail.sort('REVERSE DATE', 'UTF-8', 'ALL') but gmail doesnt support the .sort() function

                更新想出了一个办法:

                #....^other code is the same as above except need to import email module
                mail.select('inbox')
                typ, data = mail.search(None, 'ALL')
                ids = data[0]
                id_list = ids.split()
                #get the most recent email id
                latest_email_id = int( id_list[-1] )
                
                #iterate through 15 messages in decending order starting with latest_email_id
                #the '-1' dictates reverse looping order
                for i in range( latest_email_id, latest_email_id-15, -1 ):
                   typ, data = mail.fetch( i, '(RFC822)' )
                
                   for response_part in data:
                      if isinstance(response_part, tuple):
                          msg = email.message_from_string(response_part[1])
                          varSubject = msg['subject']
                          varFrom = msg['from']
                
                   #remove the brackets around the sender email address
                   varFrom = varFrom.replace('<', '')
                   varFrom = varFrom.replace('>', '')
                
                   #add ellipsis (...) if subject length is greater than 35 characters
                   if len( varSubject ) > 35:
                      varSubject = varSubject[0:32] + '...'
                
                   print '[' + varFrom.split()[-1] + '] ' + varSubject
                

                这为我提供了最新的 15 条消息主题和发件人地址,按要求按降序排列!感谢所有帮助过的人!

                this gives me the most recent 15 message subject and sender address in decending order as requested! Thanks to all who helped!

                推荐答案

                    c.select('INBOX', readonly=True)
                
                    for i in range(1, 30):
                        typ, msg_data = c.fetch(str(i), '(RFC822)')
                        for response_part in msg_data:
                            if isinstance(response_part, tuple):
                                msg = email.message_from_string(response_part[1])
                                for header in [ 'subject', 'to', 'from' ]:
                                    print '%-8s: %s' % (header.upper(), msg[header])
                

                这应该让您了解如何检索主题以及从何处检索主题?

                This should give you an idea on how to retrieve the subject and from?

                这篇关于python imaplib 获取 gmail 收件箱主题标题和发件人姓名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                <legend id='Zs0FA'><style id='Zs0FA'><dir id='Zs0FA'><q id='Zs0FA'></q></dir></style></legend>
                  <tfoot id='Zs0FA'></tfoot>
                  • <bdo id='Zs0FA'></bdo><ul id='Zs0FA'></ul>

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

                          <small id='Zs0FA'></small><noframes id='Zs0FA'>