【问题标题】:How to read emails from gmail?如何阅读来自 gmail 的电子邮件?
【发布时间】:2022-08-17 10:55:09
【问题描述】:

我正在尝试将我的 gmail 连接到 python,但显示此错误:

我已经检查了我的密码,知道可能是什么吗?

b\'[AUTHENTICATIONFAILED] Invalid credentials (Failure)\'
Traceback (most recent call last):
  File \"/Users/myuser/Documents/migrations/untitled3.py\", line 29, in read_email_from_gmail
    mail.login(FROM_EMAIL,FROM_PWD)
  File \"/Users/myuser/opt/anaconda3/lib/python3.9/imaplib.py\", line 612, in login
    raise self.error(dat[-1])
imaplib.IMAP4.error: b\'[AUTHENTICATIONFAILED] Invalid credentials (Failure)\'

这是我的代码: 我也想知道我可以使用哪个端口?

import smtplib
import time
import imaplib
import email
import traceback 

ORG_EMAIL = \"@gmail.com\" 
FROM_EMAIL = \"myemail\" + ORG_EMAIL 
FROM_PWD = \"mypassword\" 
SMTP_SERVER = \"smtp.gmail.com\" 
SMTP_PORT = ??

def read_email_from_gmail():
    try:
        mail = imaplib.IMAP4_SSL(SMTP_SERVER)
        mail.login(FROM_EMAIL,FROM_PWD)
        mail.select(\'inbox\')

        data = mail.search(None, \'ALL\')
        mail_ids = data[1]
        id_list = mail_ids[0].split()   
        first_email_id = int(id_list[0])
        latest_email_id = int(id_list[-1])

        for i in range(latest_email_id,first_email_id, -1):
            data = mail.fetch(str(i), \'(RFC822)\' )
            for response_part in data:
                arr = response_part[0]
                if isinstance(arr, tuple):
                    msg = email.message_from_string(str(arr[1],\'utf-8\'))
                    email_subject = msg[\'subject\']
                    email_from = msg[\'from\']
                    print(\'From : \' + email_from + \'\\n\')
                    print(\'Subject : \' + email_subject + \'\\n\')

    except Exception as e:
        traceback.print_exc() 
        print(str(e))

read_email_from_gmail()

我的主要目标是能够从每封电子邮件中获取 CSV 文件,但现在我只想阅读邮件。

    标签: python email gmail


    【解决方案1】:

    这是the problem,Gmail 有一个不允许使用不安全应用程序的新政策。

    为帮助确保您的帐户安全,自 2022 年 5 月 30 日起,Google 不再支持使用第三方应用或设备,这些应用或设备要求您仅使用您的用户名和密码登录您的 Google 帐户。

    【讨论】:

      【解决方案2】:

      阅读电子邮件的最佳方式是使用 GMAIL API。自 2022 年 5 月 30 日起,Google 已停止通过任何外部应用程序使用用户名和密码访问 gmail 帐户。 这就是你从 python 读取 gmail 的方式。

      这个应用程序的作用:

      • 阅读 gmail 邮件(未读)
      • 阅读邮件后将邮件设为已读
      import os.path
      import base64
      import json
      import re
      import time
      from google.auth.transport.requests import Request
      from google.oauth2.credentials import Credentials
      from google_auth_oauthlib.flow import InstalledAppFlow
      from googleapiclient.discovery import build
      import logging
      import requests
      
      SCOPES = ['https://www.googleapis.com/auth/gmail.readonly','https://www.googleapis.com/auth/gmail.modify']
      
      def readEmails():
          """Shows basic usage of the Gmail API.
          Lists the user's Gmail labels.
          """
          creds = None
          # The file token.json stores the user's access and refresh tokens, and is
          # created automatically when the authorization flow completes for the first
          # time.
          if os.path.exists('token.json'):
              creds = Credentials.from_authorized_user_file('token.json', SCOPES)
          # If there are no (valid) credentials available, let the user log in.
          if not creds or not creds.valid:
              if creds and creds.expired and creds.refresh_token:
                  creds.refresh(Request())
              else:
                  flow = InstalledAppFlow.from_client_secrets_file(               
                      # your creds file here. Please create json file as here https://cloud.google.com/docs/authentication/getting-started
                      'my_cred_file.json', SCOPES)
                  creds = flow.run_local_server(port=0)
              # Save the credentials for the next run
              with open('token.json', 'w') as token:
                  token.write(creds.to_json())
          try:
              # Call the Gmail API
              service = build('gmail', 'v1', credentials=creds)
              results = service.users().messages().list(userId='me', labelIds=['INBOX'], q="is:unread").execute()
              messages = results.get('messages',[]);
              if not messages:
                  print('No new messages.')
              else:
                  message_count = 0
                  for message in messages:
                      msg = service.users().messages().get(userId='me', id=message['id']).execute()                
                      email_data = msg['payload']['headers']
                      for values in email_data:
                          name = values['name']
                          if name == 'From':
                              from_name= values['value']                
                              for part in msg['payload']['parts']:
                                  try:
                                      data = part['body']["data"]
                                      byte_code = base64.urlsafe_b64decode(data)
      
                                      text = byte_code.decode("utf-8")
                                      print ("This is the message: "+ str(text))
      
                                      # mark the message as read (optional)
                                      msg  = service.users().messages().modify(userId='me', id=message['id'], body={'removeLabelIds': ['UNREAD']}).execute()                                                       
                                  except BaseException as error:
                                      pass                            
          except Exception as error:
              print(f'An error occurred: {error}')
      

      您需要在 Google 帐户中创建凭据文件 如何在此处创建凭证文件https://cloud.google.com/docs/authentication/getting-started

      【讨论】:

        【解决方案3】:
        1. 如果您要以这种方式进行检查,您需要在您的 Gmail 帐户中启用'Less secure apps'。因此,最好使用Gmail API

        2. 未设置 SMTP 端口 - 确保您使用的是正确的端口(993)

        【讨论】:

          【解决方案4】:

          正如 Carlos 指出的那样,谷歌不再允许不安全的应用程序访问 GMail。 “不太安全的应用程序”选项不再可用。我遇到了同样的 Gmail 登录问题,但我很高兴找到下面的文章并解决了这个问题。

          https://towardsdatascience.com/how-to-easily-automate-emails-with-python-8b476045c151

          【讨论】:

            【解决方案5】:

            smtp.gmail.com 用于发送电子邮件,阅读要连接到imap.gmail.com 的电子邮件。它适用于我的App password。我给sync multiple email servers写了一个简短的应用程序,用谷歌邮件测试过。还是如上所述,推荐使用Google API。在我的 Github 帐户上,我有一个使用 Google APIsorting emails with machine learning 的示例应用程序,如果你感兴趣的话。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-01-25
              • 2023-02-04
              • 1970-01-01
              • 1970-01-01
              • 2018-05-11
              • 2020-07-21
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多