gpt4 book ai didi

python从base64解码电子邮件

转载 作者:行者123 更新时间:2023-12-05 04:55:44 25 4
gpt4 key购买 nike

你好,我使用 python 脚本从特定地址邮件中获取消息似乎一切正常,但我对可打印结果是 base64 代码有疑问。我想在打印最终结果时解码结果以获取解码消息,请帮忙!!已经谢谢了

使用的代码。

# Importing libraries 
import imaplib, email

user = 'USER_EMAIL_ADDRESS'
password = 'USER_PASSWORD'
imap_url = 'imap.gmail.com'

# Function to get email content part i.e its body part
def get_body(msg):
if msg.is_multipart():
return get_body(msg.get_payload(0))
else:
return msg.get_payload(None, True)

# Function to search for a key value pair
def search(key, value, con):
result, data = con.search(None, key, '"{}"'.format(value))
return data

# Function to get the list of emails under this label
def get_emails(result_bytes):
msgs = [] # all the email data are pushed inside an array
for num in result_bytes[0].split():
typ, data = con.fetch(num, 'BODY.PEEK[1]')
msgs.append(data)

return msgs

# this is done to make SSL connnection with GMAIL
con = imaplib.IMAP4_SSL(imap_url)

# logging the user in
con.login(user, password)

# calling function to check for email under this label
con.select('Inbox')

# fetching emails from this user "tu**h*****1@gmail.com"
msgs = get_emails(search('FROM', 'MY_ANOTHER_GMAIL_ADDRESS', con))

# Uncomment this to see what actually comes as data
# print(msgs)


# Finding the required content from our msgs
# User can make custom changes in this part to
# fetch the required content he / she needs

# printing them by the order they are displayed in your gmail
for msg in msgs[::-1]:
for sent in msg:
if type(sent) is tuple:

# encoding set as utf-8
content = str(sent[1], 'utf-8')
data = str(content)

# Handling errors related to unicodenecode
try:
indexstart = data.find("ltr")
data2 = data[indexstart + 5: len(data)]
indexend = data2.find("</div>")

# printtng the required content which we need
# to extract from our email i.e our body
print(data2[0: indexend])

except UnicodeEncodeError as e:
pass

打印的结果

'''

aGVsbG8gd29yZCBpYW0gdGhlIG1lc3NhZ2UgZnJvbSBnbWFpbA==

'''

最佳答案

您可以只使用 base64 module解码 base64 编码的字符串:

import base64

your_string="aGVsbG8gV29ybGQ==" # the base64 encoded string you need to decode
result = base64.b64decode(your_string.encode("utf8")).decode("utf8")
print(result)

编辑:编码从 ASCII 更改为 utf-8

关于python从base64解码电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65243601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com