如何获取从gmail python收到的电子邮件?

亚历杭德罗·加西亚(Alejandro Garcia)

我想用python获取最近收到的10个gmail。

目前,我有这段代码,但是它只返回有限数量的电子邮件,并且可以直接操纵pop3,这使它不再需要很长时间。

代码源:https : //www.code-learner.com/python-use-pop3-to-read-email-example/

import poplib
import smtplib, ssl
def guess_charset(msg):
    # get charset from message object.
    charset = msg.get_charset()
    # if can not get charset
    if charset is None:
       # get message header content-type value and retrieve the charset from the value.
       content_type = msg.get('Content-Type', '').lower()
       pos = content_type.find('charset=')
       if pos >= 0:
          charset = content_type[pos + 8:].strip()
    return charset

def decode_str(s):
    value, charset = decode_header(s)[0]
    if charset:
       value = value.decode(charset)
    return value
# variable indent_number is used to decide number of indent of each level in the mail multiple bory part.
def print_info(msg, indent_number=0):
    if indent_number == 0:
       # loop to retrieve from, to, subject from email header.
       for header in ['From', 'To', 'Subject']:
           # get header value
           value = msg.get(header, '')
           if value:
              # for subject header.
              if header=='Subject':
                 # decode the subject value
                 value = decode_str(value)
              # for from and to header. 
              else:
                 # parse email address
                 hdr, addr = parseaddr(value)
                 # decode the name value.
                 name = decode_str(hdr)
                 value = u'%s <%s>' % (name, addr)
           print('%s%s: %s' % (' ' * indent_number, header, value))
    # if message has multiple part. 
    if (msg.is_multipart()):
       # get multiple parts from message body.
       parts = msg.get_payload()
       # loop for each part
       for n, part in enumerate(parts):
           print('%spart %s' % (' ' * indent_number, n))
           print('%s--------------------' % (' ' * indent_number))
           # print multiple part information by invoke print_info function recursively.
           print_info(part, indent_number + 1)
    # if not multiple part. 
    else:
        # get message content mime type
        content_type = msg.get_content_type() 
        # if plain text or html content type.
        if content_type=='text/plain' or content_type=='text/html':
           # get email content
           content = msg.get_payload(decode=True)
           # get content string charset
           charset = guess_charset(msg)
           # decode the content with charset if provided.
           if charset:
              content = content.decode(charset)
           print('%sText: %s' % (' ' * indent_number, content + '...'))
        else:
           print('%sAttachment: %s' % (' ' * indent_number, content_type))

# input email address, password and pop3 server domain or ip address
email = '[email protected]'
password = 'yourpassword'

# connect to pop3 server:
server = poplib.POP3_SSL('pop.gmail.com')
# open debug switch to print debug information between client and pop3 server.
server.set_debuglevel(1)
# get pop3 server welcome message.
pop3_server_welcome_msg = server.getwelcome().decode('utf-8')
# print out the pop3 server welcome message.
print(server.getwelcome().decode('utf-8'))

# user account authentication
server.user(email)
server.pass_(password)

# stat() function return email count and occupied disk size
print('Messages: %s. Size: %s' % server.stat())
# list() function return all email list
resp, mails, octets = server.list()
print(mails)

# retrieve the newest email index number
#index = len(mails)
index = 3
# server.retr function can get the contents of the email with index variable value index number.
resp, lines, octets = server.retr(index)

# lines stores each line of the original text of the message
# so that you can get the original text of the entire message use the join function and lines variable. 
msg_content = b'\r\n'.join(lines).decode('utf-8')
# now parse out the email object.

from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr

import poplib

# parse the email content to a message object.
msg = Parser().parsestr(msg_content)
print(len(msg_content))
# get email from, to, subject attribute value.
email_from = msg.get('From')
email_to = msg.get('To')
email_subject = msg.get('Subject')
print('From ' + email_from)
print('To ' + email_to)
print('Subject ' + email_subject)
for part in msg.walk():
    if part.get_content_type():
        body = part.get_payload(decode=True)
        print_info(msg, len(msg))


# delete the email from pop3 server directly by email index.
# server.dele(index)
# close pop3 server connection.
server.quit()

我也尝试了这段代码,但是没有用:

import imaplib, email, base64


def fetch_messages(username, password):
    messages = []
    conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
    conn.login(username, password)
    conn.select()
    typ, data = conn.uid('search', None, 'ALL')

    for num in data[0].split():
        typ, msg_data = conn.uid('fetch', num, '(RFC822)')
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                messages.append(email.message_from_string(response_part[1]))
        typ, response = conn.store(num, '+FLAGS', r'(\Seen)')
    return messages

而且这对我也不起作用...

import poplib
from email import parser
pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('@gmail.com')
pop_conn.pass_('password')

messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    print(message['subject'])
    print(message['body'])
随机数字

首选方式为https://developers.google.com/gmail/api/quickstart/python

from gmail.gmail import gmail_auth, ListThreadsMatchingQuery

service = gmail_auth()
threads = ListThreadsMatchingQuery(service, query=query)

哪里:

def ListThreadsMatchingQuery(service, user_id='me', query=''):
  """List all Threads of the user's mailbox matching the query.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    query: String used to filter messages returned.
           Eg.- 'label:UNREAD' for unread messages only.

  Returns:
    List of threads that match the criteria of the query. Note that the returned
    list contains Thread IDs, you must use get with the appropriate
    ID to get the details for a Thread.
  """
  try:
    response = service.users().threads().list(userId=user_id, q=query).execute()
    threads = []
    if 'threads' in response:
      threads.extend(response['threads'])

    while 'nextPageToken' in response:
      page_token = response['nextPageToken']
      response = service.users().threads().list(userId=user_id, q=query,
                                        pageToken=page_token).execute()
      threads.extend(response['threads'])

    return threads
  except errors.HttpError as error:
    raise error

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何仅获取今天的电子邮件-Gmail + Google脚本

如何从Gmail API获取建议的/自动完成的电子邮件?

如何在Node.js中进行Cragislist电子邮件转发(将电子邮件接收到自动生成的电子邮件地址并转发)?

在收到的电子邮件中触发的Google Gmail脚本

如何在GMAIL ADDONS中获取当前用户的电子邮件ID

如何使用python使用Google API从gmail获取电子邮件主题?

Gmail未收到来自SendGrid的电子邮件

如何使用Python获取电子邮件的收件人?

Python:如何从gmail API获取电子邮件的主题

用Python阅读Gmail电子邮件

如何使用Python使用Gmail发送电子邮件

Python获取电子邮件

为什么无法获取电子邮件(python,gmail,django)?

我如何获取收到的日期/从Python中的电子邮件发送

Gmail api:如何使用ID获取收件人的电子邮件?

从Gmail到Gmail,Streak电子邮件跟踪如何工作?

如何通过Jmeter获取特定电子邮件ID中收到的最新电子邮件

如何对收到的电子邮件进行分类

如何使Outlook 2010显示我收到的邮件的名称而不是电子邮件地址?

如何从Gmail获取特定电子邮件的线程ID

如何根据收到的日期获取电子邮件

如何从python发送电子邮件

如何使用电子邮件获取Gmail用户的个人资料图片

如何使用 Postfix 拒绝收到的电子邮件?

如何从我的 Gmail 帐户中获取所有电子邮件联系人?

如何从 Gmail、Hotmail、Yahoo 等电子邮件应用程序获取数据?

如何从今天收到的 Outlook 邮件中提取电子邮件附件?

尝试通过 python 获取电子邮件

当我收到此 java.lang 错误时,如何使用 Gmail 使用 R 发送电子邮件