sophuwu.site > mailboxxer
files and shizz
sophuwu sophie@skisiel.com
Wed, 24 Jul 2024 04:38:42 +0200
commit

cb78ab19b97973575fca793e32b13fbf3d698361

parent

d8c921cb24faa0d4bfb73d0323af4a97cad2bd4e

2 files changed, 74 insertions(+), 7 deletions(-)

jump to
A files.go

@@ -0,0 +1,66 @@

+package main + +import ( + "encoding/base64" + "fmt" + "io" + "mime" + "mime/multipart" + "net/mail" + "net/textproto" + "strings" +) + +func getfiles(files *map[string][]byte, parts interface{}) { + var body io.Reader + var head textproto.MIMEHeader + switch part := parts.(type) { + case *mail.Message: + body = part.Body + head = textproto.MIMEHeader(part.Header) + case *multipart.Part: + body = part + head = part.Header + } + mediaType, params, _ := mime.ParseMediaType(head.Get("Content-Type")) + + if strings.HasPrefix(mediaType, "multipart/") { + mr := multipart.NewReader(body, params["boundary"]) + var part *multipart.Part + var err error + for { + if part, err = mr.NextPart(); err != nil { + break + } + getfiles(files, part) + part.Close() + } + return + } + content, _ := io.ReadAll(body) + if head.Get("Content-Transfer-Encoding") == "base64" { + content, _ = base64.StdEncoding.DecodeString(string(content)) + } + if len(content) == 0 { + return + } + name := params["name"] + if strings.HasPrefix(mediaType, "text/") || strings.HasPrefix(mediaType, "message/") { + name = "body.txt" + if strings.Contains(mediaType, "html") { + name = "index.html" + } + } + (*files)[name] = append((*files)[name], content...) +} + +func EmlFiles(eml *mail.Message) map[string][]byte { + s := "" + for k := range eml.Header { + s += fmt.Sprintf("%s: %s\n", k, eml.Header.Get(k)) + } + files := make(map[string][]byte) + files["header.txt"] = []byte(s) + getfiles(&files, eml) + return files +}
M mailbox.pymailbox.py

@@ -1,6 +1,7 @@

#!/bin/python3.11 import email +import sys from email import policy from email.parser import BytesParser import os

@@ -14,12 +15,12 @@

def showparts(eml): for part in eml.walk(): - if part.get_content_disposition() == None and part.get_content_maintype() == 'text': - print(part.get_content_maintype(), part.get_content_subtype(), len(part.get_payload(decode=True))) - elif part.get_content_disposition() == 'attachment': - print(part.get_content_disposition(),part.get_content_maintype(), part.get_content_subtype(), len(part.get_payload(decode=True))) + if part.get_payload(decode=True) is not None: + print(part.get_content_type(), part.get_content_disposition(), part.get_filename(), len(part.get_payload(decode=True))) -eml = openeml('test/pro.eml') -print(eml.get('Subject'), eml.get('From'), eml.get('Date')) -showparts(eml)+for file in sys.argv[1:]: + eml = openeml(file) + print(file) + showparts(eml) + print()