繁体   English   中英

python 从 base64 解码 email

[英]python decode email from base64

你好 iam 使用 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模块来解码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) 

根据 mCoding 的建议,编码从 ASCII 更改为 utf-8

如果您需要查找所有编码的位置(可以是主题、发件人、到 email 地址和名称),下面的代码可能有用。 给定 contentData 是整个 email,

import re, base64
encodedParts=re.findall('(=\?(.+)\?B\?(.+)\?=)', contentData)
for part in encodedParts:
  encodedPart = part[0]
  charset = part[1]
  encodedContent = part[2]
  contentData = contentData.replace(encodedPart, base64.b64decode(encodedContent).decode(charset))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM