python 获取邮件_python 邮件读取

该博客介绍如何使用Python从Gmail获取未读邮件。首先导入所需的库,如googleapiclient、oauthlib等。定义了访问Gmail API的范围,然后通过检查token.pickle文件来获取或刷新用户访问令牌。接着连接到Gmail API,列出所有未读邮件,并解析邮件的主体、发件人、主题等信息,最后将这些信息写入文件。
摘要由CSDN通过智能技术生成

#import the required libraries

from googleapiclient.discovery importbuildfrom google_auth_oauthlib.flow importInstalledAppFlowfrom google.auth.transport.requests importRequestimportpickleimportos.pathimportbase64importemailfrom bs4 importBeautifulSoup#Define the SCOPES. If modifying it, delete the token.pickle file.

SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']defgetEmails():#Variable creds will store the user access token.

#If no valid token found, we will create one.

creds =None#The file token.pickle contains the user access token.

#Check if it exists

if os.path.exists('token.pickle'):#Read the token from the file and store it in the variable creds

with open('token.pickle', 'rb') as token:

creds=pickle.load(token)#If credentials are not available or are invalid, ask the user to log in.

if not creds or notcreds.valid:if creds and creds.expired andcreds.refresh_token:

creds.refresh(Request())else:

flow= InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)

creds= flow.run_local_server(port=0)#Save the access token in token.pickle file for the next run

with open('token.pickle', 'wb') as token:

pickle.dump(creds, token)#Connect to the Gmail API

service = build('gmail', 'v1', credentials=creds)#request a list of all the messages

result = service.users().messages().list(userId='me',labelIds=['INBOX'],q="is:unread").execute()#result = service.users().messages().list(userId='me',labelIds=['INBOX']).execute()

#We can also pass maxResults to get any number of emails. Like this:

#result = service.users().messages().list(maxResults=200, userId='me').execute()

messages = result.get('messages')#messages is a list of dictionaries where each dictionary contains a message id.

#iterate through all the messages

for msg inmessages:#Get the message from its id

txt = service.users().messages().get(userId='me', id=msg['id']).execute()#Use try-except to avoid any Errors

try:#Get value of 'payload' from dictionary 'txt'

payload = txt['payload']

headers= payload['headers']#Look for Subject and Sender Email in the headers

for d inheaders:if d['name'] == 'Subject':

subject= d['value']if d['name'] == 'From':

sender= d['value']#The Body of the message is in Encrypted format. So, we have to decode it.

#Get the data and decode it with base 64 decoder.

parts = payload.get('parts')[0]

data= parts['body']['data']

data= data.replace("-","+").replace("_","/")

decoded_data=base64.b64decode(data)#Now, the data obtained is in lxml. So, we will parse

#it with BeautifulSoup library

soup = BeautifulSoup(decoded_data , "lxml")

body=soup.body()

a=soup.body.text#Printing the subject, sender's email and message

with open('C:\\Users\\Eleni\\test.txt','a',encoding='utf-8') as f:

f.write('---------------------------------------------next letter---------------------------------------------')

f.write(subject)

f.write('\r\n')

f.write(a)

f.write('\r\n')

f.write(body)except:passgetEmails()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值