跳转到主要内容

Python EML解析库

项目描述

Code Health Documentation Status PyPI PyPI pyversions

eml_parser是一个Python模块,用于解析eml文件,并返回电子邮件中找到的各种信息以及计算信息。

提取和生成的信息包括但不限于

  • 附件
    • 散列值
    • 名称
  • 发件人、收件人、抄送
  • 接收服务器路径
  • 主题
  • 从邮件文本内容(包括HTML正文/附件)中解析出的URL列表

请随时向我发送您的评论/拉取请求。

有关变更日志,请参阅 CHANGELOG.md

安装

pip install eml_parser[filemagic]

:warning: 注意:如果您不想/不能使用 file-magic(例如,如果您正在使用 python-magic),请通过以下方式安装:

pip install eml_parser

已知问题

OSX 用户

请确保安装 libmagic,否则 eml_parser 将无法工作。

示例用法

import datetime
import json
import eml_parser


def json_serial(obj):
  if isinstance(obj, datetime.datetime):
      serial = obj.isoformat()
      return serial


with open('sample.eml', 'rb') as fhdl:
  raw_email = fhdl.read()

ep = eml_parser.EmlParser()
parsed_eml = ep.decode_email_bytes(raw_email)

print(json.dumps(parsed_eml, default=json_serial))

对于一个简约的 EML 文件,结果可能如下所示:

  {
    "body": [
      {
        "content_header": {
          "content-language": [
            "en-US"
          ]
        },
        "hash": "6c9f343bdb040e764843325fc5673b0f43a021bac9064075d285190d6509222d"
      }
    ],
    "header": {
      "received_src": null,
      "from": "john.doe@example.com",
      "to": [
        "test@example.com"
      ],
      "subject": "Sample EML",
      "received_foremail": [
        "test@example.com"
      ],
      "date": "2013-04-26T11:15:47+00:00",
      "header": {
        "content-language": [
          "en-US"
        ],
        "received": [
          "from localhost\tby mta.example.com (Postfix) with ESMTPS id 6388F684168\tfor <test@example.com>; Fri, 26 Apr 2013 13:15:55 +0200"
        ],
        "to": [
          "test@example.com"
        ],
        "subject": [
          "Sample EML"
        ],
        "date": [
          "Fri, 26 Apr 2013 11:15:47 +0000"
        ],
        "message-id": [
          "<F96257F63EAEB94C890EA6CE1437145C013B01FA@example.com>"
        ],
        "from": [
          "John Doe <john.doe@example.com>"
        ]
      },
      "received_domain": [
        "mta.example.com"
      ],
      "received": [
        {
          "with": "esmtps id 6388f684168",
          "for": [
            "test@example.com"
          ],
          "by": [
            "mta.example.com"
          ],
          "date": "2013-04-26T13:15:55+02:00",
          "src": "from localhost by mta.example.com (postfix) with esmtps id 6388f684168 for <test@example.com>; fri, 26 apr 2013 13:15:55 +0200"
        }
      ]
    }
  }

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。

源代码分发

eml_parser-2.0.0.tar.gz (1.0 MB 查看哈希值)

上传时间 源代码

构建分发

eml_parser-2.0.0-py3-none-any.whl (35.1 kB 查看哈希值)

上传时间 Python 3

由以下机构支持