python读取本地foxmail_用Python读取Foxmail导出的邮件
时间: 2023-07-18 10:22:20 浏览: 273
要使用Python读取Foxmail导出的邮件,可以使用Python内置的邮件模块 `email`。
首先,需要打开Foxmail导出的邮件文件,可以使用Python的 `open()` 函数来实现:
```python
with open('邮件文件.eml', 'rb') as f:
content = f.read()
```
然后,可以使用 `email` 模块中的 `message_from_bytes()` 函数将文件内容转换为一个 `email.message.Message` 对象:
```python
import email
msg = email.message_from_bytes(content)
```
现在,可以通过 `Message` 对象获取邮件的各种信息,例如邮件主题、发件人、收件人、邮件正文等:
```python
subject = msg['Subject']
from_address = msg['From']
to_address = msg['To']
body = msg.get_payload(decode=True).decode()
```
注意,邮件正文可能是HTML格式,需要进行解码和转换。
另外,还可以通过 `Message` 对象的 `walk()` 方法遍历邮件中的所有部分,例如附件等。
以上是一个简单的示例,你可以根据自己的需求进一步完善代码。
相关问题
python自动从本地foxmail中获取某一主题的读取_用Python读取Foxmail导出的邮件
下面是一个示例代码,可以实现自动从本地Foxmail中获取某一主题的邮件:
```python
import os
import email
import codecs
def get_mail_by_subject(subject):
"""
根据主题获取邮件
"""
mail_dir = 'Foxmail邮件存储路径'
for root, dirs, files in os.walk(mail_dir):
for file_name in files:
if file_name.endswith('.eml'):
file_path = os.path.join(root, file_name)
with open(file_path, 'rb') as f:
content = f.read()
try:
msg = email.message_from_bytes(content)
if msg['Subject'] == subject:
return read_foxmail_eml(file_path)
except:
pass
def read_foxmail_eml(eml_path):
"""
读取Foxmail导出的.eml文件
"""
with open(eml_path, 'rb') as f:
content = f.read()
msg = email.message_from_bytes(content)
subject = msg['Subject']
from_address = msg['From']
to_address = msg['To']
date = msg['Date']
body = msg.get_payload(decode=True).decode()
# 处理HTML格式的正文
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
filename = part.get_filename()
if content_type == 'text/html':
body = part.get_payload(decode=True).decode()
else:
content_type = msg.get_content_type()
if content_type == 'text/html':
body = msg.get_payload(decode=True).decode()
# 处理附件
attachments = []
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
filename = part.get_filename()
if filename:
filename = decode_str(filename)
data = part.get_payload(decode=True)
attachment = {'filename': filename, 'data': data}
attachments.append(attachment)
result = {'from': from_address, 'to': to_address, 'subject': subject, 'date': date, 'body': body, 'attachments': attachments}
return result
def decode_str(s):
"""
解码字符串
"""
value, charset = email.header.decode_header(s)[0]
if charset:
value = value.decode(charset)
return value
if __name__ == '__main__':
# 测试代码
subject = '测试邮件主题'
result = get_mail_by_subject(subject)
print(result)
```
你只需要将 `mail_dir` 改为你本地Foxmail邮件存储路径,将 `subject` 改为你要查找的邮件主题,运行代码即可自动获取该主题的邮件内容。
python读取本地foxmail举例实际案例代码
以下是一个读取本地Foxmail导出邮件的实际案例代码:
```python
import os
import email
import codecs
def read_foxmail_eml(eml_path):
"""
读取Foxmail导出的.eml文件
"""
with open(eml_path, 'rb') as f:
content = f.read()
msg = email.message_from_bytes(content)
subject = msg['Subject']
from_address = msg['From']
to_address = msg['To']
date = msg['Date']
body = msg.get_payload(decode=True).decode()
# 处理HTML格式的正文
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
filename = part.get_filename()
if content_type == 'text/html':
body = part.get_payload(decode=True).decode()
else:
content_type = msg.get_content_type()
if content_type == 'text/html':
body = msg.get_payload(decode=True).decode()
# 处理附件
attachments = []
if msg.is_multipart():
for part in msg.walk():
content_type = part.get_content_type()
filename = part.get_filename()
if filename:
filename = decode_str(filename)
data = part.get_payload(decode=True)
attachment = {'filename': filename, 'data': data}
attachments.append(attachment)
result = {'from': from_address, 'to': to_address, 'subject': subject, 'date': date, 'body': body, 'attachments': attachments}
return result
def decode_str(s):
"""
解码字符串
"""
value, charset = email.header.decode_header(s)[0]
if charset:
value = value.decode(charset)
return value
if __name__ == '__main__':
# 测试代码
eml_path = 'test.eml'
result = read_foxmail_eml(eml_path)
print(result)
```
这个代码可以读取指定路径下的.eml文件,并解析邮件内容,包括发件人、收件人、主题、日期、正文和附件等信息。你只需要将 `eml_path` 改为你的.eml文件路径即可。
阅读全文